Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
16 KiB

  1. /******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1994
  4. *
  5. * TITLE: REGNET.C
  6. *
  7. * VERSION: 4.01
  8. *
  9. * AUTHOR: Tracy Sharpe
  10. *
  11. * DATE: 03 May 1994
  12. *
  13. * Remote registry support for the Registry Editor.
  14. *
  15. *******************************************************************************/
  16. #include "pch.h"
  17. #include "regedit.h"
  18. #include "regkey.h"
  19. #include "regresid.h"
  20. #include <shlobj.h>
  21. #include "reghelp.h"
  22. extern HRESULT SelectComputer(HWND hWnd, LPTSTR pszRemoteName, int cchMax);
  23. const DWORD s_RegConnectHelpIDs[] = {
  24. IDC_REMOTENAME, IDH_REGEDIT_CONNECT,
  25. IDC_BROWSE, IDH_REGEDIT_CONNECT_BROWSE,
  26. 0, 0
  27. };
  28. const DWORD s_RegDisconnectHelpIDs[] = {
  29. IDC_REMOTELIST, IDH_REGEDIT_DISCONNECT,
  30. 0, 0
  31. };
  32. INT_PTR
  33. PASCAL
  34. RegConnectDlgProc(
  35. HWND hWnd,
  36. UINT Message,
  37. WPARAM wParam,
  38. LPARAM lParam
  39. );
  40. VOID
  41. PASCAL
  42. RegConnect_OnCommandBrowse(
  43. HWND hWnd
  44. );
  45. INT_PTR
  46. PASCAL
  47. RegDisconnectDlgProc(
  48. HWND hWnd,
  49. UINT Message,
  50. WPARAM wParam,
  51. LPARAM lParam
  52. );
  53. INT_PTR
  54. PASCAL
  55. RegDisconnect_OnInitDialog(
  56. HWND hWnd
  57. );
  58. VOID
  59. PASCAL
  60. RegDisconnect_OnCommandOk(
  61. HWND hWnd
  62. );
  63. /*******************************************************************************
  64. *
  65. * RegEdit_OnCommandConnect
  66. *
  67. * DESCRIPTION:
  68. *
  69. * PARAMETERS:
  70. *
  71. *******************************************************************************/
  72. VOID
  73. PASCAL
  74. RegEdit_OnCommandConnect(HWND hWnd)
  75. {
  76. UINT ErrorStringID;
  77. TCHAR RemoteName[MAX_PATH];
  78. LPTSTR lpUnslashedRemoteName;
  79. TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  80. DWORD cbComputerName;
  81. TV_ITEM TVItem;
  82. HTREEITEM hPrevTreeItem;
  83. TCHAR ConnectedName[MAX_PATH];
  84. int CompareResult;
  85. HKEY hLocalMachineKey;
  86. HWND hKeyTreeWnd;
  87. TV_INSERTSTRUCT TVInsertStruct;
  88. UINT Index;
  89. TCHAR CheckChildrenKeyName[MAXKEYNAME];
  90. //
  91. // Query the user for the name of the remote computer to connect to.
  92. //
  93. if (SUCCEEDED(SelectComputer(hWnd, (LPTSTR)RemoteName, ARRAYSIZE(RemoteName))))
  94. {
  95. RegEdit_SetWaitCursor(TRUE);
  96. //
  97. //
  98. //
  99. lpUnslashedRemoteName = (RemoteName[0] == TEXT('\\') &&
  100. RemoteName[1] == TEXT('\\')) ? &RemoteName[2] : &RemoteName[0];
  101. CharLower(lpUnslashedRemoteName);
  102. CharUpperBuff(lpUnslashedRemoteName, 1);
  103. //
  104. // Check if the user is trying to connect to the local computer and prevent
  105. // this.
  106. //
  107. cbComputerName = ARRAYSIZE(ComputerName);
  108. if (GetComputerName(ComputerName, &cbComputerName)) {
  109. if (lstrcmpi(lpUnslashedRemoteName, ComputerName) == 0) {
  110. ErrorStringID = IDS_CONNECTNOTLOCAL;
  111. goto error_ShowDialog;
  112. }
  113. }
  114. //
  115. // Check if the user is trying to connect to an already existing registry
  116. // connection and prevent this.
  117. //
  118. hKeyTreeWnd = g_RegEditData.hKeyTreeWnd;
  119. TVItem.mask = TVIF_TEXT | TVIF_PARAM | TVIF_HANDLE;
  120. TVItem.hItem = TreeView_GetRoot(hKeyTreeWnd);
  121. TVItem.pszText = ConnectedName;
  122. TVItem.cchTextMax = ARRAYSIZE(ConnectedName);
  123. while (TRUE)
  124. {
  125. hPrevTreeItem = TVItem.hItem;
  126. TVItem.hItem = TreeView_GetNextSibling(hKeyTreeWnd, TVItem.hItem);
  127. if (TVItem.hItem == NULL)
  128. break;
  129. TreeView_GetItem(hKeyTreeWnd, &TVItem);
  130. CompareResult = lstrcmpi(lpUnslashedRemoteName, ConnectedName);
  131. if (CompareResult == 0) {
  132. //
  133. // We're already connected to this machine. Set the focus to the
  134. // connection so the user can see where it is.
  135. //
  136. TreeView_SelectItem(hKeyTreeWnd, TVItem.hItem);
  137. return;
  138. }
  139. else if (CompareResult < 0)
  140. break;
  141. }
  142. //
  143. // Attempt to connect to the HKEY_LOCAL_MACHINE of the remote computer.
  144. // If this fails, assume that the computer doesn't exist or doesn't have
  145. // the registry server running.
  146. //
  147. switch (RegConnectRegistry(RemoteName, HKEY_LOCAL_MACHINE,
  148. &hLocalMachineKey)) {
  149. case ERROR_SUCCESS:
  150. break;
  151. case ERROR_ACCESS_DENIED:
  152. ErrorStringID = IDS_CONNECTACCESSDENIED;
  153. goto error_ShowDialog;
  154. default:
  155. ErrorStringID = IDS_CONNECTBADNAME;
  156. goto error_ShowDialog;
  157. }
  158. //
  159. // The connection to HKEY_LOCAL_MACHINE was successful, so add a tree item
  160. // for the remote computer and all of its predefined roots.
  161. //
  162. hKeyTreeWnd = g_RegEditData.hKeyTreeWnd;
  163. ErrorStringID = 0;
  164. TVInsertStruct.hParent = TVI_ROOT;
  165. TVInsertStruct.hInsertAfter = hPrevTreeItem;
  166. TVInsertStruct.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE |
  167. TVIF_PARAM | TVIF_CHILDREN;
  168. TVInsertStruct.item.iImage = IMAGEINDEX(IDI_REMOTE);
  169. TVInsertStruct.item.iSelectedImage = IMAGEINDEX(IDI_REMOTE);
  170. TVInsertStruct.item.cChildren = TRUE;
  171. TVInsertStruct.item.lParam = 0;
  172. TVInsertStruct.item.pszText = lpUnslashedRemoteName;
  173. TVInsertStruct.hParent = TreeView_InsertItem(hKeyTreeWnd, &TVInsertStruct);
  174. TVInsertStruct.item.iImage = IMAGEINDEX(IDI_FOLDER);
  175. TVInsertStruct.item.iSelectedImage = IMAGEINDEX(IDI_FOLDEROPEN);
  176. for (Index = 0; Index < NUMBER_REGISTRY_ROOTS; Index++) {
  177. TVInsertStruct.item.pszText = g_RegistryRoots[Index].lpKeyName;
  178. //There is no way to determine the current user remotely
  179. //We would end up mapping the default user to current user
  180. //so let's just not show this key remotely
  181. if ((Index == INDEX_HKEY_CURRENT_USER) || (Index == INDEX_HKEY_CLASSES_ROOT))
  182. continue;
  183. if (Index == INDEX_HKEY_LOCAL_MACHINE)
  184. TVInsertStruct.item.lParam = (LPARAM) hLocalMachineKey;
  185. else {
  186. #ifdef WINNT
  187. if((Index == INDEX_HKEY_DYN_DATA) || (Index == INDEX_HKEY_CURRENT_CONFIG)) {
  188. continue;
  189. }
  190. #endif
  191. if (RegConnectRegistry(RemoteName, g_RegistryRoots[Index].hKey,
  192. (PHKEY) &TVInsertStruct.item.lParam) != ERROR_SUCCESS) {
  193. ErrorStringID = IDS_CONNECTROOTFAILED;
  194. continue;
  195. }
  196. }
  197. TVInsertStruct.item.cChildren =
  198. (RegEnumKey((HKEY) TVInsertStruct.item.lParam, 0,
  199. CheckChildrenKeyName, ARRAYSIZE(CheckChildrenKeyName)) ==
  200. ERROR_SUCCESS);
  201. TreeView_InsertItem(hKeyTreeWnd, &TVInsertStruct);
  202. }
  203. TreeView_Expand(hKeyTreeWnd, TVInsertStruct.hParent, TVE_EXPAND);
  204. TreeView_EnsureVisible(hKeyTreeWnd, TVInsertStruct.hParent);
  205. RegEdit_SetWaitCursor(FALSE);
  206. TreeView_SelectItem(hKeyTreeWnd, TVInsertStruct.hParent);
  207. SetFocus(hKeyTreeWnd);
  208. if (ErrorStringID != 0) {
  209. error_ShowDialog:
  210. InternalMessageBox(g_hInstance, hWnd, MAKEINTRESOURCE(ErrorStringID),
  211. MAKEINTRESOURCE(IDS_CONNECTERRORTITLE), MB_ICONERROR | MB_OK,
  212. RemoteName);
  213. }
  214. }
  215. }
  216. /*******************************************************************************
  217. *
  218. * RegConnectDlgProc
  219. *
  220. * DESCRIPTION:
  221. *
  222. * PARAMETERS:
  223. *
  224. *******************************************************************************/
  225. INT_PTR
  226. PASCAL
  227. RegConnectDlgProc(
  228. HWND hWnd,
  229. UINT Message,
  230. WPARAM wParam,
  231. LPARAM lParam
  232. )
  233. {
  234. LPTSTR lpRemoteName;
  235. switch (Message) {
  236. case WM_INITDIALOG:
  237. SetWindowLongPtr(hWnd, DWLP_USER, (LONG) lParam);
  238. SendDlgItemMessage(hWnd, IDC_REMOTENAME, EM_SETLIMITTEXT,
  239. MAX_PATH, 0);
  240. break;
  241. case WM_COMMAND:
  242. switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  243. case IDC_REMOTENAME:
  244. if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
  245. EnableWindow(GetDlgItem(hWnd, IDOK),
  246. SendMessage(GET_WM_COMMAND_HWND(wParam, lParam),
  247. WM_GETTEXTLENGTH, 0, 0) != 0);
  248. break;
  249. case IDC_BROWSE:
  250. RegConnect_OnCommandBrowse(hWnd);
  251. break;
  252. case IDOK:
  253. lpRemoteName = (LPTSTR) GetWindowLongPtr(hWnd, DWLP_USER);
  254. GetDlgItemText(hWnd, IDC_REMOTENAME, lpRemoteName,
  255. MAX_PATH);
  256. // FALL THROUGH
  257. case IDCANCEL:
  258. EndDialog(hWnd, GET_WM_COMMAND_ID(wParam, lParam));
  259. break;
  260. }
  261. break;
  262. case WM_HELP:
  263. WinHelp(((LPHELPINFO) lParam)-> hItemHandle, g_pHelpFileName,
  264. HELP_WM_HELP, (ULONG_PTR) s_RegConnectHelpIDs);
  265. break;
  266. case WM_CONTEXTMENU:
  267. WinHelp((HWND) wParam, g_pHelpFileName, HELP_CONTEXTMENU,
  268. (ULONG_PTR) s_RegConnectHelpIDs);
  269. break;
  270. default:
  271. return FALSE;
  272. }
  273. return TRUE;
  274. }
  275. /*******************************************************************************
  276. *
  277. * RegConnect_OnCommandBrowse
  278. *
  279. * DESCRIPTION:
  280. *
  281. * PARAMETERS:
  282. *
  283. *******************************************************************************/
  284. VOID
  285. PASCAL
  286. RegConnect_OnCommandBrowse(
  287. HWND hWnd
  288. )
  289. {
  290. BROWSEINFO BrowseInfo;
  291. LPITEMIDLIST pidlComputer;
  292. TCHAR RemoteName[MAX_PATH];
  293. BrowseInfo.hwndOwner = hWnd;
  294. BrowseInfo.pidlRoot = (LPITEMIDLIST) MAKEINTRESOURCE(CSIDL_NETWORK);
  295. BrowseInfo.pszDisplayName = RemoteName;
  296. BrowseInfo.lpszTitle = LoadDynamicString(IDS_COMPUTERBROWSETITLE);
  297. BrowseInfo.ulFlags = BIF_BROWSEFORCOMPUTER;
  298. BrowseInfo.lpfn = NULL;
  299. if ((pidlComputer = SHBrowseForFolder(&BrowseInfo)) != NULL)
  300. {
  301. SHFree(pidlComputer);
  302. SetDlgItemText(hWnd, IDC_REMOTENAME, RemoteName);
  303. EnableWindow(GetDlgItem(hWnd, IDOK), TRUE);
  304. }
  305. DeleteDynamicString(BrowseInfo.lpszTitle);
  306. }
  307. /*******************************************************************************
  308. *
  309. * RegEdit_OnCommandDisconnect
  310. *
  311. * DESCRIPTION:
  312. *
  313. * PARAMETERS:
  314. *
  315. *******************************************************************************/
  316. VOID
  317. PASCAL
  318. RegEdit_OnCommandDisconnect(
  319. HWND hWnd
  320. )
  321. {
  322. DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_REGDISCONNECT), hWnd,
  323. RegDisconnectDlgProc);
  324. }
  325. /*******************************************************************************
  326. *
  327. * RegDisconnectDlgProc
  328. *
  329. * DESCRIPTION:
  330. *
  331. * PARAMETERS:
  332. *
  333. *******************************************************************************/
  334. INT_PTR
  335. PASCAL
  336. RegDisconnectDlgProc(
  337. HWND hWnd,
  338. UINT Message,
  339. WPARAM wParam,
  340. LPARAM lParam
  341. )
  342. {
  343. switch (Message) {
  344. case WM_INITDIALOG:
  345. return RegDisconnect_OnInitDialog(hWnd);
  346. case WM_COMMAND:
  347. switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  348. case IDOK:
  349. RegDisconnect_OnCommandOk(hWnd);
  350. // FALL THROUGH
  351. case IDCANCEL:
  352. EndDialog(hWnd, 0);
  353. break;
  354. }
  355. break;
  356. case WM_HELP:
  357. WinHelp(((LPHELPINFO) lParam)-> hItemHandle, g_pHelpFileName,
  358. HELP_WM_HELP, (ULONG_PTR) s_RegDisconnectHelpIDs);
  359. break;
  360. case WM_CONTEXTMENU:
  361. WinHelp((HWND) wParam, g_pHelpFileName, HELP_CONTEXTMENU,
  362. (ULONG_PTR) s_RegDisconnectHelpIDs);
  363. break;
  364. default:
  365. return FALSE;
  366. }
  367. return TRUE;
  368. }
  369. /*******************************************************************************
  370. *
  371. * RegDisconnect_OnInitDialog
  372. *
  373. * DESCRIPTION:
  374. *
  375. * PARAMETERS:
  376. * hWnd,
  377. * hFocusWnd,
  378. * lParam,
  379. *
  380. *******************************************************************************/
  381. INT_PTR
  382. PASCAL
  383. RegDisconnect_OnInitDialog(
  384. HWND hWnd
  385. )
  386. {
  387. HWND hRemoteListWnd;
  388. RECT ClientRect;
  389. LV_COLUMN LVColumn;
  390. LV_ITEM LVItem;
  391. TCHAR RemoteName[MAX_PATH];
  392. HWND hKeyTreeWnd;
  393. TV_ITEM TVItem;
  394. hRemoteListWnd = GetDlgItem(hWnd, IDC_REMOTELIST);
  395. //
  396. // Initialize the ListView control.
  397. //
  398. ListView_SetImageList(hRemoteListWnd, g_RegEditData.hImageList,
  399. LVSIL_SMALL);
  400. LVColumn.mask = LVCF_FMT | LVCF_WIDTH;
  401. LVColumn.fmt = LVCFMT_LEFT;
  402. GetClientRect(hRemoteListWnd, &ClientRect);
  403. LVColumn.cx = ClientRect.right - GetSystemMetrics(SM_CXVSCROLL) -
  404. 2 * GetSystemMetrics(SM_CXEDGE);
  405. ListView_InsertColumn(hRemoteListWnd, 0, &LVColumn);
  406. //
  407. // Walk through each remote connection listed in the KeyTree and add it
  408. // to our RemoteList.
  409. //
  410. LVItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  411. LVItem.pszText = RemoteName;
  412. LVItem.iItem = 0;
  413. LVItem.iSubItem = 0;
  414. LVItem.iImage = IMAGEINDEX(IDI_REMOTE);
  415. hKeyTreeWnd = g_RegEditData.hKeyTreeWnd;
  416. TVItem.mask = TVIF_TEXT;
  417. TVItem.hItem = TreeView_GetNextSibling(hKeyTreeWnd,
  418. TreeView_GetRoot(hKeyTreeWnd));
  419. TVItem.pszText = RemoteName;
  420. TVItem.cchTextMax = ARRAYSIZE(RemoteName);
  421. do
  422. {
  423. LVItem.lParam = (LPARAM) TVItem.hItem;
  424. TreeView_GetItem(hKeyTreeWnd, &TVItem);
  425. ListView_InsertItem(hRemoteListWnd, &LVItem);
  426. LVItem.iItem++;
  427. }
  428. while ((TVItem.hItem = TreeView_GetNextSibling(hKeyTreeWnd, TVItem.hItem)) != NULL);
  429. ListView_SetItemState(hRemoteListWnd, 0, LVIS_FOCUSED, LVIS_FOCUSED);
  430. return TRUE;
  431. }
  432. /*******************************************************************************
  433. *
  434. * RegDisconnect_OnCommandOk
  435. *
  436. * DESCRIPTION:
  437. *
  438. * PARAMETERS:
  439. * hWnd,
  440. * hFocusWnd,
  441. * lParam,
  442. *
  443. *******************************************************************************/
  444. VOID
  445. PASCAL
  446. RegDisconnect_OnCommandOk(
  447. HWND hWnd
  448. )
  449. {
  450. LV_ITEM LVItem;
  451. HWND hRemoteListWnd;
  452. //
  453. // Walk through each selected item in the ListView and disconnect the
  454. // computer.
  455. //
  456. LVItem.mask = LVIF_PARAM;
  457. LVItem.iItem = -1;
  458. LVItem.iSubItem = 0;
  459. hRemoteListWnd = GetDlgItem(hWnd, IDC_REMOTELIST);
  460. while ((LVItem.iItem = ListView_GetNextItem(hRemoteListWnd, LVItem.iItem,
  461. LVNI_SELECTED)) != -1) {
  462. ListView_GetItem(hRemoteListWnd, &LVItem);
  463. RegEdit_OnKeyTreeDisconnect(hWnd, (HTREEITEM) LVItem.lParam);
  464. }
  465. }