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.

336 lines
7.2 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 1999--2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // OleServerExport.cpp
  7. //
  8. // Description:
  9. // description-for-module
  10. //
  11. // History:
  12. // Xing Jin (i-xingj) 06-Dec-2000
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #include "stdafx.h"
  16. #include <time.h>
  17. #include <locale.h>
  18. #include "CFactory.h"
  19. #include "CAlertEmailConsumerProvider.h"
  20. #include "AlertEmailProviderGuid.h"
  21. static HINSTANCE g_hInstance;
  22. LONG g_cObj = 0;
  23. LONG g_cLock= 0;
  24. //////////////////////////////////////////////////////////////////////////////
  25. //
  26. // DllMain
  27. //
  28. // Description:
  29. // Entry point of the module.
  30. //
  31. // Arguments:
  32. // [in] hinstDLLIn
  33. // dwReasonIn
  34. // lpReservedIn
  35. //
  36. // Returns:
  37. // TRUE
  38. // FALSE
  39. //
  40. // History:
  41. // Xing Jin (i-xingj) 06-Dec-2000
  42. //
  43. //////////////////////////////////////////////////////////////////////////////
  44. BOOL
  45. WINAPI
  46. DllMain(
  47. HINSTANCE hinstDLLIn,
  48. DWORD dwReasonIn,
  49. LPVOID lpReservedIn
  50. )
  51. {
  52. if ( dwReasonIn == DLL_PROCESS_ATTACH )
  53. {
  54. g_hInstance = hinstDLLIn;
  55. setlocale( LC_ALL, "" );
  56. }
  57. else if ( dwReasonIn == DLL_PROCESS_DETACH )
  58. {
  59. }
  60. return TRUE;
  61. }
  62. //////////////////////////////////////////////////////////////////////////////
  63. //
  64. // DllGetClassObject
  65. //
  66. // Description:
  67. // Retrieves the class object from the module.
  68. //
  69. // Arguments:
  70. // [in] rclsidIn
  71. // riidIn
  72. // [out] ppvOut
  73. //
  74. // Return:
  75. // S_OK
  76. // CLASS_E_CLASSNOTAVAILABLE
  77. //
  78. // History:
  79. // Xing Jin (i-xingj) 06-Dec-2000
  80. //
  81. //////////////////////////////////////////////////////////////////////////////
  82. extern "C"
  83. HRESULT
  84. APIENTRY
  85. DllGetClassObject(
  86. REFCLSID rclsidIn,
  87. REFIID riidIn,
  88. LPVOID * ppvOut
  89. )
  90. {
  91. HRESULT hr;
  92. CFactory * pFactory;
  93. //
  94. // Verify the caller is asking for our type of object.
  95. //
  96. if ( CLSID_AlertEmailConsumerProvider != rclsidIn )
  97. {
  98. return E_FAIL;
  99. }
  100. //
  101. // Check that we can provide the interface.
  102. //
  103. if ( IID_IUnknown != riidIn && IID_IClassFactory != riidIn )
  104. {
  105. return E_NOINTERFACE;
  106. }
  107. //
  108. // Get a new class factory.
  109. //
  110. pFactory = new CFactory( rclsidIn );
  111. if ( !pFactory )
  112. {
  113. return E_OUTOFMEMORY;
  114. }
  115. //
  116. // Verify we can get an instance.
  117. //
  118. hr = pFactory->QueryInterface( riidIn, ppvOut );
  119. if ( FAILED( hr ) )
  120. {
  121. delete pFactory;
  122. }
  123. return hr;
  124. }
  125. //////////////////////////////////////////////////////////////////////////////
  126. //
  127. // DllCanUnloadNow
  128. //
  129. // Description:
  130. // Retrieves the class object from the module.
  131. //
  132. // Return:
  133. // SA_OK
  134. // SA_FALSE
  135. //
  136. // History:
  137. // Xing Jin (i-xingj) 06-Dec-2000
  138. //
  139. //////////////////////////////////////////////////////////////////////////////
  140. extern "C"
  141. HRESULT
  142. APIENTRY
  143. DllCanUnloadNow(void)
  144. {
  145. SCODE sc = TRUE;
  146. if (g_cObj || g_cLock)
  147. sc = S_FALSE;
  148. return sc;
  149. }
  150. //////////////////////////////////////////////////////////////////////////////
  151. //
  152. // DllRegisterServer
  153. //
  154. // Description:
  155. // Standard OLE entry point for registering the server.
  156. //
  157. // Returns:
  158. // S_OK Registration was successful
  159. // E_FAIL Registration failed.
  160. //
  161. // History:
  162. // Xing Jin (i-xingj) 06-Dec-2000
  163. //
  164. //////////////////////////////////////////////////////////////////////////////
  165. extern "C"
  166. HRESULT
  167. APIENTRY
  168. DllRegisterServer(void)
  169. {
  170. wchar_t Path[1024];
  171. wchar_t *pGuidStr = 0;
  172. wchar_t KeyPath[1024];
  173. //
  174. // Where are we?
  175. //
  176. DWORD dwResult = GetModuleFileNameW(g_hInstance, Path, 1023);
  177. if (0 == dwResult)
  178. {
  179. return (HRESULT_FROM_WIN32 (GetLastError ()));
  180. }
  181. Path[1023] = L'\0';
  182. //
  183. // Convert CLSID to string.
  184. //
  185. HRESULT hr = StringFromCLSID(CLSID_AlertEmailConsumerProvider, &pGuidStr);
  186. if (FAILED (hr))
  187. {
  188. return (hr);
  189. }
  190. int cchWritten = _snwprintf(KeyPath, 1023, L"CLSID\\\\%s", pGuidStr);
  191. if (cchWritten < 0)
  192. {
  193. CoTaskMemFree(pGuidStr);
  194. return E_FAIL;
  195. }
  196. KeyPath [1023] = L'\0';
  197. HKEY hKey;
  198. LONG lRes = RegCreateKeyW(HKEY_CLASSES_ROOT, KeyPath, &hKey);
  199. if (ERROR_SUCCESS != lRes)
  200. {
  201. CoTaskMemFree(pGuidStr);
  202. return E_FAIL;
  203. }
  204. wchar_t *pName = L"Microsoft Server Appliance: Alert Email Consumer Provider";
  205. lRes = RegSetValueExW(hKey, 0, 0, REG_SZ, (const BYTE *) pName, wcslen(pName) * 2 + 2);
  206. if (ERROR_SUCCESS != lRes)
  207. {
  208. RegCloseKey (hKey);
  209. CoTaskMemFree(pGuidStr);
  210. return E_FAIL;
  211. }
  212. HKEY hSubkey;
  213. lRes = RegCreateKey(hKey, L"InprocServer32", &hSubkey);
  214. if (ERROR_SUCCESS != lRes)
  215. {
  216. RegCloseKey (hKey);
  217. CoTaskMemFree(pGuidStr);
  218. return E_FAIL;
  219. }
  220. lRes = RegSetValueExW(hSubkey, 0, 0, REG_SZ, (const BYTE *) Path, wcslen(Path) * 2 + 2);
  221. if (ERROR_SUCCESS != lRes)
  222. {
  223. RegCloseKey (hSubkey);
  224. RegCloseKey (hKey);
  225. CoTaskMemFree(pGuidStr);
  226. return E_FAIL;
  227. }
  228. RegSetValueExW(hSubkey, L"ThreadingModel", 0, REG_SZ, (const BYTE *) L"Both", wcslen(L"Both") * 2 + 2);
  229. RegCloseKey(hSubkey);
  230. RegCloseKey(hKey);
  231. CoTaskMemFree(pGuidStr);
  232. return S_OK;
  233. }
  234. //////////////////////////////////////////////////////////////////////////////
  235. //
  236. // DllUnregisterServer
  237. //
  238. // Description:
  239. // Standard OLE entry point for unregistering the server.
  240. //
  241. // Returns:
  242. // S_OK Unregistration was successful
  243. // E_FAIL Unregistration failed.
  244. //
  245. // History:
  246. // Xing Jin (i-xingj) 06-Dec-2000
  247. //
  248. //////////////////////////////////////////////////////////////////////////////
  249. extern "C"
  250. HRESULT
  251. APIENTRY
  252. DllUnregisterServer(void)
  253. {
  254. wchar_t *pGuidStr = 0;
  255. HKEY hKey;
  256. wchar_t KeyPath[256];
  257. HRESULT hr = StringFromCLSID(CLSID_AlertEmailConsumerProvider, &pGuidStr);
  258. if (FAILED (hr))
  259. {
  260. return (hr);
  261. }
  262. int cchWritten = _snwprintf(KeyPath, 255, L"CLSID\\%s", pGuidStr);
  263. if (cchWritten < 0)
  264. {
  265. CoTaskMemFree(pGuidStr);
  266. return E_FAIL;
  267. }
  268. KeyPath [255] = L'\0';
  269. //
  270. // Delete InProcServer32 subkey.
  271. //
  272. LONG lRes = RegOpenKeyW(HKEY_CLASSES_ROOT, KeyPath, &hKey);
  273. if ( lRes )
  274. {
  275. CoTaskMemFree(pGuidStr);
  276. return E_FAIL;
  277. }
  278. RegDeleteKeyW(hKey, L"InprocServer32");
  279. RegCloseKey(hKey);
  280. //
  281. // Delete CLSID GUID key.
  282. //
  283. lRes = RegOpenKeyW(HKEY_CLASSES_ROOT, L"CLSID", &hKey);
  284. if ( lRes )
  285. {
  286. CoTaskMemFree(pGuidStr);
  287. return E_FAIL;
  288. }
  289. RegDeleteKeyW(hKey, pGuidStr);
  290. RegCloseKey(hKey);
  291. CoTaskMemFree(pGuidStr);
  292. return S_OK;
  293. }