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.

173 lines
4.5 KiB

  1. // dllreg.cpp -- autmatic registration and unregistration
  2. //
  3. #include "priv.h"
  4. #include <advpub.h>
  5. #include <comcat.h>
  6. #include <autodiscovery.h> // For LIBID_AutoDiscovery
  7. // helper macros
  8. // ADVPACK will return E_UNEXPECTED if you try to uninstall (which does a registry restore)
  9. // on an INF section that was never installed. We uninstall sections that may never have
  10. // been installed, so this MACRO will quiet these errors.
  11. #define QuietInstallNoOp(hr) ((E_UNEXPECTED == hr) ? S_OK : hr)
  12. BOOL UnregisterTypeLibrary(const CLSID* piidLibrary)
  13. {
  14. TCHAR szScratch[GUIDSTR_MAX];
  15. HKEY hk;
  16. BOOL fResult = FALSE;
  17. // convert the libid into a string.
  18. SHStringFromGUID(*piidLibrary, szScratch, ARRAYSIZE(szScratch));
  19. if (RegOpenKey(HKEY_CLASSES_ROOT, TEXT("TypeLib"), &hk) == ERROR_SUCCESS) {
  20. fResult = RegDeleteKey(hk, szScratch);
  21. RegCloseKey(hk);
  22. }
  23. return fResult;
  24. }
  25. HRESULT MyRegTypeLib(void)
  26. {
  27. HRESULT hr = S_OK;
  28. ITypeLib *pTypeLib;
  29. DWORD dwPathLen;
  30. TCHAR szTmp[MAX_PATH];
  31. #ifdef UNICODE
  32. WCHAR *pwsz = szTmp;
  33. #else
  34. WCHAR pwsz[MAX_PATH];
  35. #endif
  36. // Load and register our type library.
  37. dwPathLen = GetModuleFileName(HINST_THISDLL, szTmp, ARRAYSIZE(szTmp));
  38. #ifndef UNICODE
  39. if (SHAnsiToUnicode(szTmp, pwsz, MAX_PATH))
  40. #endif
  41. {
  42. hr = LoadTypeLib(pwsz, &pTypeLib);
  43. if (SUCCEEDED(hr))
  44. {
  45. // call the unregister type library as we had some old junk that
  46. // was registered by a previous version of OleAut32, which is now causing
  47. // the current version to not work on NT...
  48. UnregisterTypeLibrary(&LIBID_AutoDiscovery);
  49. hr = RegisterTypeLib(pTypeLib, pwsz, NULL);
  50. if (FAILED(hr))
  51. {
  52. TraceMsg(TF_ALWAYS, "AUTODISC: RegisterTypeLib failed (%x)", hr);
  53. }
  54. pTypeLib->Release();
  55. }
  56. else
  57. {
  58. TraceMsg(TF_ALWAYS, "AUTODISC: LoadTypeLib failed (%x)", hr);
  59. }
  60. }
  61. #ifndef UNICODE
  62. else {
  63. hr = E_FAIL;
  64. }
  65. #endif
  66. return hr;
  67. }
  68. /*----------------------------------------------------------
  69. Purpose: Calls the ADVPACK entry-point which executes an inf
  70. file section.
  71. Returns:
  72. Cond: --
  73. */
  74. HRESULT CallRegInstall(LPSTR szSection)
  75. {
  76. HRESULT hr = E_FAIL;
  77. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  78. if (hinstAdvPack)
  79. {
  80. REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, "RegInstall");
  81. if (pfnri)
  82. {
  83. char szIEPath[MAX_PATH];
  84. STRENTRY seReg[] = {
  85. { "NO_LONGER_USED", szIEPath },
  86. // These two NT-specific entries must be at the end
  87. { "25", "%SystemRoot%" },
  88. { "11", "%SystemRoot%\\system32" },
  89. };
  90. STRTABLE stReg = { ARRAYSIZE(seReg) - 2, seReg };
  91. szIEPath[0] = 0;
  92. hr = pfnri(HINST_THISDLL, szSection, &stReg);
  93. }
  94. FreeLibrary(hinstAdvPack);
  95. }
  96. return hr;
  97. }
  98. STDAPI DllRegisterServer(void)
  99. {
  100. HRESULT hr;
  101. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  102. hr = CallRegInstall("DLL_RegInstall");
  103. #ifdef FEATURE_MAILBOX
  104. hr = CallRegInstall("DLL_RegInstallMailBox");
  105. #else // FEATURE_MAILBOX
  106. CallRegInstall("DLL_RegUnInstallMailBox");
  107. #endif // FEATURE_MAILBOX
  108. MyRegTypeLib();
  109. if (hinstAdvPack)
  110. FreeLibrary(hinstAdvPack);
  111. return hr;
  112. }
  113. STDAPI DllUnregisterServer(void)
  114. {
  115. HRESULT hr;
  116. // UnInstall the registry values
  117. hr = CallRegInstall("DLL_RegUnInstall");
  118. CallRegInstall("DLL_RegUnInstallMailBox");
  119. UnregisterTypeLibrary(&LIBID_AutoDiscovery);
  120. return hr;
  121. }
  122. /*----------------------------------------------------------
  123. Purpose: Install/uninstall user settings
  124. Description: Note that this function has special error handling.
  125. The function will keep hrExternal with the worse error
  126. but will only stop executing util the internal error (hr)
  127. gets really bad. This is because we need the external
  128. error to catch incorrectly authored INFs but the internal
  129. error to be robust in attempting to install other INF sections
  130. even if one doesn't make it.
  131. */
  132. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  133. {
  134. return S_OK;
  135. }