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.

518 lines
13 KiB

  1. #define WIN31
  2. #include <windows.h>
  3. #include "RegEdit.h"
  4. #include "SDKRegEd.h"
  5. /*********************************************************/
  6. /******************* Globals *****************************/
  7. /*********************************************************/
  8. char szAppName[] = "Registration Editor" ;
  9. char szSDKRegEd[] = "SDKRegEd";
  10. char *pszLongName;
  11. char *pszOutOfMemory;
  12. extern char szNull[];
  13. HANDLE hInstance;
  14. HWND hWndMain, hWndDlg = NULL, hWndHelp;
  15. LPSTR lpCmdLine;
  16. WORD wCmdFlags, wHelpMenuItem, wHelpId, wHelpMain;
  17. LONG (FAR PASCAL *lpfnEditor)(HWND, WORD, WORD, LONG);
  18. FARPROC lpOldHook;
  19. FARPROC lpMainWndDlg = NULL;
  20. WORD wHelpIndex;
  21. HANDLE hAcc;
  22. BOOL fOpenError = FALSE;
  23. /*********************************************************/
  24. /******************* Functions ***************************/
  25. /*********************************************************/
  26. unsigned long NEAR PASCAL RetSetValue(HKEY hKey, PSTR pSubKey, PSTR pVal)
  27. {
  28. return(RegSetValue(hKey, pSubKey, REG_SZ, pVal, 0L));
  29. }
  30. #ifndef REGLOAD
  31. unsigned long NEAR PASCAL WriteThroughSetValue(HKEY hKey, PSTR pSubKey,
  32. PSTR pVal)
  33. {
  34. long lRet;
  35. if (!(lRet=RegSetValue(hKey, pSubKey, REG_SZ, pVal, 0L)))
  36. lRet = SDKSetValue(hKey, pSubKey, pVal);
  37. return(lRet);
  38. }
  39. #endif
  40. unsigned long (NEAR PASCAL *lpfnSetValue)(HKEY, PSTR, PSTR) = RetSetValue;
  41. unsigned long NEAR PASCAL MySetValue(HKEY hKey, PSTR pSubKey, PSTR pVal)
  42. {
  43. return((*lpfnSetValue)(hKey, pSubKey, pVal));
  44. }
  45. VOID NEAR PASCAL GetCommandFlags(void)
  46. {
  47. wCmdFlags = 0;
  48. while(1) {
  49. /* skip spaces */
  50. while(*lpCmdLine == ' ')
  51. ++lpCmdLine;
  52. /* check if this is a command line switch */
  53. if(*lpCmdLine!='/' && *lpCmdLine!='-')
  54. break;
  55. ++lpCmdLine;
  56. /* set known flags */
  57. while(*lpCmdLine && *lpCmdLine!=' ') {
  58. switch(*lpCmdLine) {
  59. case('s'): /* for silent */
  60. case('S'): /* for silent */
  61. wCmdFlags |= FLAG_SILENT;
  62. break;
  63. case('v'): /* use tree editor */
  64. case('V'): /* use tree editor */
  65. wCmdFlags |= FLAG_VERBOSE;
  66. break;
  67. case('u'): /* update, don't overwrite existing path entries */
  68. case('U'): /* in shell\open\command or shell\open\print */
  69. wCmdFlags |= FLAG_LEAVECOMMAND;
  70. break;
  71. }
  72. lpCmdLine = AnsiNext(lpCmdLine);
  73. }
  74. }
  75. }
  76. #ifndef REGLOAD
  77. long FAR PASCAL WndProc(HWND hWnd, WORD wMessage, WORD wParam, LONG lParam)
  78. {
  79. static HANDLE hCustRegs = NULL;
  80. HCURSOR oldCursor;
  81. switch(wMessage) {
  82. case WM_ACTIVATE:
  83. if(wParam) {
  84. hWndHelp = hWnd;
  85. SetFocus(hWndDlg);
  86. }
  87. break;
  88. case WM_CREATE:
  89. {
  90. RECT rcWnd, rcClt;
  91. DragAcceptFiles(hWnd, TRUE);
  92. lpfnEditor(hWnd, wMessage, wParam, lParam);
  93. if((rcWnd.right=MyGetProfileInt(IDS_SHORTNAME, IDS_WIDTH, 0))<=0 ||
  94. (rcWnd.bottom=MyGetProfileInt(IDS_SHORTNAME, IDS_HEIGHT, 0))<=0) {
  95. GetWindowRect(hWndDlg, &rcWnd);
  96. rcWnd.right -= rcWnd.left;
  97. rcWnd.bottom -= rcWnd.top;
  98. }
  99. GetClientRect(hWnd, &rcClt);
  100. rcClt.right -= rcWnd.right;
  101. rcClt.bottom -= rcWnd.bottom;
  102. GetWindowRect(hWnd, &rcWnd);
  103. SetWindowPos(hWnd, NULL, 0, 0, rcWnd.right-rcWnd.left-rcClt.right,
  104. rcWnd.bottom-rcWnd.top-rcClt.bottom, SWP_NOMOVE | SWP_NOZORDER);
  105. return(DefWindowProc(hWnd, wMessage, wParam, lParam));
  106. }
  107. case WM_CLOSE:
  108. case WM_QUERYENDSESSION:
  109. {
  110. HKEY hKeyRoot;
  111. WORD wErrMsg;
  112. int nReturn;
  113. if(!fOpenError) {
  114. if(!lpfnEditor(hWnd, wMessage, wParam, lParam))
  115. return(0L);
  116. if(RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKeyRoot) == ERROR_SUCCESS) {
  117. if(wErrMsg=GetErrMsg((WORD)RegCloseKey(hKeyRoot))) {
  118. PSTR pError;
  119. if(pError=(PSTR)MyLoadString(wErrMsg, NULL, LMEM_FIXED)) {
  120. nReturn = MyMessageBox(hWnd, IDS_ENDERROR,
  121. MB_YESNO|MB_ICONHAND|MB_SYSTEMMODAL, lstrlen(pError),
  122. (LPSTR)pError);
  123. LocalFree((HANDLE)pError);
  124. } else
  125. /* Notice the flags are ignored for OOM
  126. */
  127. nReturn = MyMessageBox(hWnd, IDS_OUTOFMEMORY,
  128. MB_OK|MB_ICONHAND|MB_SYSTEMMODAL, 0);
  129. if(nReturn != IDYES)
  130. return(0L);
  131. }
  132. }
  133. }
  134. return(DefWindowProc(hWnd, wMessage, wParam, lParam));
  135. }
  136. case WM_DESTROY:
  137. if(hWndDlg)
  138. DestroyWindow(hWndDlg);
  139. if(lpMainWndDlg)
  140. FreeProcInstance(lpMainWndDlg);
  141. #ifndef NOHELP
  142. WinHelp(hWnd, NULL, HELP_QUIT, 0L);
  143. #endif
  144. DragAcceptFiles(hWnd, FALSE);
  145. PostQuitMessage(0);
  146. break;
  147. case WM_SIZE:
  148. {
  149. RECT rcDlg, rcWnd;
  150. int hgt;
  151. if(wParam == SIZEICONIC)
  152. break;
  153. if(!hWndDlg) {
  154. /* This should only happen during the create message
  155. */
  156. PostMessage(hWnd, wMessage, wParam, lParam);
  157. break;
  158. }
  159. if(wParam == SIZENORMAL) {
  160. WriteProfileInt(IDS_SHORTNAME, IDS_WIDTH, LOWORD(lParam));
  161. WriteProfileInt(IDS_SHORTNAME, IDS_HEIGHT, HIWORD(lParam));
  162. }
  163. hgt = HIWORD(lParam);
  164. SetWindowPos(hWndDlg, NULL, 0, 0, LOWORD(lParam), hgt, SWP_NOZORDER);
  165. GetWindowRect(hWndDlg, &rcDlg);
  166. ScreenToClient(hWnd, (POINT *)(&rcDlg) + 1);
  167. if((WORD)rcDlg.bottom != HIWORD(lParam)) {
  168. GetWindowRect(hWnd, &rcWnd);
  169. SetWindowPos(hWnd, NULL, 0, 0, rcWnd.right-rcWnd.left,
  170. rcWnd.bottom-rcWnd.top-HIWORD(lParam)+rcDlg.bottom,
  171. SWP_NOMOVE | SWP_NOZORDER);
  172. }
  173. break;
  174. }
  175. case WM_DROPFILES:
  176. {
  177. int result, i, nFileName;
  178. HANDLE hFileName;
  179. WORD wFlags;
  180. wFlags = (GetKeyState(VK_CONTROL)&0x8000) ? FLAG_SILENT : 0;
  181. lpfnEditor(hWnd, wMessage, wParam, lParam);
  182. for(result=DragQueryFile(wParam, (UINT)-1, NULL, 0), i=0; i<result; ++i) {
  183. if(!(hFileName=GlobalAlloc(GMEM_MOVEABLE,
  184. (DWORD)(nFileName=DragQueryFile(wParam, i, NULL, 0)+1))))
  185. continue;
  186. DragQueryFile(wParam, i, GlobalLock(hFileName), nFileName);
  187. GlobalUnlock(hFileName);
  188. SendMessage(hWnd, WM_COMMAND, ID_FINISHMERGE,
  189. MAKELONG(hFileName,wFlags));
  190. }
  191. DragFinish(wParam);
  192. return(TRUE);
  193. }
  194. case WM_COMMAND:
  195. oldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  196. switch(wParam) {
  197. case ID_HELP:
  198. if(HIWORD(lParam)==MSGF_MENU && IsWindowEnabled(hWnd)
  199. && wHelpMenuItem) {
  200. WORD m = wHelpMenuItem;
  201. /* Get outta menu mode if help for a menu item */
  202. SendMessage(hWnd, WM_CANCELMODE, 0, 0L);
  203. MyHelp(hWnd, HELP_CONTEXT, m);
  204. }
  205. break;
  206. case ID_FINISHMERGE:
  207. {
  208. unsigned long (NEAR PASCAL *lpfnSave)(HKEY, PSTR, PSTR);
  209. /* If there was a file selected then merge it; otherwise,
  210. * just let lpfnEditor do its cleanup (at the end of the switch)
  211. */
  212. if (LOWORD(lParam)) {
  213. lpfnSave = lpfnSetValue;
  214. if (HIWORD(lParam)&FLAG_WRITETHROUGH && lpfnSetValue==SDKSetValue)
  215. lpfnSetValue = WriteThroughSetValue;
  216. lpfnEditor(hWnd, WM_COMMAND, ID_MERGEFILE, 0L);
  217. ProcessFiles(GetLastActivePopup(hWnd), LOWORD(lParam),
  218. HIWORD(lParam));
  219. lpfnSetValue = lpfnSave;
  220. }
  221. break;
  222. }
  223. case ID_MERGEFILE:
  224. {
  225. HANDLE hPath;
  226. lpfnEditor(hWnd, wMessage, wParam, lParam);
  227. wHelpId = IDW_OPENREG;
  228. if(!DoFileOpenDlg(hWnd, IDS_MERGETITLE, IDS_REGS, IDS_CUSTREGS,
  229. &hCustRegs, &hPath, TRUE))
  230. hPath = NULL;
  231. SendMessage(hWnd, WM_COMMAND, ID_FINISHMERGE, MAKELONG(hPath, 0));
  232. return(TRUE);
  233. }
  234. case ID_WRITEFILE:
  235. {
  236. HANDLE hPath;
  237. wHelpId = IDW_SAVEREG;
  238. if(DoFileOpenDlg(hWnd, IDS_WRITETITLE, IDS_REGS, IDS_CUSTREGS,
  239. &hCustRegs, &hPath, FALSE))
  240. lParam = (LONG)hPath;
  241. else
  242. lParam = NULL;
  243. break;
  244. }
  245. case ID_EXIT:
  246. PostMessage(hWnd, WM_CLOSE, 0, 0L);
  247. break;
  248. case ID_HELPINDEX:
  249. MyHelp(hWnd, HELP_CONTEXT, wHelpIndex);
  250. break;
  251. case ID_HELPSEARCH:
  252. MyHelp(hWnd, HELP_PARTIALKEY, (DWORD)(LPSTR)"");
  253. break;
  254. case ID_HELPUSINGHELP:
  255. MyHelp(hWnd, HELP_HELPONHELP, 0);
  256. break;
  257. case ID_ABOUT:
  258. {
  259. HANDLE hShortName, hDesc;
  260. if(!(hShortName=MyLoadString(IDS_MEDIUMNAME, NULL, LMEM_MOVEABLE)))
  261. goto Error3_1;
  262. if(!(hDesc=MyLoadString(IDS_DESCRIPTION, NULL, LMEM_MOVEABLE)))
  263. goto Error3_2;
  264. ShellAbout(hWnd, LocalLock(hShortName), LocalLock(hDesc),
  265. LoadIcon(hInstance, MAKEINTRESOURCE(MAINICON)));
  266. LocalUnlock(hDesc);
  267. LocalUnlock(hShortName);
  268. LocalFree(hDesc);
  269. Error3_2:
  270. LocalFree(hShortName);
  271. Error3_1:
  272. break;
  273. }
  274. default:
  275. break;
  276. }
  277. SetCursor(oldCursor);
  278. break;
  279. case WM_MENUSELECT:
  280. if(LOWORD(lParam)&MF_POPUP)
  281. wHelpMenuItem = 0;
  282. else if(LOWORD(lParam)&MF_SYSMENU)
  283. wHelpMenuItem = IDH_SYSMENU;
  284. else
  285. wHelpMenuItem = wHelpMain+wParam;
  286. break;
  287. default:
  288. break;
  289. }
  290. return(lpfnEditor(hWnd, wMessage, wParam, lParam));
  291. }
  292. BOOL NEAR PASCAL CreateMainWindow(void)
  293. {
  294. WNDCLASS wndclass;
  295. wndclass.style = 0;
  296. wndclass.lpfnWndProc = (WNDPROC)WndProc;
  297. wndclass.cbClsExtra = 0;
  298. wndclass.cbWndExtra = 0;
  299. wndclass.hInstance = hInstance;
  300. wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(MAINICON));
  301. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  302. wndclass.hbrBackground = COLOR_APPWORKSPACE + 1;
  303. wndclass.lpszClassName = szAppName;
  304. if(wCmdFlags&FLAG_VERBOSE) {
  305. wndclass.lpszMenuName = MAKEINTRESOURCE(SDKMAINMENU);
  306. lpfnEditor = SDKMainWnd;
  307. lpfnSetValue = SDKSetValue;
  308. wHelpMain = IDW_SDKMAIN;
  309. wHelpIndex = IDW_SDKMAIN + 0x0100 + ID_HELPINDEX;
  310. } else {
  311. wndclass.lpszMenuName = MAKEINTRESOURCE(MAINMENU);
  312. lpfnEditor = MainWnd;
  313. wHelpMain = IDW_MAIN;
  314. wHelpIndex = IDW_MAIN + 0x0100 + ID_HELPINDEX;
  315. }
  316. return(RegisterClass(&wndclass)
  317. && (hWndMain=CreateWindow(szAppName, pszLongName,
  318. WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  319. CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)));
  320. }
  321. #endif
  322. int PASCAL WinMain (HANDLE hInst, HANDLE hPrevInstance,
  323. LPSTR lpszCmdLine, int nCmdShow)
  324. {
  325. HANDLE hTemp, hCmd = NULL;
  326. #ifndef REGLOAD
  327. FARPROC lpMessageFilter;
  328. MSG msg;
  329. VOID (FAR PASCAL *lpfnRegisterPenApp)(WORD, BOOL) = NULL;
  330. #endif
  331. hInstance = hInst;
  332. lpCmdLine = lpszCmdLine;
  333. GetCommandFlags();
  334. if(!(hTemp=MyLoadString(IDS_LONGNAME, NULL, LMEM_FIXED)))
  335. return(FALSE);
  336. pszLongName = LocalLock(hTemp);
  337. if(!(hTemp=MyLoadString(IDS_OUTOFMEMORY, NULL, LMEM_FIXED)))
  338. return(FALSE);
  339. pszOutOfMemory = LocalLock(hTemp);
  340. if(*lpCmdLine && !(hCmd=StringToHandle(lpCmdLine)))
  341. return(FALSE);
  342. #ifdef REGLOAD
  343. if ((hTemp=GetModuleHandle("REGEDIT")) > 1) {
  344. PSTR pLocal;
  345. WORD wFileLen = 130;
  346. WORD wCmdLen = lstrlen(lpCmdLine) + 1;
  347. while(1) {
  348. if(!(pLocal=(PSTR)LocalAlloc(LMEM_FIXED, wFileLen+wCmdLen))) {
  349. MyMessageBox(NULL, IDS_OUTOFMEMORY,
  350. MB_OK|MB_ICONHAND|MB_SYSTEMMODAL, 0);
  351. return(FALSE);
  352. }
  353. if(GetModuleFileName(hTemp, pLocal, wFileLen) < (int)wFileLen-5)
  354. break;
  355. LocalFree((HANDLE)pLocal);
  356. wFileLen += 130;
  357. }
  358. lstrcat(pLocal, " ");
  359. lstrcat(pLocal, lpCmdLine);
  360. return(WinExec(pLocal, SW_SHOWNORMAL) > 32);
  361. }
  362. #else
  363. if(hPrevInstance) {
  364. GetInstanceData(hPrevInstance, (PSTR)&hWndMain, sizeof(hWndMain));
  365. if(hCmd)
  366. PostMessage(hWndMain, WM_COMMAND, ID_FINISHMERGE,
  367. MAKELONG(hCmd, wCmdFlags | FLAG_WRITETHROUGH));
  368. else {
  369. SetWindowPos(hWndMain, NULL, 0, 0, 0, 0,
  370. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  371. if(IsIconic(hWndMain))
  372. ShowWindow(hWndMain, SW_RESTORE);
  373. else
  374. SetActiveWindow(GetLastActivePopup(hWndMain));
  375. }
  376. return(TRUE);
  377. }
  378. #endif
  379. if(hCmd) {
  380. ProcessFiles(NULL, hCmd, wCmdFlags);
  381. return(TRUE);
  382. }
  383. #ifdef REGLOAD
  384. return(TRUE);
  385. #else
  386. if(!CreateMainWindow())
  387. return(FALSE);
  388. if(fOpenError)
  389. PostMessage(hWndMain, WM_COMMAND, ID_EXIT, 0L);
  390. else {
  391. ShowWindow(hWndMain, nCmdShow);
  392. UpdateWindow(hWndMain);
  393. }
  394. if(lpMessageFilter=MakeProcInstance(MessageFilter, hInstance))
  395. lpOldHook = SetWindowsHook(WH_MSGFILTER, lpMessageFilter);
  396. hAcc = LoadAccelerators(hInstance, wCmdFlags&FLAG_VERBOSE ?
  397. szSDKRegEd : "RegEdit");
  398. if (lpfnRegisterPenApp = (VOID (FAR PASCAL *)(WORD, BOOL))GetProcAddress(GetSystemMetrics(SM_PENWINDOWS),
  399. "RegisterPenApp"))
  400. (*lpfnRegisterPenApp)(1, TRUE);
  401. while(GetMessage(&msg, NULL, 0, 0)) {
  402. if(!hAcc || !TranslateAccelerator(hWndMain, hAcc, &msg)) {
  403. if(!hWndDlg || !IsDialogMessage(hWndDlg, &msg)) {
  404. TranslateMessage(&msg);
  405. DispatchMessage(&msg);
  406. }
  407. }
  408. }
  409. if (lpfnRegisterPenApp)
  410. (*lpfnRegisterPenApp)(1, FALSE);
  411. if(lpMessageFilter) {
  412. UnhookWindowsHook(WH_MSGFILTER, lpMessageFilter);
  413. FreeProcInstance(lpMessageFilter);
  414. }
  415. return(msg.wParam);
  416. #endif
  417. }