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.

411 lines
9.4 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CopyRight ( c ) 1999 Microsoft Corporation
  4. //
  5. // Module Name: Dnsprov.cpp
  6. //
  7. // Description:
  8. // Implementation of dll exported functions
  9. //
  10. // Author:
  11. // Henry Wang ( henrywa ) March 8, 2000
  12. //
  13. //
  14. //////////////////////////////////////////////////////////////////////
  15. #include "DnsWmi.h"
  16. DEFINE_GUID(CLSID_DNS_SERVER,0x62269fec, 0x7b32, 0x11d2, 0x9a, 0xb7,0x00, 0x00, 0xf8, 0x75, 0xc5, 0xd4);
  17. // { 62269fec-7b32-11d2-9ab7-0000f875c5d4 }
  18. //Count number of objects and number of locks.
  19. long g_cObj=0;
  20. long g_cLock=0;
  21. HMODULE ghModule;
  22. extern DWORD DnsWmiDebugFlag = 0;
  23. extern DWORD DnsLibDebugFlag = 0;
  24. //***************************************************************************
  25. //
  26. // CompileMofFile
  27. //
  28. // Purpose: Automagically compile the MOF file into the WMI repository.
  29. //
  30. // Return: S_OK or error if unable to compile or file MOF.
  31. //
  32. //***************************************************************************
  33. static
  34. SCODE
  35. CompileMofFile(
  36. VOID )
  37. {
  38. SCODE sc = S_OK;
  39. const WCHAR szMofRelativePath[] = L"\\system32\\wbem\\dnsprov.mof";
  40. WCHAR szMofPath[ MAX_PATH + 5 ] = L"";
  41. IMofCompiler * pMofComp = NULL;
  42. HANDLE h;
  43. WBEM_COMPILE_STATUS_INFO Info;
  44. //
  45. // Formulate path of MOF file
  46. //
  47. if ( GetSystemWindowsDirectoryW(
  48. szMofPath,
  49. MAX_PATH - wcslen( szMofRelativePath ) ) == 0 )
  50. {
  51. sc = GetLastError();
  52. goto Done;
  53. }
  54. lstrcatW( szMofPath, szMofRelativePath );
  55. //
  56. // Verify that MOF file exists.
  57. //
  58. h = CreateFileW(
  59. szMofPath,
  60. GENERIC_READ,
  61. FILE_SHARE_READ | FILE_SHARE_WRITE,
  62. NULL,
  63. OPEN_EXISTING,
  64. FILE_ATTRIBUTE_NORMAL,
  65. NULL );
  66. if ( h == INVALID_HANDLE_VALUE )
  67. {
  68. sc = ERROR_FILE_NOT_FOUND;
  69. goto Done;
  70. }
  71. CloseHandle( h );
  72. //
  73. // Load and invoke the MOF compiler.
  74. //
  75. sc = CoCreateInstance(
  76. CLSID_MofCompiler,
  77. NULL,
  78. CLSCTX_INPROC_SERVER,
  79. IID_IMofCompiler,
  80. ( LPVOID * ) &pMofComp );
  81. if ( FAILED( sc ) )
  82. {
  83. goto Done;
  84. }
  85. sc = pMofComp->CompileFile (
  86. ( LPWSTR ) szMofPath,
  87. NULL, // load into namespace specified in MOF file
  88. NULL, // use default User
  89. NULL, // use default Authority
  90. NULL, // use default Password
  91. 0, // no options
  92. 0, // no class flags
  93. 0, // no instance flags
  94. &Info );
  95. //
  96. // Cleanup and return.
  97. //
  98. Done:
  99. if ( pMofComp )
  100. {
  101. pMofComp->Release();
  102. }
  103. return sc;
  104. } // CompileMofFile
  105. //***************************************************************************
  106. //
  107. BOOL
  108. WINAPI
  109. DllMain(
  110. HANDLE hModule,
  111. DWORD dwReason,
  112. LPVOID lpReserved
  113. )
  114. {
  115. DBG_FN( "DllMain" );
  116. #if DBG
  117. DWORD pid = GetCurrentProcessId();
  118. if ( dwReason == DLL_PROCESS_ATTACH )
  119. {
  120. CHAR szBase[ MAX_PATH ];
  121. CHAR szFlagFile[ MAX_PATH + 50 ];
  122. CHAR szLogFile[ MAX_PATH + 50 ];
  123. //
  124. // Initialize debug logging.
  125. //
  126. GetWindowsDirectoryA( szBase, sizeof( szBase ) );
  127. strcat( szBase, DNSWMI_DBG_LOG_DIR );
  128. strcpy( szFlagFile, szBase );
  129. strcat( szFlagFile, DNSWMI_DBG_FLAG_FILE_NAME );
  130. sprintf(
  131. szLogFile,
  132. "%s" DNSWMI_DBG_LOG_FILE_BASE_NAME ".%03X.log",
  133. szBase,
  134. pid );
  135. Dns_StartDebug(
  136. 0,
  137. szFlagFile,
  138. &DnsWmiDebugFlag,
  139. szLogFile,
  140. DNSWMI_DBG_LOG_FILE_WRAP );
  141. // Turn off dnslib logging except for basic output controls.
  142. if ( pDnsDebugFlag )
  143. {
  144. pDnsDebugFlag = &DnsLibDebugFlag;
  145. *pDnsDebugFlag = 0x1000000D;
  146. }
  147. IF_DEBUG( START_BREAK )
  148. {
  149. DebugBreak();
  150. }
  151. }
  152. #endif
  153. DNS_DEBUG( INIT, (
  154. "%s: PID %03X reason %d returning TRUE\n", fn, pid, dwReason ));
  155. return TRUE;
  156. } // DllMain
  157. //***************************************************************************
  158. //
  159. // DllCanUnloadNow
  160. //
  161. // Purpose: Called periodically by Ole in order to determine if the
  162. // DLL can be freed.
  163. //
  164. // Return: S_OK if there are no objects in use and the class factory
  165. // isn't locked.
  166. //
  167. //***************************************************************************
  168. STDAPI DllCanUnloadNow(void)
  169. {
  170. DBG_FN( "DllCanUnloadNow" )
  171. SCODE sc;
  172. //It is OK to unload if there are no objects or locks on the
  173. // class factory.
  174. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  175. DNS_DEBUG( INIT, ( "%s: returning 0x%08x\n", fn, sc ));
  176. return sc;
  177. }
  178. //***************************************************************************
  179. //
  180. // DllRegisterServer
  181. //
  182. // Purpose: Called during setup or by regsvr32.
  183. //
  184. // Return: NOERROR if registration successful, error otherwise.
  185. //***************************************************************************
  186. STDAPI DllRegisterServer(void)
  187. {
  188. SCODE sc = S_OK;
  189. TCHAR szID[128];
  190. WCHAR wcID[128];
  191. TCHAR szCLSID[128];
  192. TCHAR szModule[MAX_PATH];
  193. TCHAR * pName = TEXT("MS_NT_DNS_PROVIDER");
  194. TCHAR * pModel = TEXT("Both");
  195. HKEY hKey1, hKey2;
  196. ghModule = GetModuleHandle(TEXT("Dnsprov"));
  197. // Create the path.
  198. StringFromGUID2(
  199. CLSID_DNS_SERVER,
  200. wcID,
  201. 128);
  202. #ifndef UNICODE
  203. wcstombs(szID, wcID, 128);
  204. #else
  205. _tcscpy(szID, wcID);
  206. #endif
  207. lstrcpy(
  208. szCLSID,
  209. TEXT("Software\\classes\\CLSID\\")
  210. );
  211. lstrcat(szCLSID, szID);
  212. // Create entries under CLSID
  213. RegCreateKey(
  214. HKEY_LOCAL_MACHINE,
  215. szCLSID,
  216. &hKey1);
  217. RegSetValueEx(
  218. hKey1,
  219. NULL,
  220. 0,
  221. REG_SZ,
  222. (BYTE *)pName,
  223. lstrlen(pName)+1
  224. );
  225. RegCreateKey(
  226. hKey1,
  227. TEXT("InprocServer32"),
  228. &hKey2);
  229. GetModuleFileName(
  230. ghModule,
  231. szModule,
  232. MAX_PATH);
  233. RegSetValueEx(
  234. hKey2,
  235. NULL,
  236. 0,
  237. REG_SZ,
  238. (BYTE *)szModule,
  239. lstrlen(szModule)+1);
  240. RegSetValueEx(
  241. hKey2,
  242. TEXT("ThreadingModel"),
  243. 0,
  244. REG_SZ,
  245. (BYTE *)pModel, lstrlen(pModel)+1);
  246. CloseHandle(hKey1);
  247. CloseHandle(hKey2);
  248. //
  249. // Compile the MOF file. If this fails, it would be good to
  250. // tell the admin, but I don't have an easy way to do that.
  251. //
  252. CompileMofFile();
  253. return sc;
  254. }
  255. //***************************************************************************
  256. //
  257. // DllUnregisterServer
  258. //
  259. // Purpose: Called when it is time to remove the registry entries.
  260. //
  261. // Return: NOERROR if registration successful, error otherwise.
  262. //***************************************************************************
  263. STDAPI DllUnregisterServer(void)
  264. {
  265. TCHAR szID[128];
  266. WCHAR wcID[128];
  267. TCHAR szCLSID[128];
  268. HKEY hKey;
  269. // Create the path using the CLSID
  270. StringFromGUID2(CLSID_DNS_SERVER, wcID, 128);
  271. #ifndef UNICODE
  272. wcstombs(szID, wcID, 128);
  273. #else
  274. _tcscpy(szID, wcID);
  275. #endif
  276. lstrcpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  277. lstrcat(szCLSID, szID);
  278. // First delete the InProcServer subkey.
  279. DWORD dwRet = RegOpenKey(
  280. HKEY_LOCAL_MACHINE,
  281. szCLSID,
  282. &hKey);
  283. if(dwRet == NO_ERROR)
  284. {
  285. RegDeleteKey(
  286. hKey,
  287. TEXT("InProcServer32"));
  288. CloseHandle(hKey);
  289. }
  290. dwRet = RegOpenKey(
  291. HKEY_LOCAL_MACHINE,
  292. TEXT("Software\\classes\\CLSID"),
  293. &hKey);
  294. if(dwRet == NO_ERROR)
  295. {
  296. RegDeleteKey(
  297. hKey,
  298. szID);
  299. CloseHandle(hKey);
  300. }
  301. return NOERROR;
  302. }
  303. //***************************************************************************
  304. //
  305. // DllGetClassObject
  306. //
  307. // Purpose: Called by Ole when some client wants a class factory. Return
  308. // one only if it is the sort of class this DLL supports.
  309. //
  310. //***************************************************************************
  311. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  312. {
  313. DBG_FN( "DllGetClassObject" )
  314. HRESULT hr;
  315. CProvFactory *pObj = NULL;
  316. if ( CLSID_DNS_SERVER != rclsid )
  317. {
  318. hr = E_FAIL;
  319. goto Done;
  320. }
  321. pObj = new CProvFactory();
  322. if ( NULL == pObj )
  323. {
  324. hr = E_OUTOFMEMORY;
  325. goto Done;
  326. }
  327. hr = pObj->QueryInterface( riid, ppv );
  328. if ( FAILED( hr ) )
  329. {
  330. delete pObj;
  331. }
  332. Done:
  333. DNS_DEBUG( INIT, ( "%s: returning 0x%08x\n", fn, hr ));
  334. return hr;
  335. }