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.

313 lines
7.6 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+1] = {0};
  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. SHFusionInitializeFromModule(m_hInstance);
  74. return CWinApp::InitInstance();
  75. }
  76. int CDomainApp::ExitInstance()
  77. {
  78. _Module.Term();
  79. DEBUG_VERIFY_INSTANCE_COUNT(CComponentImpl);
  80. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  81. SHFusionUninitialize();
  82. return CWinApp::ExitInstance();
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Used to determine whether the DLL can be unloaded by OLE
  86. STDAPI DllCanUnloadNow(void)
  87. {
  88. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  89. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Returns a class factory to create an object of the requested type
  93. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  94. {
  95. return _Module.GetClassObject(rclsid, riid, ppv);
  96. }
  97. LPCTSTR g_cszBasePath = _T("Software\\Microsoft\\MMC\\SnapIns");
  98. LPCTSTR g_cszNameString = _T("NameString");
  99. LPCTSTR g_cszNameStringIndirect = _T("NameStringIndirect");
  100. LPCTSTR g_cszProvider = _T("Provider");
  101. LPCTSTR g_cszVersion = _T("Version");
  102. LPCTSTR g_cszAbout = _T("About");
  103. LPCTSTR g_cszStandAlone = _T("StandAlone");
  104. LPCTSTR g_cszNodeTypes = _T("NodeTypes");
  105. LPCTSTR GUIDToCString(REFGUID guid, CString & str)
  106. {
  107. USES_CONVERSION;
  108. OLECHAR lpszGUID[128];
  109. int nChars = ::StringFromGUID2(guid, lpszGUID, 128);
  110. LPTSTR lpString = OLE2T(lpszGUID);
  111. LPTSTR lpGUID = str.GetBuffer(nChars);
  112. if (lpGUID)
  113. {
  114. // NOTICE-2002/03/07-ericb - SecurityPush: reviewed safe.
  115. CopyMemory(lpGUID, lpString, nChars*sizeof(TCHAR));
  116. str.ReleaseBuffer();
  117. }
  118. return str;
  119. }
  120. HRESULT RegisterSnapin()
  121. {
  122. HRESULT hr = S_OK;
  123. CString strKey;
  124. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  125. try
  126. {
  127. CString str;
  128. CRegKey rkBase;
  129. rkBase.Open(HKEY_LOCAL_MACHINE, g_cszBasePath);
  130. // Create snapin GUID key and set properties
  131. CRegKey rkCLSID;
  132. rkCLSID.Create(rkBase, GUIDToCString(CLSID_DomainAdmin, str));
  133. str.LoadString(IDS_DESCRIPTION);
  134. // str = _T("MSFT Domain Tree Manager");
  135. rkCLSID.SetValue(str, g_cszNameString);
  136. {
  137. WCHAR szModule[_MAX_PATH+1] = {0};
  138. ::GetModuleFileName(AfxGetInstanceHandle(), szModule, _MAX_PATH);
  139. str.Format(_T("@%s,-%d"), szModule, IDS_DESCRIPTION);
  140. rkCLSID.SetValue(str, g_cszNameStringIndirect);
  141. }
  142. rkCLSID.SetValue(STR_SNAPIN_COMPANY, g_cszProvider);
  143. str.Format(TEXT("%hs"), STR_SNAPIN_VERSION); // this is a concatenation of ANSI strings, hence this conversion to UNICODE.
  144. rkCLSID.SetValue(str, g_cszVersion);
  145. rkCLSID.SetValue(GUIDToCString(CLSID_DomainSnapinAbout, str), g_cszAbout);
  146. // Create "StandAlone" key
  147. CRegKey rkStandAlone;
  148. rkStandAlone.Create(rkCLSID, g_cszStandAlone);
  149. // Create "NodeTypes" key
  150. CRegKey rkNodeTypes;
  151. rkNodeTypes.Create(rkCLSID, g_cszNodeTypes);
  152. // NodeTypes guids
  153. CRegKey rkN1;
  154. rkN1.Create(rkNodeTypes, GUIDToCString(cDefaultNodeType, str));
  155. }
  156. catch(CMemoryException * e)
  157. {
  158. e->Delete();
  159. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  160. }
  161. catch(COleException * e)
  162. {
  163. e->Delete();
  164. hr = SELFREG_E_CLASS;
  165. }
  166. return hr;
  167. }
  168. HRESULT UnregisterSnapin()
  169. {
  170. HRESULT hr = S_OK;
  171. try
  172. {
  173. CRegKey rkBase;
  174. rkBase.Open(HKEY_LOCAL_MACHINE, g_cszBasePath);
  175. CString str;
  176. rkBase.RecurseDeleteKey(GUIDToCString(CLSID_DomainAdmin, str));
  177. }
  178. catch(CException * e)
  179. {
  180. DWORD err = ::GetLastError();
  181. hr = HRESULT_FROM_WIN32(err);
  182. e->Delete();
  183. }
  184. return hr;
  185. }
  186. /////////////////////////////////////////////////////////////////////////////
  187. // DllRegisterServer - Adds entries to the system registry
  188. STDAPI DllRegisterServer(void)
  189. {
  190. HRESULT hRes = _Module.RegisterServer(/* bRegTypeLib */ FALSE);
  191. if (FAILED(hRes))
  192. return SELFREG_E_CLASS;
  193. return RegisterSnapin();
  194. }
  195. /////////////////////////////////////////////////////////////////////////////
  196. // DllUnregisterServer - Removes entries from the system registry
  197. STDAPI DllUnregisterServer(void)
  198. {
  199. _Module.UnregisterServer();
  200. return UnregisterSnapin();
  201. }
  202. ///////////////////////////////////////////////////////////////////////
  203. // CDsUiWizDLL
  204. CDsUiWizDLL::CDsUiWizDLL()
  205. {
  206. m_hLibrary = NULL;
  207. m_pfFunction = NULL;
  208. }
  209. CDsUiWizDLL::~CDsUiWizDLL()
  210. {
  211. if (m_hLibrary != NULL)
  212. {
  213. ::FreeLibrary(m_hLibrary);
  214. m_hLibrary = NULL;
  215. }
  216. }
  217. BOOL CDsUiWizDLL::Load()
  218. {
  219. if (m_hLibrary != NULL)
  220. return TRUE; // already loaded
  221. // NOTICE-2002/03/07-ericb - SecurityPush: reviewed safe.
  222. m_hLibrary = ::LoadLibrary(_T("dsuiwiz.dll"));
  223. if (NULL == m_hLibrary)
  224. {
  225. // The library is not present
  226. return FALSE;
  227. }
  228. m_pfFunction = ::GetProcAddress(m_hLibrary, "TrustWizard" );
  229. if ( NULL == m_pfFunction )
  230. {
  231. // The library is present but does not have the entry point
  232. ::FreeLibrary( m_hLibrary );
  233. m_hLibrary = NULL;
  234. return FALSE;
  235. }
  236. ASSERT(m_hLibrary != NULL);
  237. ASSERT(m_pfFunction != NULL);
  238. return TRUE;
  239. }
  240. typedef HRESULT (*TRUST_WIZARD_PROC) (HWND, LPCWSTR);
  241. HRESULT CDsUiWizDLL::TrustWizard(HWND hWndParent, LPCWSTR lpsz)
  242. {
  243. ASSERT(m_hLibrary != NULL);
  244. ASSERT(m_pfFunction != NULL);
  245. return ((TRUST_WIZARD_PROC)m_pfFunction) (hWndParent, lpsz);
  246. }