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.

198 lines
5.5 KiB

  1. /*
  2. * i n i t . c p p
  3. *
  4. * Purpose:
  5. *
  6. * History
  7. *
  8. * Copyright (C) Microsoft Corp. 1995, 1996.
  9. */
  10. #include <pch.hxx>
  11. #include "dllmain.h"
  12. #include "msoert.h"
  13. #include "mimeole.h"
  14. #include "envhost.h"
  15. #include "init.h"
  16. LRESULT CALLBACK InitWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
  17. HRESULT HrCreateNoteThisThread(WPARAM wp, LPARAM lp);
  18. DWORD MainThreadProc(LPVOID lpvUnused);
  19. void NoteMsgPump();
  20. CGWNote *g_pActiveNote=0;
  21. HWND g_hwndInit = NULL;
  22. HEVENT g_hEventSpoolerInit =NULL;
  23. DWORD g_dwNoteThreadID=0;
  24. BOOL g_fInitialized=FALSE;
  25. static HTHREAD s_hMainThread = NULL;
  26. static HEVENT s_hInitEvent = NULL;
  27. static DWORD s_dwMainThreadId = 0;
  28. static TCHAR s_szInitWndClass[] = "GWInitWindow";
  29. void InitGWNoteThread(BOOL fInit)
  30. {
  31. if (fInit)
  32. {
  33. // create an event for the main thread to signal
  34. if (s_hInitEvent = CreateEvent(NULL, FALSE, FALSE, NULL))
  35. {
  36. if (s_hMainThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MainThreadProc, NULL, 0, &s_dwMainThreadId))
  37. {
  38. HANDLE rghWait[]={s_hMainThread, s_hInitEvent};
  39. // wait for the main thread to signal that initialization is complete
  40. WaitForMultipleObjects(sizeof(rghWait)/sizeof(HANDLE), rghWait, FALSE, INFINITE);
  41. }
  42. CloseHandle(s_hInitEvent);
  43. s_hInitEvent = NULL;
  44. }
  45. }
  46. else
  47. {
  48. // tell the main thread to deinitialize everything
  49. // the SendMessage() will block the calling thread until deinit is complete,
  50. if (g_hwndInit)
  51. SendMessage(g_hwndInit, ITM_SHUTDOWNTHREAD, 0, 0);
  52. // wait for main thread to terminate (when it exits its message loop)
  53. // this isn't strictly necessary, but it helps to ensure proper cleanup.
  54. WaitForSingleObject(s_hMainThread, INFINITE);
  55. CloseHandle(s_hMainThread);
  56. s_hMainThread = NULL;
  57. }
  58. }
  59. DWORD MainThreadProc(LPVOID lpvUnused)
  60. {
  61. DWORD dw;
  62. HRESULT hr;
  63. RECT rc={0};
  64. WNDCLASS wc = { 0, // style
  65. InitWndProc, // lpfnWndProc
  66. 0, // cbClsExtra
  67. 0, // cbWndExtra
  68. g_hInst, // hInstance
  69. NULL, // hIcon
  70. NULL, // hCursor
  71. NULL, // hbrBackground
  72. NULL, // lpszMenuName
  73. s_szInitWndClass }; // lpszClassName
  74. g_dwNoteThreadID = GetCurrentThreadId();
  75. if (!RegisterClass(&wc))
  76. return 0;
  77. g_hwndInit = CreateWindowEx(NULL,
  78. s_szInitWndClass,
  79. s_szInitWndClass,
  80. WS_POPUP,
  81. 0,0,0,0,
  82. NULL,
  83. NULL,
  84. g_hInst,
  85. NULL);
  86. if (!g_hwndInit)
  87. return 0;
  88. OleInitialize(0);
  89. g_fInitialized=TRUE;
  90. SetEvent(s_hInitEvent);
  91. NoteMsgPump();
  92. return 0;
  93. }
  94. LRESULT CALLBACK InitWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
  95. {
  96. BOOL fRet;
  97. HRESULT hr;
  98. switch(msg)
  99. {
  100. case ITM_SHUTDOWNTHREAD:
  101. Assert(GetCurrentThreadId()==s_dwMainThreadId);
  102. OleUninitialize();
  103. DestroyWindow(hwnd);
  104. PostThreadMessage(s_dwMainThreadId, WM_QUIT, 0, 0);
  105. g_fInitialized=FALSE;
  106. return 0;
  107. case ITM_CREATENOTEONTHREAD:
  108. return (LONG)HrCreateNoteThisThread(wp, lp);
  109. }
  110. return DefWindowProc(hwnd, msg, wp, lp);
  111. }
  112. void NoteMsgPump()
  113. {
  114. MSG msg;
  115. while (GetMessage(&msg, NULL, 0, 0))
  116. {
  117. if (msg.hwnd != g_hwndInit && // ignore init window msgs
  118. IsWindow(msg.hwnd)) // ignore per-task msgs where hwnd=0
  119. {
  120. if(g_pActiveNote && // if a note has focus, call it's XLateAccelerator...
  121. g_pActiveNote->TranslateAcclerator(&msg)==S_OK)
  122. continue;
  123. }
  124. TranslateMessage(&msg);
  125. DispatchMessage(&msg);
  126. }
  127. }
  128. HRESULT HrCreateNote(REFCLSID clsidEnvelope, DWORD dwFlags)
  129. {
  130. LPOLESTR pstr;
  131. HRESULT hr;
  132. if (FAILED(StringFromCLSID(clsidEnvelope, &pstr)))
  133. return E_FAIL;
  134. // switch thread
  135. hr = SendMessage (g_hwndInit, ITM_CREATENOTEONTHREAD, (WPARAM)pstr, (LPARAM)dwFlags);
  136. CoTaskMemFree(pstr);
  137. return hr;
  138. }
  139. HRESULT HrCreateNoteThisThread(WPARAM wp, LPARAM lp)
  140. {
  141. static HINSTANCE s_hRichEdit=0;
  142. HRESULT hr;
  143. CGWNote *pNote=0;
  144. CLSID clsid;
  145. // hack, need to free lib this
  146. if (!s_hRichEdit)
  147. s_hRichEdit = LoadLibrary("RICHED32.DLL");
  148. // need to create this puppy on new thread
  149. pNote = new CGWNote(NULL);
  150. if (!pNote)
  151. return E_OUTOFMEMORY;
  152. CLSIDFromString((LPOLESTR)wp, &clsid);
  153. hr = pNote->Init(clsid, (DWORD)lp);
  154. if (FAILED(hr))
  155. goto error;
  156. hr = pNote->Show();
  157. error:
  158. ReleaseObj(pNote);
  159. return hr;
  160. }