dllreg.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 4k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. // dllreg.cpp -- autmatic registration and unregistration
  2. //
  3. #include "priv.h"
  4. #include <advpub.h>
  5. #include <comcat.h>
  6. // helper macros
  7. // ADVPACK will return E_UNEXPECTED if you try to uninstall (which does a registry restore)
  8. // on an INF section that was never installed.  We uninstall sections that may never have
  9. // been installed, so this MACRO will quiet these errors.
  10. #define QuietInstallNoOp(hr)   ((E_UNEXPECTED == hr) ? S_OK : hr)
  11. BOOL UnregisterTypeLibrary(const CLSID* piidLibrary)
  12. {
  13.     TCHAR szScratch[GUIDSTR_MAX];
  14.     HKEY hk;
  15.     BOOL fResult = FALSE;
  16.     // convert the libid into a string.
  17.     //
  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.     
  24.     return fResult;
  25. }
  26. HRESULT RegisterTypeLib(void)
  27. {
  28.     HRESULT hr = S_OK;
  29.     ITypeLib *pTypeLib;
  30.     DWORD   dwPathLen;
  31.     TCHAR   szTmp[MAX_PATH];
  32. #ifdef UNICODE
  33.     WCHAR   *pwsz = szTmp; 
  34. #else
  35.     WCHAR   pwsz[MAX_PATH];
  36. #endif
  37.     // Load and register our type library.
  38.     //
  39.     dwPathLen = GetModuleFileName(HINST_THISDLL, szTmp, ARRAYSIZE(szTmp));
  40. #ifndef UNICODE
  41.     if (SHAnsiToUnicode(szTmp, pwsz, MAX_PATH)) 
  42. #endif
  43.     {
  44.         hr = LoadTypeLib(pwsz, &pTypeLib);
  45.         if (SUCCEEDED(hr))
  46.         {
  47.             // call the unregister type library as we had some old junk that
  48.             // was registered by a previous version of OleAut32, which is now causing
  49.             // the current version to not work on NT...
  50.             UnregisterTypeLibrary(&LIBID_SrvWizLib);
  51.             hr = RegisterTypeLib(pTypeLib, pwsz, NULL);
  52.             if (FAILED(hr))
  53.             {
  54.             }
  55.             pTypeLib->Release();
  56.         }
  57.         else
  58.         {
  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(HINSTANCE hinstSrvWiz, 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 szThisDLL[MAX_PATH];
  84.             // Get the location of this DLL from the HINSTANCE
  85.             if ( !GetModuleFileNameA(hinstSrvWiz, szThisDLL, ARRAYSIZE(szThisDLL)) )
  86.             {
  87.                 // Failed, just say "srvwiz.dll"
  88.                 lstrcpyA(szThisDLL, "srvwiz.dll");
  89.             }
  90.             STRENTRY seReg[] = {
  91.                 { "THISDLL", szThisDLL },
  92.                 // These two NT-specific entries must be at the end
  93.                 { "25", "%SystemRoot%" },
  94.                 { "11", "%SystemRoot%\system32" },
  95.             };
  96.             STRTABLE stReg = { ARRAYSIZE(seReg), seReg };
  97.             hr = pfnri(g_hinst, szSection, &stReg);
  98.         }
  99.         FreeLibrary(hinstAdvPack);
  100.     }
  101.     return hr;
  102. }
  103. STDAPI DllRegisterServer(void)
  104. {
  105.     HRESULT hr;
  106.     // Delete any old registration entries, then add the new ones.
  107.     // Keep ADVPACK.DLL loaded across multiple calls to RegInstall.
  108.     // (The inf engine doesn't guarantee DelReg/AddReg order, that's
  109.     // why we explicitly unreg and reg here.)
  110.     //
  111.     HINSTANCE hinstSrvWiz = GetModuleHandle(TEXT("SRVWIZ.DLL"));
  112.     hr = CallRegInstall(hinstSrvWiz, "RegDll");
  113.     RegisterTypeLib();
  114.     return hr;
  115. }
  116. STDAPI DllUnregisterServer(void)
  117. {
  118.     HRESULT hr;
  119.     HINSTANCE hinstSrvWiz = GetModuleHandle(TEXT("SRVWIZ.DLL"));
  120.     // UnInstall the registry values
  121.     hr = CallRegInstall(hinstSrvWiz, "UnregDll");
  122.     UnregisterTypeLibrary(&LIBID_SrvWizLib);
  123.     return hr;
  124. }
  125. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  126. {
  127.     return S_OK;    
  128. }