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.

309 lines
6.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: domain.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include <atlimpl.cpp> // include only once ont in PCH
  12. // this redefines the DEFINE_GUID() macro to do allocation.
  13. #include "initguid.h"
  14. #include <dsclient.h>
  15. #include <dsadminp.h>
  16. #include "resource.h"
  17. #include "domain.h"
  18. #include "domobj.h"
  19. #include "cdomain.h"
  20. HRESULT WINAPI CDomainAdminModule::UpdateRegistryCLSID(const CLSID& clsid, BOOL bRegister)
  21. {
  22. static const WCHAR szIPS32[] = _T("InprocServer32");
  23. static const WCHAR szCLSID[] = _T("CLSID");
  24. HRESULT hRes = S_OK;
  25. LPOLESTR lpOleStrCLSIDValue;
  26. ::StringFromCLSID(clsid, &lpOleStrCLSIDValue);
  27. CRegKey key;
  28. if (bRegister)
  29. {
  30. LONG lRes = key.Open(HKEY_CLASSES_ROOT, szCLSID);
  31. if (lRes == ERROR_SUCCESS)
  32. {
  33. lRes = key.Create(key, lpOleStrCLSIDValue);
  34. if (lRes == ERROR_SUCCESS)
  35. {
  36. WCHAR szModule[_MAX_PATH];
  37. ::GetModuleFileName(m_hInst, szModule, _MAX_PATH);
  38. key.SetKeyValue(szIPS32, szModule);
  39. }
  40. }
  41. if (lRes != ERROR_SUCCESS)
  42. hRes = HRESULT_FROM_WIN32(lRes);
  43. }
  44. else
  45. {
  46. key.Attach(HKEY_CLASSES_ROOT);
  47. if (key.Open(key, szCLSID) == ERROR_SUCCESS)
  48. key.RecurseDeleteKey(lpOleStrCLSIDValue);
  49. }
  50. ::CoTaskMemFree(lpOleStrCLSIDValue);
  51. return hRes;
  52. }
  53. CDomainAdminModule _Module;
  54. BEGIN_OBJECT_MAP(ObjectMap)
  55. OBJECT_ENTRY(CLSID_DomainAdmin, CComponentDataImpl)
  56. OBJECT_ENTRY(CLSID_DomainSnapinAbout, CDomainSnapinAbout)
  57. END_OBJECT_MAP()
  58. #ifdef _DEBUG
  59. #define new DEBUG_NEW
  60. #undef THIS_FILE
  61. static char THIS_FILE[] = __FILE__;
  62. #endif
  63. class CDomainApp : public CWinApp
  64. {
  65. public:
  66. virtual BOOL InitInstance();
  67. virtual int ExitInstance();
  68. };
  69. CDomainApp theApp;
  70. BOOL CDomainApp::InitInstance()
  71. {
  72. _Module.Init(ObjectMap, m_hInstance);
  73. return CWinApp::InitInstance();
  74. }
  75. int CDomainApp::ExitInstance()
  76. {
  77. _Module.Term();
  78. DEBUG_VERIFY_INSTANCE_COUNT(CComponentImpl);
  79. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  80. return CWinApp::ExitInstance();
  81. }
  82. /////////////////////////////////////////////////////////////////////////////
  83. // Used to determine whether the DLL can be unloaded by OLE
  84. STDAPI DllCanUnloadNow(void)
  85. {
  86. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  87. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // Returns a class factory to create an object of the requested type
  91. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  92. {
  93. return _Module.GetClassObject(rclsid, riid, ppv);
  94. }
  95. LPCTSTR g_cszBasePath = _T("Software\\Microsoft\\MMC\\SnapIns");
  96. LPCTSTR g_cszNameString = _T("NameString");
  97. LPCTSTR g_cszNameStringIndirect = _T("NameStringIndirect");
  98. LPCTSTR g_cszProvider = _T("Provider");
  99. LPCTSTR g_cszVersion = _T("Version");
  100. LPCTSTR g_cszAbout = _T("About");
  101. LPCTSTR g_cszStandAlone = _T("StandAlone");
  102. LPCTSTR g_cszNodeTypes = _T("NodeTypes");
  103. LPCTSTR GUIDToCString(REFGUID guid, CString & str)
  104. {
  105. USES_CONVERSION;
  106. OLECHAR lpszGUID[128];
  107. int nChars = ::StringFromGUID2(guid, lpszGUID, 128);
  108. LPTSTR lpString = OLE2T(lpszGUID);
  109. LPTSTR lpGUID = str.GetBuffer(nChars);
  110. if (lpGUID)
  111. {
  112. CopyMemory(lpGUID, lpString, nChars*sizeof(TCHAR));
  113. str.ReleaseBuffer();
  114. }
  115. return str;
  116. }
  117. HRESULT RegisterSnapin()
  118. {
  119. HRESULT hr = S_OK;
  120. CString strKey;
  121. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  122. try
  123. {
  124. CString str;
  125. CRegKey rkBase;
  126. rkBase.Open(HKEY_LOCAL_MACHINE, g_cszBasePath);
  127. // Create snapin GUID key and set properties
  128. CRegKey rkCLSID;
  129. rkCLSID.Create(rkBase, GUIDToCString(CLSID_DomainAdmin, str));
  130. str.LoadString(IDS_DESCRIPTION);
  131. // str = _T("MSFT Domain Tree Manager");
  132. rkCLSID.SetValue(str, g_cszNameString);
  133. {
  134. WCHAR szModule[_MAX_PATH];
  135. ::GetModuleFileName(AfxGetInstanceHandle(), szModule, _MAX_PATH);
  136. str.Format(_T("@%s,-%d"), szModule, IDS_DESCRIPTION);
  137. rkCLSID.SetValue(str, g_cszNameStringIndirect);
  138. }
  139. str.LoadString(IDS_COMPANY);
  140. rkCLSID.SetValue(str, g_cszProvider);
  141. str.LoadString(IDS_SNAPIN_VERSION);
  142. rkCLSID.SetValue((LPCWSTR)str, g_cszVersion);
  143. rkCLSID.SetValue(GUIDToCString(CLSID_DomainSnapinAbout, str), g_cszAbout);
  144. // Create "StandAlone" key
  145. CRegKey rkStandAlone;
  146. rkStandAlone.Create(rkCLSID, g_cszStandAlone);
  147. // Create "NodeTypes" key
  148. CRegKey rkNodeTypes;
  149. rkNodeTypes.Create(rkCLSID, g_cszNodeTypes);
  150. // NodeTypes guids
  151. CRegKey rkN1;
  152. rkN1.Create(rkNodeTypes, GUIDToCString(cDefaultNodeType, str));
  153. }
  154. catch(CMemoryException * e)
  155. {
  156. e->Delete();
  157. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  158. }
  159. catch(COleException * e)
  160. {
  161. e->Delete();
  162. hr = SELFREG_E_CLASS;
  163. }
  164. return hr;
  165. }
  166. HRESULT UnregisterSnapin()
  167. {
  168. HRESULT hr = S_OK;
  169. try
  170. {
  171. CRegKey rkBase;
  172. rkBase.Open(HKEY_LOCAL_MACHINE, g_cszBasePath);
  173. CString str;
  174. rkBase.RecurseDeleteKey(GUIDToCString(CLSID_DomainAdmin, str));
  175. }
  176. catch(CException * e)
  177. {
  178. DWORD err = ::GetLastError();
  179. hr = HRESULT_FROM_WIN32(err);
  180. e->Delete();
  181. }
  182. return hr;
  183. }
  184. /////////////////////////////////////////////////////////////////////////////
  185. // DllRegisterServer - Adds entries to the system registry
  186. STDAPI DllRegisterServer(void)
  187. {
  188. HRESULT hRes = _Module.RegisterServer(/* bRegTypeLib */ FALSE);
  189. if (FAILED(hRes))
  190. return SELFREG_E_CLASS;
  191. return RegisterSnapin();
  192. }
  193. /////////////////////////////////////////////////////////////////////////////
  194. // DllUnregisterServer - Removes entries from the system registry
  195. STDAPI DllUnregisterServer(void)
  196. {
  197. _Module.UnregisterServer();
  198. return UnregisterSnapin();
  199. }
  200. ///////////////////////////////////////////////////////////////////////
  201. // CDsUiWizDLL
  202. CDsUiWizDLL::CDsUiWizDLL()
  203. {
  204. m_hLibrary = NULL;
  205. m_pfFunction = NULL;
  206. }
  207. CDsUiWizDLL::~CDsUiWizDLL()
  208. {
  209. if (m_hLibrary != NULL)
  210. {
  211. ::FreeLibrary(m_hLibrary);
  212. m_hLibrary = NULL;
  213. }
  214. }
  215. BOOL CDsUiWizDLL::Load()
  216. {
  217. if (m_hLibrary != NULL)
  218. return TRUE; // already loaded
  219. m_hLibrary = ::LoadLibrary(_T("dsuiwiz.dll"));
  220. if (NULL == m_hLibrary)
  221. {
  222. // The library is not present
  223. return FALSE;
  224. }
  225. m_pfFunction = ::GetProcAddress(m_hLibrary, "TrustWizard" );
  226. if ( NULL == m_pfFunction )
  227. {
  228. // The library is present but does not have the entry point
  229. ::FreeLibrary( m_hLibrary );
  230. m_hLibrary = NULL;
  231. return FALSE;
  232. }
  233. ASSERT(m_hLibrary != NULL);
  234. ASSERT(m_pfFunction != NULL);
  235. return TRUE;
  236. }
  237. typedef HRESULT (*TRUST_WIZARD_PROC) (HWND, LPCWSTR);
  238. HRESULT CDsUiWizDLL::TrustWizard(HWND hWndParent, LPCWSTR lpsz)
  239. {
  240. ASSERT(m_hLibrary != NULL);
  241. ASSERT(m_pfFunction != NULL);
  242. return ((TRUST_WIZARD_PROC)m_pfFunction) (hWndParent, lpsz);
  243. }