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.

359 lines
10 KiB

  1. #include "precomp.hxx"
  2. CMDCOMSrvFactory::CMDCOMSrvFactory()
  3. :m_mdcObject()
  4. {
  5. m_dwRefCount=0;
  6. //
  7. // Addref object, so refcount doesn't go to 0 if all clients release.
  8. //
  9. m_mdcObject.AddRef();
  10. }
  11. CMDCOMSrvFactory::~CMDCOMSrvFactory()
  12. {
  13. m_mdcObject.Release();
  14. }
  15. HRESULT
  16. CMDCOMSrvFactory::CreateInstance(
  17. IUnknown *pUnkOuter,
  18. REFIID riid,
  19. void ** ppObject)
  20. {
  21. HRESULT hr = S_OK;
  22. // Check arguments
  23. if ( ppObject == NULL )
  24. {
  25. hr = E_INVALIDARG;
  26. goto exit;
  27. }
  28. // Initialize
  29. *ppObject = NULL;
  30. // Check for aggregation
  31. if ( pUnkOuter != NULL )
  32. {
  33. // Aggregation is not supported by CMDCOM
  34. hr = CLASS_E_NOAGGREGATION;
  35. goto exit;
  36. }
  37. // Delegate
  38. hr = m_mdcObject.QueryInterface( riid, ppObject );
  39. if ( FAILED(hr) )
  40. {
  41. goto exit;
  42. }
  43. exit:
  44. return hr;
  45. }
  46. HRESULT
  47. CMDCOMSrvFactory::LockServer(
  48. BOOL fLock)
  49. {
  50. if (fLock)
  51. {
  52. InterlockedIncrement((long *)&g_dwRefCount);
  53. }
  54. else
  55. {
  56. InterlockedDecrement((long *)&g_dwRefCount);
  57. }
  58. return S_OK;
  59. }
  60. HRESULT
  61. CMDCOMSrvFactory::QueryInterface(
  62. REFIID riid,
  63. void **ppObject)
  64. {
  65. HRESULT hr = S_OK;
  66. // Check arguments
  67. if ( ppObject == NULL )
  68. {
  69. hr = E_INVALIDARG;
  70. goto exit;
  71. }
  72. // Initialize
  73. *ppObject = NULL;
  74. if ( ( riid != IID_IUnknown ) &&
  75. ( riid != IID_IClassFactory ) )
  76. {
  77. hr = E_NOINTERFACE;
  78. goto exit;
  79. }
  80. hr = m_mdcObject.GetConstructorError();
  81. if ( FAILED(hr) )
  82. {
  83. goto exit;
  84. }
  85. // Return the requested inteface
  86. *ppObject = (IClassFactory *) this;
  87. AddRef();
  88. exit:
  89. // Done
  90. return hr;
  91. }
  92. ULONG
  93. CMDCOMSrvFactory::AddRef()
  94. {
  95. DWORD dwRefCount;
  96. dwRefCount = InterlockedIncrement((long *)&m_dwRefCount);
  97. return dwRefCount;
  98. }
  99. ULONG
  100. CMDCOMSrvFactory::Release()
  101. {
  102. DWORD dwRefCount;
  103. dwRefCount = InterlockedDecrement((long *)&m_dwRefCount);
  104. //
  105. // There must only be one copy of this. So keep the first one around regardless.
  106. //
  107. return dwRefCount;
  108. }
  109. STDAPI
  110. DllGetClassObject(
  111. REFCLSID rclsid,
  112. REFIID riid,
  113. void ** ppObject)
  114. {
  115. HRESULT hr = S_OK;
  116. // Check arguments
  117. if ( ppObject == NULL )
  118. {
  119. hr = E_INVALIDARG;
  120. goto exit;
  121. }
  122. // Initialize
  123. *ppObject = NULL;
  124. // Check the class id
  125. if ( rclsid != CLSID_MDCOM )
  126. {
  127. hr = CLASS_E_CLASSNOTAVAILABLE;
  128. goto exit;
  129. }
  130. // If the class factory is NULL than we are not running in a service
  131. if ( g_pFactory == NULL )
  132. {
  133. hr = E_ACCESSDENIED;
  134. goto exit;
  135. }
  136. // Return the requested interface
  137. hr = g_pFactory->QueryInterface( riid, ppObject );
  138. if ( FAILED(hr) )
  139. {
  140. goto exit;
  141. }
  142. exit:
  143. // Done
  144. return hr;
  145. }
  146. HRESULT _stdcall
  147. DllCanUnloadNow()
  148. {
  149. if (g_dwRefCount)
  150. {
  151. return S_FALSE;
  152. }
  153. else
  154. {
  155. return S_OK;
  156. }
  157. }
  158. STDAPI
  159. DllRegisterServer(void)
  160. {
  161. HKEY hKeyCLSID;
  162. HKEY hKeyInproc32;
  163. DWORD dwDisposition;
  164. HMODULE hModule;
  165. DWORD dwReturn = ERROR_SUCCESS;
  166. dwReturn = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  167. TEXT("CLSID\\{BA4E57F0-FAB6-11cf-9D1A-00AA00A70D51}"),
  168. NULL,
  169. TEXT(""),
  170. REG_OPTION_NON_VOLATILE,
  171. KEY_ALL_ACCESS,
  172. NULL,
  173. &hKeyCLSID,
  174. &dwDisposition);
  175. if (dwReturn == ERROR_SUCCESS)
  176. {
  177. dwReturn = RegSetValueEx(hKeyCLSID,
  178. TEXT(""),
  179. NULL,
  180. REG_SZ,
  181. (BYTE*) TEXT("MD COM Server"),
  182. sizeof(TEXT("MD COM Server")));
  183. if (dwReturn == ERROR_SUCCESS)
  184. {
  185. dwReturn = RegCreateKeyEx(hKeyCLSID,
  186. "InprocServer32",
  187. NULL, TEXT(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  188. &hKeyInproc32, &dwDisposition);
  189. if (dwReturn == ERROR_SUCCESS)
  190. {
  191. hModule=GetModuleHandle(TEXT("METADATA.DLL"));
  192. if (!hModule)
  193. {
  194. dwReturn = GetLastError();
  195. }
  196. else
  197. {
  198. TCHAR szName[MAX_PATH+1];
  199. if (GetModuleFileName(hModule,
  200. szName,
  201. sizeof(szName)) == NULL)
  202. {
  203. dwReturn = GetLastError();
  204. }
  205. else
  206. {
  207. dwReturn = RegSetValueEx(hKeyInproc32,
  208. TEXT(""),
  209. NULL,
  210. REG_SZ,
  211. (BYTE*) szName,
  212. sizeof(TCHAR)*(lstrlen(szName)+1));
  213. if (dwReturn == ERROR_SUCCESS)
  214. {
  215. dwReturn = RegSetValueEx(hKeyInproc32,
  216. TEXT("ThreadingModel"),
  217. NULL,
  218. REG_SZ,
  219. (BYTE*) TEXT("Both"),
  220. sizeof(TEXT("Both")));
  221. }
  222. }
  223. }
  224. RegCloseKey(hKeyInproc32);
  225. }
  226. }
  227. RegCloseKey(hKeyCLSID);
  228. }
  229. if( dwReturn == ERROR_SUCCESS )
  230. {
  231. dwReturn = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  232. TEXT("CLSID\\{8ad3dcf8-869e-4c0e-89c2-86d7710610aa}"),
  233. NULL,
  234. TEXT(""),
  235. REG_OPTION_NON_VOLATILE,
  236. KEY_ALL_ACCESS,
  237. NULL,
  238. &hKeyCLSID,
  239. &dwDisposition);
  240. if (dwReturn == ERROR_SUCCESS)
  241. {
  242. dwReturn = RegSetValueEx(hKeyCLSID,
  243. TEXT(""),
  244. NULL,
  245. REG_SZ,
  246. (BYTE*) TEXT("MD COM2 Server"),
  247. sizeof(TEXT("MD COM2 Server")));
  248. if (dwReturn == ERROR_SUCCESS)
  249. {
  250. dwReturn = RegCreateKeyEx(hKeyCLSID,
  251. "InprocServer32",
  252. NULL, TEXT(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  253. &hKeyInproc32, &dwDisposition);
  254. if (dwReturn == ERROR_SUCCESS)
  255. {
  256. hModule=GetModuleHandle(TEXT("METADATA.DLL"));
  257. if (!hModule)
  258. {
  259. dwReturn = GetLastError();
  260. }
  261. else
  262. {
  263. TCHAR szName[MAX_PATH+1];
  264. if (GetModuleFileName(hModule,
  265. szName,
  266. sizeof(szName)) == NULL)
  267. {
  268. dwReturn = GetLastError();
  269. }
  270. else
  271. {
  272. dwReturn = RegSetValueEx(hKeyInproc32,
  273. TEXT(""),
  274. NULL,
  275. REG_SZ,
  276. (BYTE*) szName,
  277. sizeof(TCHAR)*(lstrlen(szName)+1));
  278. if (dwReturn == ERROR_SUCCESS)
  279. {
  280. dwReturn = RegSetValueEx(hKeyInproc32,
  281. TEXT("ThreadingModel"),
  282. NULL,
  283. REG_SZ,
  284. (BYTE*) TEXT("Both"),
  285. sizeof(TEXT("Both")));
  286. }
  287. }
  288. }
  289. RegCloseKey(hKeyInproc32);
  290. }
  291. }
  292. RegCloseKey(hKeyCLSID);
  293. }
  294. }
  295. return RETURNCODETOHRESULT(dwReturn);
  296. }
  297. STDAPI
  298. DllUnregisterServer(void)
  299. {
  300. DWORD dwReturn = ERROR_SUCCESS;
  301. dwReturn = RegDeleteKey(HKEY_CLASSES_ROOT,
  302. TEXT("CLSID\\{BA4E57F0-FAB6-11cf-9D1A-00AA00A70D51}\\InprocServer32"));
  303. if (dwReturn == ERROR_SUCCESS)
  304. {
  305. dwReturn = RegDeleteKey(HKEY_CLASSES_ROOT,
  306. TEXT("CLSID\\{BA4E57F0-FAB6-11cf-9D1A-00AA00A70D51}"));
  307. }
  308. if( dwReturn == ERROR_SUCCESS )
  309. {
  310. dwReturn = RegDeleteKey(HKEY_CLASSES_ROOT,
  311. TEXT("CLSID\\{8ad3dcf8-869e-4c0e-89c2-86d7710610aa}\\InprocServer32"));
  312. if (dwReturn == ERROR_SUCCESS)
  313. {
  314. dwReturn = RegDeleteKey(HKEY_CLASSES_ROOT,
  315. TEXT("CLSID\\{8ad3dcf8-869e-4c0e-89c2-86d7710610aa}"));
  316. }
  317. }
  318. return RETURNCODETOHRESULT(dwReturn);
  319. }
  320.