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.

965 lines
29 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WMI Framework Instance provider
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (C) 2000 Microsoft Corp.
  13. //
  14. //***************************************************************************
  15. #include "stdafx.h"
  16. #include <FWcommon.h>
  17. //#include <thrdbase.h>
  18. #include <objbase.h>
  19. #include <initguid.h>
  20. #include <regapi.h>
  21. #include <tchar.h>
  22. //#include "LogMsg.h"
  23. #include "trace.h"
  24. #include "terminal.h"
  25. #include "winstation.h"
  26. HINSTANCE g_hInstance = NULL;
  27. #ifdef UNICODE
  28. #pragma message("Its unicode")
  29. #else
  30. #pragma message("Its ansi")
  31. #endif
  32. //============
  33. // {39524FB7-028F-4035-8C2B-8E65D2B17E67}
  34. DEFINE_GUID(CLSID_CIM_WIN32_TSSESSIONDIRECTORY,
  35. 0x39524fb7, 0x28f, 0x4035, 0x8c, 0x2b, 0x8e, 0x65, 0xd2, 0xb1, 0x7e, 0x67);
  36. // {7970614A-BD82-439e-A828-CC96F8E91428}
  37. DEFINE_GUID(CLSID_CIM_WIN32_TERMINALSERVICE,
  38. 0x7970614a, 0xbd82, 0x439e, 0xa8, 0x28, 0xcc, 0x96, 0xf8, 0xe9, 0x14, 0x28);
  39. // {6BD6AECA-AFB0-45b7-BAC4-F292EC0F3F41}
  40. DEFINE_GUID(CLSID_CIM_WIN32_TERMINALSERVICESETTING,
  41. 0x6bd6aeca, 0xafb0, 0x45b7, 0xba, 0xc4, 0xf2, 0x92, 0xec, 0xf, 0x3f, 0x41);
  42. // {74628299-57EC-4f12-BA1C-08B477BF447A}
  43. DEFINE_GUID(CLSID_CIM_WIN32_TERMINAL,
  44. 0x74628299, 0x57ec, 0x4f12, 0xba, 0x1c, 0x8, 0xb4, 0x77, 0xbf, 0x44, 0x7a);
  45. // {4C8A0917-F587-4ecf-9C89-48147528F4E1}
  46. DEFINE_GUID(CLSID_CIM_WIN32_TSGENERALSETTING,
  47. 0x4c8a0917, 0xf587, 0x4ecf, 0x9c, 0x89, 0x48, 0x14, 0x75, 0x28, 0xf4, 0xe1);
  48. // {C41FF872-07B1-4926-819B-8C94E6B1FBB9}
  49. DEFINE_GUID(CLSID_CIM_WIN32_TSLOGONSETTING,
  50. 0xc41ff872, 0x7b1, 0x4926, 0x81, 0x9b, 0x8c, 0x94, 0xe6, 0xb1, 0xfb, 0xb9);
  51. // {BE9B3133-250B-44a5-A278-C532B7B7FF76}
  52. DEFINE_GUID(CLSID_CIM_WIN32_TSSESSIONSETTING,
  53. 0xbe9b3133, 0x250b, 0x44a5, 0xa2, 0x78, 0xc5, 0x32, 0xb7, 0xb7, 0xff, 0x76);
  54. // {9A17DFD1-34FA-4d61-B9BB-3A1097E7FADF}
  55. DEFINE_GUID(CLSID_CIM_WIN32_TSENVIRONMENTSETTING,
  56. 0x9a17dfd1, 0x34fa, 0x4d61, 0xb9, 0xbb, 0x3a, 0x10, 0x97, 0xe7, 0xfa, 0xdf);
  57. // {DBD71B6B-F717-4a61-A914-2337BC50B0D6}
  58. DEFINE_GUID(CLSID_CIM_WIN32_TSREMOTECONTROLSETTING,
  59. 0xdbd71b6b, 0xf717, 0x4a61, 0xa9, 0x14, 0x23, 0x37, 0xbc, 0x50, 0xb0, 0xd6);
  60. // {647BD8C2-658B-4eca-ABC7-FC5C0BF1704D}
  61. DEFINE_GUID(CLSID_CIM_WIN32_TSCLIENTSETTING,
  62. 0x647bd8c2, 0x658b, 0x4eca, 0xab, 0xc7, 0xfc, 0x5c, 0xb, 0xf1, 0x70, 0x4d);
  63. // {D25E0260-AAB7-48cb-A192-4D73D2FD375F}
  64. DEFINE_GUID(CLSID_CIM_WIN32_TSNETWORKADAPTERSETTING,
  65. 0xd25e0260, 0xaab7, 0x48cb, 0xa1, 0x92, 0x4d, 0x73, 0xd2, 0xfd, 0x37, 0x5f);
  66. // {FA06375D-F0A4-4a47-AD8D-148595F1E0B8}
  67. DEFINE_GUID(CLSID_CIM_WIN32_TSPERMISSIONSSETTING,
  68. 0xfa06375d, 0xf0a4, 0x4a47, 0xad, 0x8d, 0x14, 0x85, 0x95, 0xf1, 0xe0, 0xb8);
  69. // {7757BA9B-7986-4866-B53F-A31E89FCBA15}
  70. DEFINE_GUID(CLSID_CIM_WIN32_TSNETWORKADAPTERLISTSETTING,
  71. 0x7757ba9b, 0x7986, 0x4866, 0xb5, 0x3f, 0xa3, 0x1e, 0x89, 0xfc, 0xba, 0x15);
  72. // {BDF9E8B8-6C66-4e45-BA15-E050393DD079}
  73. DEFINE_GUID(CLSID_CIM_WIN32_TSACCOUNT,
  74. 0xbdf9e8b8, 0x6c66, 0x4e45, 0xba, 0x15, 0xe0, 0x50, 0x39, 0x3d, 0xd0, 0x79);
  75. // {B490264C-8D8F-40fd-B1BE-CD69AD779EC1}
  76. DEFINE_GUID(CLSID_CIM_WIN32_TERMINALSERVICETOSETTING,
  77. 0xb490264c, 0x8d8f, 0x40fd, 0xb1, 0xbe, 0xcd, 0x69, 0xad, 0x77, 0x9e, 0xc1);
  78. // {BA4EAC04-AFCE-441a-A4F0-C4A97545425A}
  79. DEFINE_GUID(CLSID_CIM_WIN32_TERMINALTERMINALSETTING,
  80. 0xba4eac04, 0xafce, 0x441a, 0xa4, 0xf0, 0xc4, 0xa9, 0x75, 0x45, 0x42, 0x5a);
  81. // {CCBA37FC-982B-433b-8AC2-455E616A8559}
  82. DEFINE_GUID(CLSID_CIM_WIN32_TSSESSIONDIRECTORYSETTING,
  83. 0xccba37fc, 0x982b, 0x433b, 0x8a, 0xc2, 0x45, 0x5e, 0x61, 0x6a, 0x85, 0x59);
  84. CRITICAL_SECTION g_critsect;
  85. CWin32_TerminalServiceSetting* g_pobj = NULL;
  86. CWin32_TerminalService* g_pTerminalServiceObj = NULL;
  87. CWin32_TSSessionDirectory* g_pTSSessionDirectoryObj = NULL;
  88. CWin32_Terminal* g_pTerminalObj = NULL;
  89. CWin32_TSGeneralSetting* g_pTSGeneralSettingObj = NULL;
  90. CWin32_TSLogonSetting* g_pTSLogonSettingObj = NULL;
  91. CWin32_TSSessionSetting* g_pTSSessionSettingObj = NULL;
  92. CWin32_TSEnvironmentSetting* g_pTSEnvironmentSettingObj = NULL;
  93. CWin32_TSRemoteControlSetting* g_pTSRemoteControlSettingObj = NULL;
  94. CWin32_TSClientSetting* g_pTSClientSettingObj = NULL;
  95. CWin32_TSNetworkAdapterSetting* g_pTSNetworkAdapterSettingObj = NULL;
  96. CWin32_TSPermissionsSetting* g_pTSPermissionsSettingObj = NULL;
  97. CWin32_TSNetworkAdapterListSetting* g_pTSNetworkAdapterListSettingObj = NULL;
  98. CWin32_TSAccount* g_pTSAccountObj = NULL;
  99. DEFINE_GUID(CLSID__CfgComp,0xBB0D7187,0x3C44,0x11D2,0xBB,0x98,0x30,0x78,0x30,0x2C,0x20,0x30);
  100. DEFINE_GUID(IID__ICfgComp,0xBB0D7186,0x3C44,0x11D2,0xBB,0x98,0x30,0x78,0x30,0x2C,0x20,0x30);
  101. //Count number of objects and number of locks.
  102. long g_cLock=0;
  103. /***************************************************************************
  104. * SetKeyAndValue
  105. *
  106. * Purpose:
  107. * Private helper function for DllRegisterServer that creates
  108. * a key, sets a value, and closes that key.
  109. *
  110. * Parameters:
  111. * pszKey LPTSTR to the ame of the key
  112. * pszSubkey LPTSTR ro the name of a subkey
  113. * pszValue LPTSTR to the value to store
  114. *
  115. * Return Value:
  116. * BOOL TRUE if successful, FALSE otherwise.
  117. ***************************************************************************/
  118. BOOL SetKeyAndValue (
  119. wchar_t *pszKey,
  120. wchar_t *pszSubkey,
  121. wchar_t *pszValueName,
  122. wchar_t *pszValue
  123. )
  124. {
  125. HKEY hKey;
  126. TCHAR szKey[MAX_PATH+1];
  127. if(lstrlen(pszKey) > MAX_PATH)
  128. {
  129. return FALSE;
  130. }
  131. lstrcpy(szKey, pszKey);
  132. if (NULL!=pszSubkey && (lstrlen(pszKey)+lstrlen(pszSubkey)+1) <= MAX_PATH )
  133. {
  134. lstrcat(szKey, TEXT("\\"));
  135. lstrcat(szKey, pszSubkey);
  136. }
  137. if (ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE
  138. , szKey, 0, NULL, REG_OPTION_NON_VOLATILE
  139. , KEY_ALL_ACCESS, NULL, &hKey, NULL))
  140. return FALSE;
  141. if (NULL!=pszValue)
  142. {
  143. if (ERROR_SUCCESS != RegSetValueEx(hKey, (LPCTSTR)pszValueName, 0, REG_SZ, (BYTE *)(LPCTSTR)pszValue
  144. , (_tcslen(pszValue)+1)*sizeof(TCHAR)))
  145. return FALSE;
  146. }
  147. RegCloseKey(hKey);
  148. return TRUE;
  149. }
  150. //***************************************************************************
  151. //
  152. // Is4OrMore
  153. //
  154. // Returns true if win95 or any version of NT > 3.51
  155. //
  156. //***************************************************************************
  157. BOOL Is4OrMore(void)
  158. {
  159. OSVERSIONINFO os;
  160. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  161. if(!GetVersionEx(&os))
  162. return FALSE; // should never happen
  163. return os.dwMajorVersion >= 4;
  164. }
  165. HRESULT RegisterServer (
  166. TCHAR *a_pName,
  167. REFGUID a_rguid
  168. )
  169. {
  170. WCHAR wcID[128];
  171. TCHAR szCLSID[128];
  172. TCHAR szModule[MAX_PATH];
  173. TCHAR * pName = TEXT("WBEM Framework Instance Provider");
  174. TCHAR * pModel;
  175. HKEY hKey1;
  176. GetModuleFileName(g_hInstance, szModule, MAX_PATH);
  177. // Normally we want to use "Both" as the threading model since
  178. // the DLL is free threaded, but NT 3.51 Ole doesnt work unless
  179. // the model is "Aparment"
  180. if(Is4OrMore())
  181. pModel = TEXT("Apartment") ;
  182. else
  183. pModel = TEXT("Apartment") ;
  184. // Create the path.
  185. StringFromGUID2(a_rguid, wcID, 128);
  186. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  187. lstrcat(szCLSID, wcID);
  188. #ifdef LOCALSERVER
  189. TCHAR szProviderCLSIDAppID[128];
  190. _tcscpy(szProviderCLSIDAppID,TEXT("SOFTWARE\\CLASSES\\APPID\\"));
  191. lstrcat(szProviderCLSIDAppID, wcID);
  192. if (FALSE ==SetKeyAndValue(szProviderCLSIDAppID, NULL, NULL, a_pName ))
  193. return SELFREG_E_CLASS;
  194. #endif
  195. // Create entries under CLSID
  196. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  197. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)a_pName, (lstrlen(a_pName)+1) *
  198. sizeof(TCHAR));
  199. #ifdef LOCALSERVER
  200. if (FALSE ==SetKeyAndValue(szCLSID, TEXT("LocalServer32"), NULL,szModule))
  201. return SELFREG_E_CLASS;
  202. if (FALSE ==SetKeyAndValue(szCLSID, TEXT("LocalServer32"),TEXT("ThreadingModel"), pModel))
  203. return SELFREG_E_CLASS;
  204. #else
  205. HKEY hKey2 ;
  206. RegCreateKey(hKey1, TEXT("InprocServer32"), &hKey2);
  207. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  208. (lstrlen(szModule)+1) * sizeof(TCHAR));
  209. RegSetValueEx(hKey2, TEXT("ThreadingModel"), 0, REG_SZ,
  210. (BYTE *)pModel, (lstrlen(pModel)+1) * sizeof(TCHAR));
  211. CloseHandle(hKey2);
  212. #endif
  213. CloseHandle(hKey1);
  214. return S_OK;
  215. }
  216. HRESULT UnregisterServer (
  217. REFGUID a_rguid
  218. )
  219. {
  220. TCHAR szID[128];
  221. WCHAR wcID[128];
  222. TCHAR szCLSID[128];
  223. HKEY hKey;
  224. // Create the path using the CLSID
  225. StringFromGUID2( a_rguid, wcID, 128);
  226. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  227. lstrcat(szCLSID, wcID);
  228. DWORD dwRet ;
  229. #ifdef LOCALSERVER
  230. TCHAR szProviderCLSIDAppID[128];
  231. _tcscpy(szProviderCLSIDAppID,TEXT("SOFTWARE\\CLASSES\\APPID\\"));
  232. _tcscat(szProviderCLSIDAppID,szCLSID);
  233. //Delete entries under APPID
  234. DWORD hrStatus = RegDeleteKey(HKEY_CLASSES_ROOT, szProviderCLSIDAppID);
  235. TCHAR szTemp[128];
  236. _stprintf(szTemp, TEXT("%s\\%s"),szCLSID, TEXT("LocalServer32"));
  237. hrStatus = RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  238. #else
  239. // First delete the InProcServer subkey.
  240. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  241. if(dwRet == NO_ERROR)
  242. {
  243. RegDeleteKey(hKey, TEXT("InProcServer32") );
  244. CloseHandle(hKey);
  245. }
  246. #endif
  247. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\CLASSES\\CLSID"), &hKey);
  248. if(dwRet == NO_ERROR)
  249. {
  250. RegDeleteKey(hKey,szID);
  251. CloseHandle(hKey);
  252. }
  253. else
  254. {
  255. ERR((TB,"UnregisterServer ret 0x%x\n", dwRet));
  256. }
  257. return HRESULT_FROM_WIN32( dwRet );
  258. }
  259. //***************************************************************************
  260. //
  261. // DllGetClassObject
  262. //
  263. // Purpose: Called by Ole when some client wants a class factory. Return
  264. // one only if it is the sort of class this DLL supports.
  265. //
  266. //***************************************************************************
  267. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  268. {
  269. HRESULT hr = S_OK;
  270. CWbemGlueFactory *pObj;
  271. if ( ( CLSID_CIM_WIN32_TERMINALSERVICESETTING == rclsid ) ||
  272. ( CLSID_CIM_WIN32_TERMINAL == rclsid ) || ( CLSID_CIM_WIN32_TSGENERALSETTING == rclsid ) ||
  273. ( CLSID_CIM_WIN32_TSLOGONSETTING == rclsid ) || ( CLSID_CIM_WIN32_TSSESSIONSETTING == rclsid ) ||
  274. ( CLSID_CIM_WIN32_TSENVIRONMENTSETTING == rclsid ) || ( CLSID_CIM_WIN32_TSCLIENTSETTING == rclsid ) ||
  275. ( CLSID_CIM_WIN32_TSNETWORKADAPTERSETTING == rclsid ) || ( CLSID_CIM_WIN32_TSPERMISSIONSSETTING == rclsid) ||
  276. ( CLSID_CIM_WIN32_TSNETWORKADAPTERLISTSETTING == rclsid ) || (CLSID_CIM_WIN32_TSACCOUNT == rclsid) ||
  277. ( CLSID_CIM_WIN32_TSREMOTECONTROLSETTING == rclsid ) || ( CLSID_CIM_WIN32_TERMINALSERVICE == rclsid ) ||
  278. ( CLSID_CIM_WIN32_TERMINALSERVICETOSETTING == rclsid ) || ( CLSID_CIM_WIN32_TERMINALTERMINALSETTING == rclsid) ||
  279. ( CLSID_CIM_WIN32_TSSESSIONDIRECTORYSETTING == rclsid ) || ( CLSID_CIM_WIN32_TSSESSIONDIRECTORY == rclsid ))
  280. {
  281. EnterCriticalSection(&g_critsect);
  282. try{
  283. pObj =new CWbemGlueFactory () ;
  284. if (NULL==pObj)
  285. {
  286. hr = E_OUTOFMEMORY;
  287. }
  288. else
  289. {
  290. hr=pObj->QueryInterface(riid, ppv);
  291. if (FAILED(hr))
  292. delete pObj;
  293. }
  294. if( SUCCEEDED(hr) )
  295. {
  296. // EnterCriticalSection prevents more than one threads from instantiating the global pointers to the objects.
  297. if( g_pobj == NULL )
  298. {
  299. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TerminalServiceSetting object created"));
  300. g_pobj = new CWin32_TerminalServiceSetting( PROVIDER_NAME_Win32_WIN32_TERMINALSERVICESETTING_Prov, L"root\\cimv2");
  301. }
  302. if( g_pTerminalServiceObj == NULL )
  303. {
  304. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TerminalService object created"));
  305. g_pTerminalServiceObj = new CWin32_TerminalService( PROVIDER_NAME_Win32_WIN32_TERMINALSERVICE_Prov, L"root\\cimv2");
  306. }
  307. if( g_pTSSessionDirectoryObj == NULL )
  308. {
  309. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSSessionDirectory object created"));
  310. g_pTSSessionDirectoryObj = new CWin32_TSSessionDirectory( PROVIDER_NAME_Win32_WIN32_TSSESSIONDIRECTORY_Prov, L"root\\cimv2");
  311. }
  312. if( g_pTerminalObj == NULL )
  313. {
  314. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_Terminal object created"));
  315. g_pTerminalObj = new CWin32_Terminal( PROVIDER_NAME_Win32_WIN32_TERMINAL_Prov, L"root\\cimv2");
  316. }
  317. if( g_pTSGeneralSettingObj == NULL )
  318. {
  319. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSGeneralSetting object created"));
  320. g_pTSGeneralSettingObj = new CWin32_TSGeneralSetting( PROVIDER_NAME_Win32_WIN32_TSGENERALSETTING_Prov, L"root\\cimv2");
  321. }
  322. if( g_pTSLogonSettingObj == NULL )
  323. {
  324. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSLogonSetting object created"));
  325. g_pTSLogonSettingObj = new CWin32_TSLogonSetting( PROVIDER_NAME_Win32_WIN32_TSLOGONSETTING_Prov, L"root\\cimv2");
  326. }
  327. if( g_pTSSessionSettingObj == NULL )
  328. {
  329. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSSessionSetting object created"));
  330. g_pTSSessionSettingObj = new CWin32_TSSessionSetting( PROVIDER_NAME_Win32_WIN32_TSSESSIONSETTING_Prov, L"root\\cimv2");
  331. }
  332. if( g_pTSEnvironmentSettingObj == NULL )
  333. {
  334. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSEnvironmentSetting object created"));
  335. g_pTSEnvironmentSettingObj = new CWin32_TSEnvironmentSetting( PROVIDER_NAME_Win32_WIN32_TSENVIRONMENTSETTING_Prov, L"root\\cimv2");
  336. }
  337. if( g_pTSRemoteControlSettingObj == NULL )
  338. {
  339. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TRemoteControlSetting object created"));
  340. g_pTSRemoteControlSettingObj = new CWin32_TSRemoteControlSetting( PROVIDER_NAME_Win32_WIN32_TSREMOTECONTROLSETTING_Prov, L"root\\cimv2");
  341. }
  342. if( g_pTSClientSettingObj == NULL )
  343. {
  344. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSClientSetting object created"));
  345. g_pTSClientSettingObj = new CWin32_TSClientSetting( PROVIDER_NAME_Win32_WIN32_TSCLIENTSETTING_Prov, L"root\\cimv2");
  346. }
  347. if( g_pTSNetworkAdapterSettingObj == NULL )
  348. {
  349. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSNetworkAdapterSetting object created"));
  350. g_pTSNetworkAdapterSettingObj = new CWin32_TSNetworkAdapterSetting( PROVIDER_NAME_Win32_WIN32_TSNETWORKADAPTERSETTING_Prov, L"root\\cimv2");
  351. }
  352. if( g_pTSPermissionsSettingObj == NULL )
  353. {
  354. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSPermissionsSetting object created"));
  355. g_pTSPermissionsSettingObj = new CWin32_TSPermissionsSetting( PROVIDER_NAME_Win32_WIN32_TSPERMISSIONSSETTING_Prov, L"root\\cimv2");
  356. }
  357. if( g_pTSNetworkAdapterListSettingObj == NULL )
  358. {
  359. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSNetworkAdapterListSetting object created"));
  360. g_pTSNetworkAdapterListSettingObj = new CWin32_TSNetworkAdapterListSetting( PROVIDER_NAME_Win32_WIN32_TSNETWORKADAPTERLISTSETTING_Prov, L"root\\cimv2");
  361. }
  362. if( g_pTSAccountObj == NULL )
  363. {
  364. TRC2((TB, "DllMain DLL_PROCESS_ATTACH: CWin32_TSAccount object created"));
  365. g_pTSAccountObj = new CWin32_TSAccount( PROVIDER_NAME_Win32_WIN32_TSACCOUNT_Prov, L"root\\cimv2");
  366. }
  367. }
  368. }
  369. catch (...)
  370. {
  371. hr = E_OUTOFMEMORY;
  372. }
  373. LeaveCriticalSection(&g_critsect);
  374. }
  375. else
  376. {
  377. hr=E_FAIL;
  378. ERR((TB, "DllGetClassObject ret 0x%x\n" , hr));
  379. }
  380. return hr;
  381. }
  382. //***************************************************************************
  383. //
  384. // DllCanUnloadNow
  385. //
  386. // Purpose: Called periodically by Ole in order to determine if the
  387. // DLL can be freed.
  388. //
  389. // Return: S_OK if there are no objects in use and the class factory
  390. // isn't locked.
  391. //
  392. //***************************************************************************
  393. STDAPI DllCanUnloadNow(void)
  394. {
  395. SCODE sc;
  396. // It is OK to unload if there are no objects or locks on the
  397. // class factory and the framework is done with you.
  398. if ((0L==g_cLock) && CWbemProviderGlue::FrameworkLogoffDLL(L"TSCFGWMI"))
  399. {
  400. // EnterCriticalSection prevents multiple threads from accessing the global pointers concurrently and
  401. // allows only one thread access to free the objects based on the condition that g_cLock count is zero
  402. // and FrameworkLogoffDLL is TRUE.
  403. EnterCriticalSection(&g_critsect);
  404. if( g_pobj != NULL )
  405. {
  406. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TerminalServiceSetting object deleted"));
  407. delete g_pobj;
  408. g_pobj = NULL;
  409. }
  410. if( g_pTerminalServiceObj != NULL )
  411. {
  412. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TerminalService object deleted"));
  413. delete g_pTerminalServiceObj;
  414. g_pTerminalServiceObj = NULL;
  415. }
  416. if( g_pTSSessionDirectoryObj != NULL )
  417. {
  418. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSSessionDirectory object deleted"));
  419. delete g_pTSSessionDirectoryObj;
  420. g_pTSSessionDirectoryObj = NULL;
  421. }
  422. if( g_pTerminalObj != NULL )
  423. {
  424. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_Terminal object deleted"));
  425. delete g_pTerminalObj;
  426. g_pTerminalObj = NULL;
  427. }
  428. if( g_pTSGeneralSettingObj != NULL )
  429. {
  430. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSGeneralSetting object deleted"));
  431. delete g_pTSGeneralSettingObj;
  432. g_pTSGeneralSettingObj = NULL;
  433. }
  434. if( g_pTSLogonSettingObj != NULL )
  435. {
  436. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSLogonSetting object deleted"));
  437. delete g_pTSLogonSettingObj;
  438. g_pTSLogonSettingObj = NULL;
  439. }
  440. if( g_pTSSessionSettingObj != NULL )
  441. {
  442. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSSessionSetting object deleted"));
  443. delete g_pTSSessionSettingObj;
  444. g_pTSSessionSettingObj = NULL;
  445. }
  446. if( g_pTSEnvironmentSettingObj != NULL )
  447. {
  448. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSEnvironmentSetting object deleted"));
  449. delete g_pTSEnvironmentSettingObj;
  450. g_pTSEnvironmentSettingObj = NULL;
  451. }
  452. if( g_pTSRemoteControlSettingObj != NULL )
  453. {
  454. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSRemoteControlSetting object deleted"));
  455. delete g_pTSRemoteControlSettingObj;
  456. g_pTSRemoteControlSettingObj = NULL;
  457. }
  458. if( g_pTSClientSettingObj != NULL )
  459. {
  460. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSClientSetting object deleted"));
  461. delete g_pTSClientSettingObj;
  462. g_pTSClientSettingObj = NULL;
  463. }
  464. if( g_pTSNetworkAdapterSettingObj != NULL )
  465. {
  466. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSNetworkAdapterSetting object deleted"));
  467. delete g_pTSNetworkAdapterSettingObj;
  468. g_pTSNetworkAdapterSettingObj = NULL;
  469. }
  470. if( g_pTSPermissionsSettingObj != NULL )
  471. {
  472. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSPermissionsSetting object deleted"));
  473. delete g_pTSPermissionsSettingObj;
  474. g_pTSPermissionsSettingObj = NULL;
  475. }
  476. if( g_pTSNetworkAdapterListSettingObj != NULL )
  477. {
  478. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSNetworkAdapterListSetting object deleted"));
  479. delete g_pTSNetworkAdapterListSettingObj;
  480. g_pTSNetworkAdapterListSettingObj = NULL;
  481. }
  482. if( g_pTSAccountObj != NULL )
  483. {
  484. TRC2((TB, "DllMain DLL_PROCESS_DETACH: CWin32_TSAccount object deleted"));
  485. delete g_pTSAccountObj;
  486. g_pTSAccountObj = NULL;
  487. }
  488. // LeaveCriticalSection releases the critical section once the thread has freed all objects.
  489. LeaveCriticalSection(&g_critsect);
  490. sc = S_OK;
  491. }
  492. else
  493. {
  494. sc = S_FALSE;
  495. // ERR((TB, "DllCanUnloadNow ret 0x%x\n" , sc));
  496. }
  497. return sc;
  498. }
  499. //***************************************************************************
  500. //
  501. // DllRegisterServer
  502. //
  503. // Purpose: Called during setup or by regsvr32.
  504. //
  505. // Return: NOERROR if registration successful, error otherwise.
  506. //***************************************************************************
  507. STDAPI DllRegisterServer(void)
  508. {
  509. HRESULT hrStatus;
  510. hrStatus = RegisterServer( TEXT("WBEM Win32_TERMINALSERVICESETTING Provider"), CLSID_CIM_WIN32_TERMINALSERVICESETTING ) ;
  511. if( SUCCEEDED( hrStatus ) )
  512. {
  513. TRC2((TB,"RegisterServer Win32_TERMINALSERVICESETTING: succeeded"));
  514. hrStatus = RegisterServer( TEXT("WBEM WIN32_TERMINAL Provider"), CLSID_CIM_WIN32_TERMINAL ) ;
  515. }
  516. if( SUCCEEDED( hrStatus ) )
  517. {
  518. TRC2((TB,"RegisterServer Win32_TERMINAL: succeeded"));
  519. hrStatus = RegisterServer( TEXT("WBEM WIN32_TERMINALSERVICE Provider"), CLSID_CIM_WIN32_TERMINALSERVICE ) ;
  520. }
  521. if( SUCCEEDED( hrStatus ) )
  522. {
  523. TRC2((TB,"RegisterServer Win32_TERMINALSERVICE: succeeded"));
  524. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSSESSIONDIRECTORY Provider"), CLSID_CIM_WIN32_TSSESSIONDIRECTORY ) ;
  525. }
  526. if( SUCCEEDED( hrStatus ) )
  527. {
  528. TRC2((TB,"RegisterServer Win32_TSSESSIONDIRECTORY: succeeded"));
  529. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSGENERALSETTING Provider"), CLSID_CIM_WIN32_TSGENERALSETTING ) ;
  530. }
  531. if( SUCCEEDED( hrStatus ) )
  532. {
  533. TRC2((TB,"RegisterServer Win32_TSGENERALSETTING: succeeded"));
  534. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSLOGONSETTING Provider"), CLSID_CIM_WIN32_TSLOGONSETTING ) ;
  535. }
  536. if( SUCCEEDED( hrStatus ) )
  537. {
  538. TRC2((TB,"RegisterServer Win32_TSLOGONSETTING: succeeded"));
  539. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSSESSIONSETTING Provider"), CLSID_CIM_WIN32_TSSESSIONSETTING ) ;
  540. }
  541. if( SUCCEEDED( hrStatus ) )
  542. {
  543. TRC2((TB,"RegisterServer Win32_TSSESSIONSETTING: succeeded"));
  544. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSENVIRONMENTSETTING Provider"), CLSID_CIM_WIN32_TSENVIRONMENTSETTING ) ;
  545. }
  546. if( SUCCEEDED( hrStatus ) )
  547. {
  548. TRC2((TB,"RegisterServer Win32_TSENVIRONMENTSETTING: succeeded"));
  549. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSREMOTECONTROLSETTING Provider"), CLSID_CIM_WIN32_TSREMOTECONTROLSETTING ) ;
  550. }
  551. if( SUCCEEDED( hrStatus ) )
  552. {
  553. TRC2((TB,"RegisterServer Win32_TSREMOTECONTROLSETTING: succeeded"));
  554. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSCLIENTSETTING Provider"), CLSID_CIM_WIN32_TSCLIENTSETTING ) ;
  555. }
  556. if( SUCCEEDED( hrStatus ) )
  557. {
  558. TRC2((TB,"RegisterServer Win32_TSCLIENTSETTING: succeeded"));
  559. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSNETWORKADAPTERSETTING Provider"), CLSID_CIM_WIN32_TSNETWORKADAPTERSETTING ) ;
  560. }
  561. if( SUCCEEDED( hrStatus ) )
  562. {
  563. TRC2((TB,"RegisterServer Win32_TSNETWORKADAPTERSETTING: succeeded"));
  564. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSPERMISSIONSSETTING Provider"), CLSID_CIM_WIN32_TSPERMISSIONSSETTING ) ;
  565. }
  566. if( SUCCEEDED( hrStatus ) )
  567. {
  568. TRC2((TB,"RegisterServer Win32_TSPERMISSIONSSETTING: succeeded"));
  569. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSNETWORKADAPTERLISTSETTING Provider"), CLSID_CIM_WIN32_TSNETWORKADAPTERLISTSETTING ) ;
  570. }
  571. if( SUCCEEDED( hrStatus ) )
  572. {
  573. TRC2((TB,"RegisterServer Win32_TSNETWORKADAPTERLISTSETTINGS: succeeded"));
  574. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSACCOUNT Provider"), CLSID_CIM_WIN32_TSACCOUNT ) ;
  575. }
  576. if( SUCCEEDED( hrStatus ) )
  577. {
  578. TRC2((TB,"RegisterServer Win32_TSACCOUNT: succeeded"));
  579. hrStatus = RegisterServer( TEXT("WBEM WIN32_TERMINALSERVICETOSETTING Provider"), CLSID_CIM_WIN32_TERMINALSERVICETOSETTING ) ;
  580. }
  581. if (SUCCEEDED( hrStatus ) )
  582. {
  583. TRC2((TB, "RegisterServer Win32_TERMINALSERVICETOSETTING: succeeded"));
  584. hrStatus = RegisterServer( TEXT("WBEM WIN32_TERMINALTERMINALSETTING Provider"), CLSID_CIM_WIN32_TERMINALTERMINALSETTING ) ;
  585. }
  586. if (SUCCEEDED( hrStatus ) )
  587. {
  588. TRC2((TB, "RegisterServer Win32_TERMINALTERMINALSETTING: succeeded"));
  589. hrStatus = RegisterServer( TEXT("WBEM WIN32_TSSESSIONDIRECTORYSETTING Provider"), CLSID_CIM_WIN32_TSSESSIONDIRECTORYSETTING ) ;
  590. }
  591. if (SUCCEEDED( hrStatus ) )
  592. {
  593. TRC2((TB, "RegisterServer Win32_TSSESSIONDIRECTORYSETTING: succeeded"));
  594. }
  595. return hrStatus;
  596. }
  597. //***************************************************************************
  598. //
  599. // DllUnregisterServer
  600. //
  601. // Purpose: Called when it is time to remove the registry entries.
  602. //
  603. // Return: NOERROR if registration successful, error otherwise.
  604. //***************************************************************************
  605. STDAPI DllUnregisterServer(void)
  606. {
  607. UnregisterServer( CLSID_CIM_WIN32_TERMINALSERVICE );
  608. UnregisterServer( CLSID_CIM_WIN32_TSSESSIONDIRECTORY ) ;
  609. UnregisterServer( CLSID_CIM_WIN32_TERMINALSERVICESETTING ) ;
  610. UnregisterServer( CLSID_CIM_WIN32_TERMINAL ) ;
  611. UnregisterServer( CLSID_CIM_WIN32_TSGENERALSETTING ) ;
  612. UnregisterServer( CLSID_CIM_WIN32_TSLOGONSETTING ) ;
  613. UnregisterServer( CLSID_CIM_WIN32_TSSESSIONSETTING ) ;
  614. UnregisterServer( CLSID_CIM_WIN32_TSENVIRONMENTSETTING ) ;
  615. UnregisterServer( CLSID_CIM_WIN32_TSREMOTECONTROLSETTING ) ;
  616. UnregisterServer( CLSID_CIM_WIN32_TSCLIENTSETTING ) ;
  617. UnregisterServer( CLSID_CIM_WIN32_TSNETWORKADAPTERSETTING ) ;
  618. UnregisterServer( CLSID_CIM_WIN32_TSPERMISSIONSSETTING ) ;
  619. UnregisterServer( CLSID_CIM_WIN32_TSNETWORKADAPTERLISTSETTING ) ;
  620. UnregisterServer( CLSID_CIM_WIN32_TSACCOUNT ) ;
  621. UnregisterServer( CLSID_CIM_WIN32_TERMINALSERVICETOSETTING ) ;
  622. UnregisterServer( CLSID_CIM_WIN32_TERMINALTERMINALSETTING ) ;
  623. UnregisterServer( CLSID_CIM_WIN32_TSSESSIONDIRECTORYSETTING ) ;
  624. return S_OK;
  625. }
  626. //***************************************************************************
  627. //
  628. // DllMain
  629. //
  630. // Purpose: Called by the operating system when processes and threads are
  631. // initialized and terminated, or upon calls to the LoadLibrary
  632. // and FreeLibrary functions
  633. //
  634. // Return: TRUE if load was successful, else FALSE
  635. //***************************************************************************
  636. BOOL APIENTRY DllMain ( HINSTANCE hInstDLL, // handle to dll module
  637. DWORD fdwReason, // reason for calling function
  638. LPVOID lpReserved ) // reserved
  639. {
  640. BOOL bRet = TRUE;
  641. // Perform actions based on the reason for calling.
  642. if( DLL_PROCESS_ATTACH == fdwReason )
  643. {
  644. DisableThreadLibraryCalls(hInstDLL);
  645. // CriticalSection object is initialized on Thread attach.
  646. __try
  647. {
  648. InitializeCriticalSection(&g_critsect);
  649. }
  650. __except (EXCEPTION_EXECUTE_HANDLER)
  651. {
  652. return E_FAIL;
  653. }
  654. g_hInstance = hInstDLL ;
  655. bRet = CWbemProviderGlue :: FrameworkLoginDLL ( L"TSCFGWMI" ) ;
  656. }
  657. else if( DLL_PROCESS_DETACH == fdwReason )
  658. {
  659. // CriticalSection object is deleted
  660. DeleteCriticalSection(&g_critsect);
  661. }
  662. return bRet; // Status of DLL_PROCESS_ATTACH.
  663. }