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.

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