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.

343 lines
11 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1992-1999 Microsoft Corporation
  4. //
  5. // File: libmain.cpp
  6. //
  7. // Description :
  8. // The main entry point to the dll
  9. //
  10. // Part of : Wbem ADS 3rd party extension
  11. //
  12. // History:
  13. // corinaf 10/9/98 Created
  14. //
  15. //***************************************************************************
  16. #include "precomp.h"
  17. //#define INITGUID
  18. HINSTANCE g_hInst = NULL;
  19. ULONG g_ulObjCount = 0; // Number of objects alive in the dll
  20. BOOL g_bLogging = TRUE;
  21. //+---------------------------------------------------------------
  22. //
  23. // Function: DllGetClassObject
  24. //
  25. // Synopsis: Standard DLL entrypoint for locating class factories
  26. // Called by OLE's CoGetClassObject (from CoCreateInstance)
  27. //
  28. //----------------------------------------------------------------
  29. extern "C"
  30. STDAPI
  31. DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID FAR* ppv)
  32. {
  33. HRESULT hr;
  34. IClassFactory *pCF;
  35. *ppv = NULL;
  36. if (clsid == CLSID_WMIExtension)
  37. {
  38. pCF = new CWMIExtensionCF();
  39. if (!pCF)
  40. return E_OUTOFMEMORY;
  41. }
  42. else
  43. return E_FAIL;
  44. hr = pCF->QueryInterface(iid, ppv);
  45. //If QI failed delete the CF object
  46. if (FAILED(hr))
  47. delete pCF;
  48. // pCF->Release(); Don't release because CWMIExtensionCF() sets the ref count to 0 not 1.
  49. return hr;
  50. }
  51. //+---------------------------------------------------------------
  52. //
  53. // Function: DllCanUnloadNow
  54. //
  55. // Synopsis: Standard DLL entrypoint to determine if DLL can be unloaded
  56. //
  57. //---------------------------------------------------------------
  58. extern "C"
  59. STDAPI
  60. DllCanUnloadNow(void)
  61. {
  62. return (g_ulObjCount > 0 ? S_FALSE : S_OK);
  63. }
  64. //+---------------------------------------------------------------------------
  65. //
  66. // Function: DllMain
  67. //
  68. // Synopsis: entry point for NT - post .546
  69. //
  70. //----------------------------------------------------------------------------
  71. extern "C"
  72. BOOL WINAPI
  73. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  74. {
  75. switch (dwReason)
  76. {
  77. case DLL_PROCESS_ATTACH:
  78. DisableThreadLibraryCalls((HINSTANCE)hDll);
  79. g_hInst = (HINSTANCE)hDll;
  80. break;
  81. case DLL_PROCESS_DETACH:
  82. break;
  83. default:
  84. break;
  85. }
  86. return TRUE;
  87. // return LibMain((HINSTANCE)hDll, dwReason, lpReserved);
  88. }
  89. #define WCHAR_LEN_IN_BYTES(str) wcslen(str)*sizeof(WCHAR)+sizeof(WCHAR)
  90. //***************************************************************************
  91. //
  92. // DllRegisterServer
  93. //
  94. // Purpose: Called during setup or by regsvr32.
  95. //
  96. // Return: NOERROR if registration successful, error otherwise.
  97. //***************************************************************************
  98. STDAPI DllRegisterServer(void)
  99. {
  100. HKEY hKey1=NULL, hKey2=NULL, hKey3=NULL, hKey4=NULL;
  101. DWORD dwDisposition;
  102. WCHAR wcClsid[128], wcIid[128], wcTypelibid[128];
  103. WCHAR wcKey[128];
  104. WCHAR wcModule[128];
  105. WCHAR wcText[] = L"WMI ADSI Extension";
  106. WCHAR wcTypeLibText[] = L"WMI ADSI Extension Type Library";
  107. WCHAR wcModel[] = L"Apartment";
  108. // Create strings for the CLSID & IID
  109. StringFromGUID2(CLSID_WMIExtension, wcClsid, 128);
  110. StringFromGUID2(IID_IWMIExtension, wcIid, 128);
  111. StringFromGUID2(LIBID_WMIEXTENSIONLib, wcTypelibid, 128);
  112. GetModuleFileName(g_hInst, wcModule, 128);
  113. //Create entry under CLSID
  114. //==========================
  115. wcscpy(wcKey, L"CLSID\\");
  116. wcscat(wcKey, wcClsid);
  117. RegCreateKeyEx(HKEY_CLASSES_ROOT, wcKey,
  118. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  119. &hKey1, &dwDisposition);
  120. if (!hKey1) return E_FAIL;
  121. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (LPBYTE)wcText, WCHAR_LEN_IN_BYTES(wcText));
  122. RegCreateKeyEx(hKey1,L"InprocServer32",
  123. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  124. &hKey2, &dwDisposition);
  125. if (!hKey2) { RegCloseKey(hKey1); return E_FAIL; }
  126. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (LPBYTE)wcModule, WCHAR_LEN_IN_BYTES(wcModule));
  127. RegSetValueEx(hKey2, L"ThreadingModel", 0, REG_SZ, (LPBYTE)wcModel, WCHAR_LEN_IN_BYTES(wcModel));
  128. RegCloseKey(hKey2); hKey2=NULL;
  129. //RegCreateKey(hKey1, L"ProgID", &hKey2);
  130. //RegSetValueEx(hKey2, NULL, 0, REG_SZ, (LPBYTE)WBEM_NAMESPACE_NAME, WCHAR_LEN_IN_BYTES(WBEM_NAMESPACE_NAME));
  131. //RegCloseKey(hKey2);
  132. RegCreateKeyEx(hKey1, L"TypeLib",
  133. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  134. &hKey2, &dwDisposition);
  135. if (!hKey2) { RegCloseKey(hKey1); return E_FAIL; }
  136. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (LPBYTE)wcTypelibid, WCHAR_LEN_IN_BYTES(wcTypelibid));
  137. RegCloseKey(hKey2); hKey2=NULL;
  138. RegCreateKeyEx(hKey1, L"Version",
  139. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  140. &hKey2, &dwDisposition);
  141. if (!hKey2) { RegCloseKey(hKey1); return E_FAIL; }
  142. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (LPBYTE)L"1.0", WCHAR_LEN_IN_BYTES(L"1.0"));
  143. RegCloseKey(hKey2); hKey2=NULL;
  144. RegCloseKey(hKey1); hKey1=NULL;
  145. //Create entries under Typelib for the type library
  146. //=================================================
  147. wcscpy(wcKey, L"Typelib\\");
  148. wcscat(wcKey, wcTypelibid);
  149. RegCreateKeyEx(HKEY_CLASSES_ROOT, wcKey,
  150. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  151. &hKey1, &dwDisposition);
  152. if (!hKey1) return E_FAIL;
  153. RegCreateKeyEx(hKey1, L"1.0",
  154. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  155. &hKey2, &dwDisposition);
  156. if (!hKey2) { RegCloseKey(hKey1); return E_FAIL; }
  157. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (LPBYTE)wcTypeLibText, WCHAR_LEN_IN_BYTES(wcTypeLibText));
  158. RegCreateKeyEx(hKey2, L"0",
  159. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  160. &hKey3, &dwDisposition);
  161. if (!hKey3) { RegCloseKey(hKey2); RegCloseKey(hKey1); return E_FAIL; }
  162. RegCreateKeyEx(hKey3, L"win32",
  163. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  164. &hKey4, &dwDisposition);
  165. if (!hKey4) { RegCloseKey(hKey3); RegCloseKey(hKey2); RegCloseKey(hKey1); return E_FAIL; }
  166. //Create path to typelib - take module path and change file extension
  167. WCHAR *ext = wcsrchr(wcModule, L'.');
  168. wcscpy(ext, L".tlb");
  169. RegSetValueEx(hKey4, NULL, 0, REG_SZ, (LPBYTE)wcModule, WCHAR_LEN_IN_BYTES(wcModule));
  170. RegCloseKey(hKey4); hKey4=NULL;
  171. RegCloseKey(hKey3); hKey3=NULL;
  172. RegCreateKeyEx(hKey2, L"FLAGS",
  173. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  174. &hKey3, &dwDisposition);
  175. if (!hKey3) { RegCloseKey(hKey2); RegCloseKey(hKey1); return E_FAIL; }
  176. RegSetValueEx(hKey3, NULL, 0, REG_SZ, (LPBYTE)L"0", WCHAR_LEN_IN_BYTES(L"0"));
  177. RegCloseKey(hKey3); hKey3=NULL;
  178. RegCreateKeyEx(hKey2, L"HELPDIR",
  179. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  180. &hKey3, &dwDisposition);
  181. if (!hKey3) { RegCloseKey(hKey2); RegCloseKey(hKey1); return E_FAIL; }
  182. RegSetValueEx(hKey3, NULL, 0, REG_SZ, (LPBYTE)L"", WCHAR_LEN_IN_BYTES(L""));
  183. RegCloseKey(hKey3); hKey3=NULL;
  184. RegCloseKey(hKey2); hKey2=NULL;
  185. RegCloseKey(hKey1); hKey1=NULL;
  186. //Make ADSI extension registration
  187. //=================================
  188. wcscpy(wcKey, L"SOFTWARE\\Microsoft\\ADs\\Providers\\LDAP\\Extensions\\");
  189. wcscat (wcKey, L"Computer\\"); //for Computer class extension
  190. wcscat(wcKey, wcClsid);
  191. RegCreateKeyEx(HKEY_LOCAL_MACHINE, wcKey,
  192. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  193. &hKey1, &dwDisposition);
  194. if (!hKey1) return E_FAIL;
  195. RegSetValueEx(hKey1, L"Interfaces", 0, REG_MULTI_SZ, (LPBYTE)wcIid, WCHAR_LEN_IN_BYTES(wcIid));
  196. RegCloseKey(hKey1); hKey1=NULL;
  197. return NOERROR;
  198. }
  199. //***************************************************************************
  200. //
  201. // DllUnregisterServer
  202. //
  203. // Purpose: Called when it is time to remove the registry entries.
  204. //
  205. // Return: NOERROR if registration successful, error otherwise.
  206. //***************************************************************************
  207. STDAPI DllUnregisterServer(void)
  208. {
  209. HRESULT hr;
  210. HKEY hKey1, hKey2, hKey3;
  211. WCHAR wcClsid[128], wcIid[128], wcTypelibid[128];
  212. WCHAR wcKey[128];
  213. WCHAR wcText[] = L"WMI ADSI Extension";
  214. WCHAR wcTypeLibText[] = L"WMI ADSI Extension Type Library";
  215. WCHAR wcModel[] = L"Apartment";
  216. // Create strings for the CLSID & IID
  217. StringFromGUID2(CLSID_WMIExtension, wcClsid, 128);
  218. StringFromGUID2(IID_IWMIExtension, wcIid, 128);
  219. StringFromGUID2(LIBID_WMIEXTENSIONLib, wcTypelibid, 128);
  220. //Delete entry under \software\microsoft\ads for the extension
  221. //============================================================
  222. wcscpy(wcKey, L"SOFTWARE\\Microsoft\\ADs\\Providers\\LDAP\\Extensions\\");
  223. wcscat (wcKey, L"Computer\\"); //for Computer class extension
  224. hr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, wcKey, 0, KEY_ALL_ACCESS, &hKey1);
  225. if (hr == NOERROR)
  226. {
  227. RegDeleteKey(hKey1, wcClsid);
  228. RegCloseKey(hKey1);
  229. }
  230. //Delete entries under CLSID
  231. //==========================
  232. wcscpy(wcKey, L"CLSID\\");
  233. wcscat(wcKey, wcClsid);
  234. hr = RegOpenKeyEx(HKEY_CLASSES_ROOT, wcKey, 0, KEY_ALL_ACCESS, &hKey1);
  235. if(hr == NOERROR)
  236. {
  237. RegDeleteKey(hKey1, L"InProcServer32");
  238. RegDeleteKey(hKey1, L"TypeLib");
  239. RegDeleteKey(hKey1, L"Version");
  240. RegCloseKey(hKey1);
  241. }
  242. hr = RegOpenKeyEx(HKEY_CLASSES_ROOT, L"CLSID", 0, KEY_ALL_ACCESS, &hKey1);
  243. if(hr == NOERROR)
  244. {
  245. RegDeleteKey(hKey1,wcClsid);
  246. RegCloseKey(hKey1);
  247. }
  248. //Delete entries under Typelib for the type library
  249. //=================================================
  250. wcscpy(wcKey, L"Typelib\\");
  251. wcscat(wcKey, wcTypelibid);
  252. hr = RegOpenKeyEx(HKEY_CLASSES_ROOT, wcKey, 0, KEY_ALL_ACCESS, &hKey1);
  253. if (hr == NOERROR)
  254. {
  255. hr = RegOpenKeyEx(hKey1, L"1.0", 0, KEY_ALL_ACCESS, &hKey2);
  256. if (hr == NOERROR)
  257. {
  258. hr = RegOpenKeyEx(hKey2, L"0", 0, KEY_ALL_ACCESS, &hKey3);
  259. if (hr == NOERROR)
  260. {
  261. RegDeleteKey(hKey3, L"win32");
  262. RegCloseKey(hKey3);
  263. }
  264. RegDeleteKey(hKey2, L"0");
  265. RegDeleteKey(hKey2, L"FLAGS");
  266. RegDeleteKey(hKey2, L"HELPDIR");
  267. RegCloseKey(hKey2);
  268. }
  269. RegDeleteKey(hKey1, L"1.0");
  270. RegCloseKey(hKey1);
  271. }
  272. hr = RegOpenKeyEx(HKEY_CLASSES_ROOT, L"Typelib", 0, KEY_ALL_ACCESS, &hKey1);
  273. if (hr == NOERROR)
  274. {
  275. RegDeleteKey(hKey1, wcTypelibid);
  276. RegCloseKey(hKey1);
  277. }
  278. return NOERROR;
  279. }