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.

212 lines
5.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1993 - 2000.
  5. //
  6. // File: nusrmgr.cpp
  7. //
  8. // Contents: DllMain routines
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <advpub.h> // for REGINSTALL
  13. #define DECL_CRTFREE
  14. #include <crtfree.h>
  15. #include "resource.h"
  16. #include "nusrmgr_i.c"
  17. #include "commondialog.h"
  18. #include "passportmanager.h"
  19. #include "toolbar.h"
  20. DWORD g_tlsAppCommandHook = -1;
  21. CComModule _Module;
  22. BEGIN_OBJECT_MAP(ObjectMap)
  23. OBJECT_ENTRY(CLSID_CommonDialog, CCommonDialog)
  24. OBJECT_ENTRY(CLSID_PassportManager, CPassportManager)
  25. OBJECT_ENTRY(CLSID_Toolbar, CToolbar)
  26. END_OBJECT_MAP()
  27. //
  28. // DllMain (attach/deatch) routine
  29. //
  30. STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  31. {
  32. if (dwReason == DLL_PROCESS_ATTACH)
  33. {
  34. // Don't put it under #ifdef DEBUG
  35. CcshellGetDebugFlags();
  36. DisableThreadLibraryCalls(hInstance);
  37. g_tlsAppCommandHook = TlsAlloc();
  38. SHFusionInitializeFromModuleID(hInstance, 123);
  39. _Module.Init(ObjectMap, hInstance, &LIBID_NUSRMGRLib);
  40. }
  41. else if (dwReason == DLL_PROCESS_DETACH)
  42. {
  43. _Module.Term();
  44. SHFusionUninitialize();
  45. if (-1 != g_tlsAppCommandHook)
  46. {
  47. TlsFree(g_tlsAppCommandHook);
  48. }
  49. }
  50. return TRUE;
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. // Used to determine whether the DLL can be unloaded by OLE
  54. STDAPI DllCanUnloadNow()
  55. {
  56. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. // Returns a class factory to create an object of the requested type
  60. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  61. {
  62. return _Module.GetClassObject(rclsid, riid, ppv);
  63. }
  64. //
  65. // Calls the ADVPACK entry-point which executes an inf
  66. // file section.
  67. //
  68. HRESULT CallRegInstall(HINSTANCE hinstFTP, LPSTR szSection)
  69. {
  70. UNREFERENCED_PARAMETER(hinstFTP);
  71. HRESULT hr = E_FAIL;
  72. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  73. if (hinstAdvPack)
  74. {
  75. REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, "RegInstall");
  76. if (pfnri)
  77. {
  78. char szThisDLL[MAX_PATH];
  79. // Get the location of this DLL from the HINSTANCE
  80. if (GetModuleFileNameA(_Module.GetModuleInstance(), szThisDLL, ARRAYSIZE(szThisDLL)))
  81. {
  82. STRENTRY seReg[] = {
  83. {"THISDLL", szThisDLL },
  84. { "25", "%SystemRoot%" }, // These two NT-specific entries
  85. { "11", "%SystemRoot%\\system32" }, // must be at the end of the table
  86. };
  87. STRTABLE stReg = {ARRAYSIZE(seReg) - 2, seReg};
  88. hr = pfnri(_Module.GetResourceInstance(), szSection, &stReg);
  89. }
  90. }
  91. FreeLibrary(hinstAdvPack);
  92. }
  93. return hr;
  94. }
  95. HRESULT UnregisterTypeLibrary(const CLSID* piidLibrary)
  96. {
  97. HRESULT hr = E_FAIL;
  98. TCHAR szGuid[GUIDSTR_MAX];
  99. HKEY hk;
  100. // convert the libid into a string.
  101. //
  102. SHStringFromGUID(*piidLibrary, szGuid, ARRAYSIZE(szGuid));
  103. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("TypeLib"), 0, MAXIMUM_ALLOWED, &hk) == ERROR_SUCCESS)
  104. {
  105. if (SHDeleteKey(hk, szGuid))
  106. {
  107. // success
  108. hr = S_OK;
  109. }
  110. RegCloseKey(hk);
  111. }
  112. else
  113. {
  114. hr = HRESULT_FROM_WIN32(GetLastError());
  115. }
  116. return hr;
  117. }
  118. HRESULT RegisterTypeLibrary(const CLSID* piidLibrary)
  119. {
  120. HRESULT hr = E_FAIL;
  121. ITypeLib* pTypeLib;
  122. WCHAR wszModuleName[MAX_PATH];
  123. // Load and register our type library.
  124. if (GetModuleFileNameW(_Module.GetModuleInstance(), wszModuleName, ARRAYSIZE(wszModuleName)))
  125. {
  126. hr = LoadTypeLib(wszModuleName, &pTypeLib);
  127. if (SUCCEEDED(hr))
  128. {
  129. // call the unregister type library in case we had some old junk in the registry
  130. UnregisterTypeLibrary(piidLibrary);
  131. hr = RegisterTypeLib(pTypeLib, wszModuleName, NULL);
  132. if (FAILED(hr))
  133. {
  134. TraceMsg(TF_WARNING, "RegisterTypeLibrary: RegisterTypeLib failed (%x)", hr);
  135. }
  136. pTypeLib->Release();
  137. }
  138. else
  139. {
  140. TraceMsg(TF_WARNING, "RegisterTypeLibrary: LoadTypeLib failed (%x) on", hr);
  141. }
  142. }
  143. return hr;
  144. }
  145. STDAPI DllRegisterServer(void)
  146. {
  147. HRESULT hr;
  148. hr = CallRegInstall(_Module.GetResourceInstance(), "UserAccountsInstall");
  149. ASSERT(SUCCEEDED(hr));
  150. hr = RegisterTypeLibrary(&LIBID_NUSRMGRLib);
  151. ASSERT(SUCCEEDED(hr));
  152. return hr;
  153. }
  154. STDAPI DllUnregisterServer(void)
  155. {
  156. HRESULT hr;
  157. hr = CallRegInstall(_Module.GetResourceInstance(), "UserAccountsUninstall");
  158. ASSERT(SUCCEEDED(hr));
  159. hr = UnregisterTypeLibrary(&LIBID_NUSRMGRLib);
  160. ASSERT(SUCCEEDED(hr));
  161. return hr;
  162. }
  163. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  164. {
  165. return S_OK;
  166. }