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.

502 lines
13 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. Main.cpp
  5. Abstract:
  6. Implements the startup code and message pump for the main application.
  7. Notes:
  8. ANSI only - must run on Win9x.
  9. History:
  10. 01/30/01 rparsons Created
  11. 01/10/02 rparsons Revised
  12. 02/20/02 rparsons Only corrupt the heap if the user specifically wants
  13. us to.
  14. --*/
  15. #include "demoapp.h"
  16. extern APPINFO g_ai;
  17. extern LPFNDEMOAPPEXP DemoAppExpFunc;
  18. /*++
  19. Routine Description:
  20. Sets up the window class struct for the main app.
  21. Arguments:
  22. hInstance - App instance handle.
  23. Return Value:
  24. TRUE on success, FALSE otherwise.
  25. --*/
  26. BOOL
  27. InitMainApplication(
  28. IN HINSTANCE hInstance
  29. )
  30. {
  31. WNDCLASS wc;
  32. wc.style = CS_HREDRAW | CS_VREDRAW;
  33. wc.lpfnWndProc = MainWndProc;
  34. wc.cbClsExtra = 0;
  35. wc.cbWndExtra = 0;
  36. wc.hInstance = hInstance;
  37. wc.hIcon = (HICON)LoadImage(hInstance,
  38. MAKEINTRESOURCE(IDI_APPICON),
  39. IMAGE_ICON,
  40. 16,
  41. 16,
  42. 0);
  43. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  44. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  45. wc.lpszMenuName = MAKEINTRESOURCE(IDM_MAIN_MENU);
  46. wc.lpszClassName = MAIN_APP_CLASS;
  47. return RegisterClass(&wc);
  48. }
  49. /*++
  50. Routine Description:
  51. Creates the main window.
  52. Arguments:
  53. hInstance - App instance handle.
  54. nCmdShow - Window show flag.
  55. Return Value:
  56. TRUE on success, FALSE otherwise.
  57. --*/
  58. BOOL
  59. InitMainInstance(
  60. IN HINSTANCE hInstance,
  61. IN int nCmdShow
  62. )
  63. {
  64. HWND hWnd;
  65. //
  66. // Create the main window.
  67. //
  68. hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
  69. MAIN_APP_CLASS,
  70. MAIN_APP_TITLE,
  71. WS_BORDER | WS_OVERLAPPEDWINDOW |
  72. WS_THICKFRAME,
  73. CW_USEDEFAULT,
  74. CW_USEDEFAULT,
  75. CW_USEDEFAULT,
  76. CW_USEDEFAULT,
  77. NULL,
  78. NULL,
  79. hInstance,
  80. NULL);
  81. if (!hWnd) {
  82. return FALSE;
  83. }
  84. return TRUE;
  85. }
  86. /*++
  87. Routine Description:
  88. Runs the message loop for the main app.
  89. Arguments:
  90. hWnd - Main window handle.
  91. uMsg - Windows message.
  92. wParam - Additional message info.
  93. lParam - Additional message info.
  94. Return Value:
  95. TRUE if the message was processed, FALSE otherwise.
  96. --*/
  97. LRESULT
  98. CALLBACK
  99. MainWndProc(
  100. IN HWND hWnd,
  101. IN UINT uMsg,
  102. IN WPARAM wParam,
  103. IN LPARAM lParam
  104. )
  105. {
  106. switch (uMsg) {
  107. case WM_CREATE:
  108. {
  109. char szError[MAX_PATH];
  110. POINT pt;
  111. RECT rc;
  112. g_ai.hWndMain = hWnd;
  113. //
  114. // Load the library to be used later.
  115. //
  116. if (g_ai.fEnableBadFunc) {
  117. BadLoadLibrary();
  118. }
  119. //
  120. // See if extended behavior should be enabled.
  121. //
  122. if (g_ai.fExtended && g_ai.fRunApp) {
  123. AddExtendedItems(hWnd);
  124. }
  125. //
  126. // See if internal behavior should be enabled.
  127. //
  128. if (g_ai.fInternal && g_ai.fRunApp) {
  129. AddInternalItems(hWnd);
  130. }
  131. //
  132. // Create the edit box.
  133. //
  134. GetClientRect(hWnd, &rc);
  135. g_ai.hWndEdit = CreateWindowEx(0,
  136. "EDIT",
  137. NULL,
  138. WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  139. ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL |
  140. ES_READONLY,
  141. 0,
  142. rc.bottom,
  143. rc.right,
  144. rc.bottom,
  145. hWnd,
  146. (HMENU)IDC_EDIT,
  147. g_ai.hInstance,
  148. NULL);
  149. if (!g_ai.hWndEdit) {
  150. return FALSE;
  151. }
  152. //
  153. // Load the edit box with the contents of our text file.
  154. //
  155. LoadFileIntoEditBox();
  156. //
  157. // Attempt to get previous window settings from the registry.
  158. //
  159. BadSaveToRegistry(FALSE, &pt);
  160. if (pt.x != 0) {
  161. SetWindowPos(hWnd,
  162. HWND_NOTOPMOST,
  163. pt.x,
  164. pt.y,
  165. 0,
  166. 0,
  167. SWP_NOSIZE);
  168. } else {
  169. CenterWindow(hWnd);
  170. }
  171. //
  172. // Display our disclaimer.
  173. //
  174. LoadString(g_ai.hInstance, IDS_DEMO_ONLY, szError, sizeof(szError));
  175. MessageBox(hWnd, szError, MAIN_APP_TITLE, MB_OK | MB_TOPMOST | MB_ICONEXCLAMATION);
  176. //
  177. // Attempt to create our 'temporary file' in the Windows directory.
  178. // This will fail in a limited-user context.
  179. // Note that the file gets deleted immediately after it is created.
  180. //
  181. if (g_ai.fEnableBadFunc) {
  182. if (!BadCreateTempFile()) {
  183. LoadString(g_ai.hInstance, IDS_LUA_SAVE_FAILED, szError, sizeof(szError));
  184. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  185. }
  186. }
  187. ShowWindow(hWnd, SW_SHOWNORMAL);
  188. break;
  189. }
  190. case WM_DESTROY:
  191. PostQuitMessage(0);
  192. break;
  193. case WM_CLOSE:
  194. {
  195. char szError[MAX_PATH];
  196. DWORD dwParam = 0;
  197. RECT rc;
  198. if (g_ai.fEnableBadFunc) {
  199. //
  200. // Attempt to delete our keys from the registry.
  201. //
  202. if (!BadDeleteRegistryKey()) {
  203. LoadString(g_ai.hInstance, IDS_REG_DELETE, szError, sizeof(szError));
  204. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  205. }
  206. //
  207. // Attempt to save our position information to the registry.
  208. //
  209. GetWindowRect(hWnd, &rc);
  210. if (!BadSaveToRegistry(TRUE, (LPPOINT)&rc)) {
  211. LoadString(g_ai.hInstance, IDS_REG_SAVE, szError, sizeof(szError));
  212. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  213. }
  214. //
  215. // Attempt to call the function that we got a pointer to earlier
  216. // but has since been freed. This should cause an access violation.
  217. //
  218. DemoAppExpFunc(&dwParam);
  219. }
  220. PostQuitMessage(0);
  221. break;
  222. }
  223. case WM_SIZE:
  224. MoveWindow(g_ai.hWndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  225. break;
  226. case WM_SETFOCUS:
  227. if (!IsIconic(hWnd)) {
  228. SetFocus(g_ai.hWndEdit);
  229. }
  230. break;
  231. case WM_QUERYENDSESSION:
  232. return TRUE;
  233. case WM_KILLFOCUS:
  234. SendMessage(g_ai.hWndEdit, uMsg, wParam, lParam);
  235. break;
  236. case WM_ACTIVATE:
  237. if ((LOWORD(wParam) == WA_ACTIVE ||
  238. LOWORD(wParam) == WA_CLICKACTIVE) &&
  239. (!IsIconic(hWnd))) {
  240. if (GetForegroundWindow() == hWnd) {
  241. SetFocus(GetForegroundWindow());
  242. }
  243. }
  244. break;
  245. case WM_COMMAND:
  246. switch (LOWORD(wParam)) {
  247. case IDM_EDIT_CUT:
  248. SendMessage(g_ai.hWndEdit, WM_CUT, 0, 0);
  249. break;
  250. case IDM_EDIT_COPY:
  251. SendMessage(g_ai.hWndEdit, WM_COPY, 0, 0);
  252. break;
  253. case IDM_EDIT_PASTE:
  254. SendMessage(g_ai.hWndEdit, WM_PASTE, 0, 0);
  255. break;
  256. case IDM_EDIT_DELETE:
  257. SendMessage(g_ai.hWndEdit, WM_CLEAR, 0, 0);
  258. break;
  259. case IDM_EDIT_UNDO:
  260. {
  261. //
  262. // Send WM_UNDO only if there is something to be undone.
  263. //
  264. if (SendMessage(g_ai.hWndEdit, EM_CANUNDO, 0, 0)) {
  265. SendMessage(g_ai.hWndEdit, WM_UNDO, 0, 0);
  266. } else {
  267. char szError[MAX_PATH];
  268. LoadString(g_ai.hInstance, IDS_CANT_UNDO, szError, sizeof(szError));
  269. MessageBox(hWnd, szError, MAIN_APP_TITLE, MB_ICONINFORMATION);
  270. }
  271. break;
  272. }
  273. case IDM_FILE_EXIT:
  274. PostMessage(hWnd, WM_CLOSE, 0, 0);
  275. break;
  276. case IDM_HELP_ABOUT:
  277. ShellAbout(hWnd,
  278. MAIN_APP_TITLE,
  279. NULL,
  280. NULL);
  281. break;
  282. case IDM_FILE_PRINT:
  283. {
  284. char szText[MAX_PATH];
  285. LoadString(g_ai.hInstance, IDS_THANKS, szText, sizeof(szText));
  286. PrintDemoText(hWnd, szText);
  287. break;
  288. }
  289. case IDM_FILE_SAVEAS:
  290. ShowSaveDialog();
  291. break;
  292. case IDM_FILE_SAVE:
  293. {
  294. if (g_ai.fEnableBadFunc) {
  295. //
  296. // Attempt to save a bogus temp file, but do it wrong.
  297. //
  298. if (!BadWriteToFile()) {
  299. char szError[MAX_PATH];
  300. LoadString(g_ai.hInstance, IDS_SAVE_FAILED, szError, sizeof(szError));
  301. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  302. }
  303. }
  304. break;
  305. }
  306. case IDM_HELP_TOPICS:
  307. //
  308. // Launch the help file with a bad path to winhelp.
  309. //
  310. BadLaunchHelpFile(g_ai.fEnableBadFunc ? FALSE : TRUE);
  311. break;
  312. case IDM_FORMAT_FONT:
  313. {
  314. char szError[MAX_PATH];
  315. if (g_ai.fEnableBadFunc) {
  316. if (g_ai.fInsecure) {
  317. //
  318. // Do some bad things and corrupt the heap.
  319. //
  320. BadCorruptHeap();
  321. } else {
  322. LoadString(g_ai.hInstance, IDS_NOT_INSECURE, szError, sizeof(szError));
  323. MessageBox(hWnd, szError, MAIN_APP_TITLE, MB_ICONEXCLAMATION);
  324. }
  325. }
  326. //
  327. // Display a font dialog for fun.
  328. //
  329. DisplayFontDlg(hWnd);
  330. break;
  331. }
  332. case IDM_ACCESS_VIOLATION:
  333. AccessViolation();
  334. break;
  335. case IDM_EXCEED_BOUNDS:
  336. ExceedArrayBounds();
  337. break;
  338. case IDM_FREE_MEM_TWICE:
  339. FreeMemoryTwice();
  340. break;
  341. case IDM_FREE_INVALID_MEM:
  342. FreeInvalidMemory();
  343. break;
  344. case IDM_PRIV_INSTRUCTION:
  345. PrivilegedInstruction();
  346. break;
  347. case IDM_HEAP_CORRUPTION:
  348. HeapCorruption();
  349. break;
  350. case IDM_PROPAGATION_TEST:
  351. {
  352. char szOutputFile[MAX_PATH];
  353. char szMessage[MAX_PATH];
  354. char szTemp[MAX_PATH];
  355. ExtractExeFromLibrary(sizeof(szOutputFile), szOutputFile);
  356. if (*szOutputFile) {
  357. LoadString(g_ai.hInstance, IDS_EXTRACTION, szTemp, sizeof(szTemp));
  358. StringCchPrintf(szMessage,
  359. sizeof(szMessage),
  360. szTemp,
  361. szOutputFile);
  362. MessageBox(hWnd, szMessage, MAIN_APP_TITLE, MB_ICONINFORMATION | MB_OK);
  363. BadCreateProcess(szOutputFile, szOutputFile, TRUE);
  364. }
  365. break;
  366. }
  367. default:
  368. break;
  369. }
  370. default:
  371. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  372. }
  373. return FALSE;
  374. }