Source code of Windows XP (NT5)
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.

294 lines
7.4 KiB

  1. //+-------------------------------------------------------------------
  2. // File: csrvapp.cxx
  3. //
  4. // Contents: Implementation of CTestServerApp
  5. //
  6. // Classes: CTestServerApp
  7. //
  8. // History: 17-Dec-92 DeanE Created
  9. // 31-Dec-93 ErikGav Chicago port
  10. // 25-Apr-95 BruceMa CoRevokeClassObject before shutting down
  11. //---------------------------------------------------------------------
  12. #pragma optimize("",off)
  13. #include <windows.h>
  14. #include <ole2.h>
  15. #include "testsrv.hxx"
  16. #include <except.hxx>
  17. void ProcessCmdLine(LPSTR, BOOL *);
  18. // Used to send a quit message
  19. extern HWND g_hwndMain;
  20. //+--------------------------------------------------------------
  21. // Function: CTestServerApp::CTestServerApp
  22. //
  23. // Synopsis: Constructor - initialize members
  24. //
  25. // Parameters: None
  26. //
  27. // Returns: None
  28. //
  29. // History: 17-Dec-92 DeanE Created
  30. //---------------------------------------------------------------
  31. CTestServerApp::CTestServerApp()
  32. {
  33. _pteClassFactory = NULL;
  34. _dwRegId = 0;
  35. _fRegistered = FALSE;
  36. _fInitialized = FALSE;
  37. _fEmbedded = TRUE;
  38. _cEmbeddedObjs = 0;
  39. }
  40. //+--------------------------------------------------------------
  41. // Function: CTestServerApp::~CTestServerApp
  42. //
  43. // Synopsis: Insure pointers are free - note this is mainly for
  44. // error-checking.
  45. //
  46. // Parameters: None
  47. //
  48. // Returns: None
  49. //
  50. // History: 17-Dec-92 DeanE Created
  51. //---------------------------------------------------------------
  52. CTestServerApp::~CTestServerApp()
  53. {
  54. Win4Assert(_pteClassFactory == NULL &&
  55. "Class factory should have been released");
  56. }
  57. //+--------------------------------------------------------------
  58. // Function: CTestServerApp::InitApp
  59. //
  60. // Synopsis: Initialize this instance of the app.
  61. //
  62. // Parameters: [lpszCmdline] - Command line of the application.
  63. //
  64. // Returns: S_OK if everything was initialized, or an error if not.
  65. //
  66. // History: 17-Dec-92 DeanE Created
  67. //
  68. // Notes: If this does not return, the CloseApp method should
  69. // still be called for proper cleanup.
  70. //---------------------------------------------------------------
  71. SCODE CTestServerApp::InitApp(LPSTR lpszCmdline)
  72. {
  73. SCODE sc;
  74. // Check OLE version running
  75. // BUGBUG - NYI by OLE
  76. // Bail out if we are not running with an acceptable version of OLE
  77. // Process Command Line arguments
  78. ProcessCmdLine(lpszCmdline, &_fEmbedded);
  79. // Look up the thread mode from the win.ini file.
  80. DWORD thread_mode;
  81. TCHAR buffer[80];
  82. int len;
  83. len = GetProfileString( TEXT("TestSrv"),
  84. TEXT("ThreadMode"),
  85. TEXT("MultiThreaded"),
  86. buffer,
  87. sizeof(buffer) / sizeof(TCHAR));
  88. if (lstrcmp(buffer, TEXT("ApartmentThreaded")) == 0)
  89. {
  90. thread_mode = COINIT_APARTMENTTHREADED;
  91. sc = CoInitialize(NULL);
  92. }
  93. else
  94. {
  95. #ifdef MULTI_THREADING
  96. thread_mode = COINIT_MULTITHREADED;
  97. sc = CoInitializeEx(NULL, thread_mode);
  98. #else
  99. // multi-threading not supported
  100. sc = E_INVALIDARG;
  101. #endif
  102. }
  103. if (S_OK == sc)
  104. {
  105. _fInitialized = TRUE;
  106. }
  107. else
  108. {
  109. return(sc);
  110. }
  111. // Create the applications class factory - note that we have to free
  112. // at a later time
  113. _pteClassFactory = CTestEmbedCF::Create(this);
  114. if (NULL == _pteClassFactory)
  115. {
  116. return(E_ABORT);
  117. }
  118. // Register the class with OLE
  119. sc = CoRegisterClassObject(
  120. CLSID_TestEmbed,
  121. _pteClassFactory,
  122. CLSCTX_LOCAL_SERVER,
  123. REGCLS_MULTIPLEUSE,
  124. &_dwRegId);
  125. if (S_OK == sc)
  126. {
  127. _fRegistered = TRUE;
  128. }
  129. return(sc);
  130. }
  131. //+--------------------------------------------------------------
  132. // Function: CTestServerApp::CloseApp
  133. //
  134. // Synopsis: Clean up resources this instance of the app is using.
  135. //
  136. // Parameters: None
  137. //
  138. // Returns: S_OK if everything was cleaned up, or an error if not.
  139. //
  140. // History: 17-Dec-92 DeanE Created
  141. //---------------------------------------------------------------
  142. SCODE CTestServerApp::CloseApp()
  143. {
  144. // Release this apps class factory, and insure the returned count is 0
  145. if (NULL != _pteClassFactory)
  146. {
  147. if (0 == _pteClassFactory->Release())
  148. {
  149. _pteClassFactory = NULL;
  150. }
  151. else
  152. {
  153. // BUGBUG - Log error
  154. }
  155. }
  156. // Uninitialize OLE only if OleInitialize succeeded
  157. if (TRUE == _fInitialized)
  158. {
  159. CoUninitialize();
  160. }
  161. return(S_OK);
  162. }
  163. //+--------------------------------------------------------------
  164. // Function: CTestServerApp::GetEmbeddedFlag
  165. //
  166. // Synopsis: Returns TRUE if app was started for an embedded object,
  167. // FALSE if standalone.
  168. //
  169. // Parameters: None
  170. //
  171. // Returns: BOOL (_fEmbedded)
  172. //
  173. // History: 17-Dec-92 DeanE Created
  174. //
  175. // Notes: BUGBUG - This should be an inline method
  176. //---------------------------------------------------------------
  177. CTestServerApp::GetEmbeddedFlag()
  178. {
  179. return(_fEmbedded);
  180. }
  181. //+--------------------------------------------------------------
  182. // Function: CTestServerApp::IncEmbeddedCount
  183. //
  184. // Synopsis: Increments the count of embedded objects the server
  185. // has open.
  186. //
  187. // Parameters: None
  188. //
  189. // Returns: ULONG (_cEmbeddedObjs)
  190. //
  191. // History: 17-Dec-92 DeanE Created
  192. //
  193. // Notes: BUGBUG - This should be an inline method
  194. //---------------------------------------------------------------
  195. ULONG CTestServerApp::IncEmbeddedCount()
  196. {
  197. return(++_cEmbeddedObjs);
  198. }
  199. //+--------------------------------------------------------------
  200. // Function: CTestServerApp::DecEmbeddedCount
  201. //
  202. // Synopsis: Decrements the count of embedded objects the server
  203. // has open. If 0 are left and we were running for an
  204. // embedded object(s), shut down.
  205. //
  206. // Parameters: None
  207. //
  208. // Returns: ULONG (_cEmbeddedObjs)
  209. //
  210. // History: 17-Dec-92 DeanE Created
  211. //
  212. //---------------------------------------------------------------
  213. ULONG CTestServerApp::DecEmbeddedCount()
  214. {
  215. if ((0 == --_cEmbeddedObjs) && _fEmbedded)
  216. {
  217. // Revoke the class object, if registered
  218. if (TRUE == _fRegistered)
  219. {
  220. CoRevokeClassObject(_dwRegId);
  221. // OutputDebugStringA("Revoking class object now!\n");
  222. }
  223. // Shut down the app
  224. SendMessage(g_hwndMain, WM_USER, 0xFFFFFFFF, 0xFFFFFFFF);
  225. }
  226. return(_cEmbeddedObjs);
  227. }
  228. //+--------------------------------------------------------------
  229. // Function: ProcessCmdline
  230. //
  231. // Synopsis: Checks the cmd line parameters, in particular for
  232. // '/Embedding' or '-Embedding'.
  233. //
  234. // Parameters: [lpszCmdLine] - Command line buffer.
  235. // [pfEmbedded] - Flag should be set to true if we get
  236. // the '/Embedding' switch.
  237. //
  238. // Returns: void
  239. //
  240. // History: 25-Nov-92 DeanE Created
  241. //
  242. // Notes: Only two valid commandlines for this program:
  243. // (1) -Embedding when started by OLE or (2) Null
  244. // string if started from the command line.
  245. //---------------------------------------------------------------
  246. void ProcessCmdLine(LPSTR lpszCmdline, BOOL *pfEmbedded)
  247. {
  248. if (lpszCmdline[0] == 0)
  249. {
  250. *pfEmbedded = FALSE;
  251. return;
  252. }
  253. if (strcmp(lpszCmdline, "-Embedding") == 0)
  254. {
  255. *pfEmbedded = TRUE;
  256. return;
  257. }
  258. Win4Assert(!"testsrv received an invalid command line!");
  259. *pfEmbedded = FALSE;
  260. return;
  261. }