Windows NT 4.0 source code leak
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.

272 lines
5.0 KiB

4 years ago
  1. /***
  2. *
  3. * Copyright (C) 1993-1994, Microsoft Corporation. All Rights Reserved.
  4. * Information Contained Herein Is Proprietary and Confidential.
  5. *
  6. * File:
  7. * main.cpp
  8. *
  9. * Purpose:
  10. * This file contains:
  11. *
  12. * WinMain() and WndProc(), which are called by Windows when the app is run.
  13. * Code to initialize and uninitialize OLE.
  14. * Code to create and destroy the window for the application.
  15. *
  16. *****************************************************************************/
  17. #include "common.h"
  18. #include "resource.h"
  19. #include "hello.h"
  20. #include <string.h>
  21. /* Global variables.
  22. */
  23. DWORD g_dwCHelloCF = 0; // Holds the return code for class factory creation.
  24. CHello FAR* g_phello = NULL; // Pointer to a CHello object.
  25. TCHAR g_szAppName[] = TSTR("Hello"); // Name of the application.
  26. /* Forward declarations.
  27. */
  28. HRESULT InitOle(void);
  29. void UninitOle(void);
  30. BOOL InitApplication(HINSTANCE);
  31. BOOL InitInstance(HINSTANCE, int);
  32. /* Let Windows call WinMain and WndProc.
  33. */
  34. extern "C"
  35. {
  36. long FAR PASCAL WndProc(HWND, UINT, WPARAM, LPARAM);
  37. int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  38. }
  39. /* WinMain
  40. *
  41. * Windows calls WinMain when the application starts.
  42. *
  43. */
  44. extern "C" int PASCAL
  45. WinMain(
  46. HINSTANCE hinst,
  47. HINSTANCE hPrevInstance,
  48. LPSTR lpCmdLine,
  49. int nCmdShow)
  50. {
  51. MSG msg;
  52. if(!hPrevInstance)
  53. if(!InitApplication(hinst))
  54. return FALSE;
  55. if(InitOle() != NOERROR)
  56. return FALSE;
  57. if(!InitInstance(hinst, nCmdShow)){
  58. UninitOle();
  59. return FALSE;
  60. }
  61. while(GetMessage(&msg, NULL, NULL, NULL)) {
  62. TranslateMessage(&msg);
  63. DispatchMessage(&msg);
  64. }
  65. // Uninitialize OLE.
  66. UninitOle();
  67. return msg.wParam;
  68. }
  69. /* InitApplication
  70. *
  71. * Create a window and register it with Windows.
  72. *
  73. * Return FALSE if an error occurs and TRUE otherwise.
  74. */
  75. BOOL
  76. InitApplication(HINSTANCE hinst)
  77. {
  78. WNDCLASS wc;
  79. wc.style = CS_HREDRAW | CS_VREDRAW;
  80. wc.lpfnWndProc = WndProc;
  81. wc.cbClsExtra = 0;
  82. wc.cbWndExtra = DLGWINDOWEXTRA;
  83. wc.hInstance = hinst;
  84. wc.hIcon = LoadIcon(hinst, g_szAppName); // Loads hello.ico
  85. wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Normal arrow pointer
  86. wc.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE+1);
  87. wc.lpszMenuName = NULL; // No menus.
  88. wc.lpszClassName= g_szAppName;
  89. // Tell Windows about the window class we just created.
  90. // Exit if an error occurs.
  91. if(!RegisterClass(&wc))
  92. return FALSE;
  93. return TRUE;
  94. }
  95. /* InitInstance
  96. *
  97. * Create an instance of CHello and make g_phello
  98. * point to it..
  99. *
  100. * Return FALSE if an error occurs and TRUE otherwise.
  101. */
  102. BOOL
  103. InitInstance(HINSTANCE hinst, int nCmdShow)
  104. {
  105. HWND hwnd;
  106. // Create the window and show it
  107. hwnd = CreateDialog(hinst, g_szAppName, 0, NULL);
  108. ShowWindow(hwnd, nCmdShow);
  109. g_phello->m_hwnd = hwnd;
  110. return TRUE;
  111. }
  112. extern "C" long FAR PASCAL
  113. WndProc(
  114. HWND hwnd,
  115. UINT message,
  116. WPARAM wParam,
  117. LPARAM lParam)
  118. {
  119. switch(message) {
  120. case WM_COMMAND:
  121. g_phello->ProcessCommand(wParam);
  122. return 0;
  123. case WM_DESTROY:
  124. PostQuitMessage(0);
  125. return 0;
  126. }
  127. return DefWindowProc(hwnd, message, wParam, lParam);
  128. }
  129. HRESULT
  130. InitOle()
  131. {
  132. HRESULT hresult;
  133. IClassFactory FAR* pcf;
  134. if((hresult = OleInitialize(NULL)) != NOERROR)
  135. return hresult;
  136. // create the single global instance of CHello
  137. if((g_phello = CHello::Create()) == NULL){
  138. hresult = ResultFromScode(E_OUTOFMEMORY);
  139. return hresult;
  140. }
  141. // Create an instance of the class factory for CHello.
  142. // Exit if an error occurs.
  143. pcf = CHelloCF::Create();
  144. if (pcf == NULL) {
  145. UninitOle();
  146. return hresult;
  147. }
  148. // Register the class factroy. Exit if an error occurs.
  149. hresult = CoRegisterClassObject(CLSID_CHello,
  150. pcf,
  151. CLSCTX_LOCAL_SERVER,
  152. REGCLS_MULTIPLEUSE,
  153. &g_dwCHelloCF);
  154. if (hresult != NOERROR) {
  155. pcf->Release();
  156. UninitOle();
  157. return hresult;
  158. }
  159. pcf->Release();
  160. // If execution has reached this spot, then no errors have occurred.
  161. return NOERROR;
  162. }
  163. /* UninitOLE
  164. *
  165. * Tell OLE that we are going away.
  166. *
  167. */
  168. void
  169. UninitOle()
  170. {
  171. // If a class factory was successfully created earlier then
  172. // tell OLE that the object is no longer available.
  173. if(g_dwCHelloCF != 0)
  174. CoRevokeClassObject(g_dwCHelloCF);
  175. // cause the remaining typeinfo to be released
  176. if (g_phello != NULL)
  177. g_phello->Release();
  178. // Tell OLE we are done using them.
  179. OleUninitialize();
  180. }
  181. #if defined(WIN32)
  182. extern "C" char FAR*
  183. ConvertStrWtoA(OLECHAR FAR* strIn, char FAR* buf, UINT size)
  184. {
  185. int badConversion = FALSE;
  186. WideCharToMultiByte(CP_ACP, NULL,
  187. strIn, -1,
  188. buf, size,
  189. NULL, &badConversion);
  190. return buf;
  191. }
  192. extern "C" char FAR*
  193. AnsiString(OLECHAR FAR* strIn)
  194. {
  195. static char buf[256];
  196. return (ConvertStrWtoA(strIn, buf, 256));
  197. }
  198. #endif