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

Windows Kernel

Development Platform:

Visual C++

  1. /*----------------------------------------------------------------------------
  2. / Title;
  3. /   dll.cpp
  4. /
  5. / Authors;
  6. /   David De Vorchik (daviddv)
  7. /
  8. / Notes;
  9. /   Core entry points for the DLL
  10. /----------------------------------------------------------------------------*/
  11. #include "pch.h"
  12. #include "query.h"
  13. #include <advpub.h>         // For REGINSTALL
  14. #pragma hdrstop
  15. #define INITGUID
  16. #include <initguid.h>
  17. #include "cmnquery.h"
  18. #include "dsquery.h"
  19. #include "iids.h"
  20. /*----------------------------------------------------------------------------
  21. / Globals
  22. /----------------------------------------------------------------------------*/
  23. HINSTANCE g_hInstance = 0;
  24. HRESULT CallRegInstall(LPSTR szSection);
  25. /*-----------------------------------------------------------------------------
  26. / DllMain
  27. / -------
  28. /   Main entry point.  We are passed reason codes and assored other
  29. /   information when loaded or closed down.
  30. /
  31. / In:
  32. /   hInstance = our instance handle
  33. /   dwReason = reason code
  34. /   pReserved = depends on the reason code.
  35. /
  36. / Out:
  37. /   -
  38. /----------------------------------------------------------------------------*/
  39. STDAPI_(BOOL) DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID pReserved )
  40. {
  41.     if ( dwReason == DLL_PROCESS_ATTACH )
  42.     {
  43.         GLOBAL_HINSTANCE = hInstance;
  44.     }
  45.     return TRUE;
  46. }
  47. /*-----------------------------------------------------------------------------
  48. / DllCanUnloadNow
  49. / ---------------
  50. /   Called by the outside world to determine if our DLL can be unloaded. If we
  51. /   have any objects in existance then we must not unload.
  52. /
  53. / In:
  54. /   -
  55. / Out:
  56. /   BOOL inidicate unload state.
  57. /----------------------------------------------------------------------------*/
  58. STDAPI DllCanUnloadNow( void )
  59. {
  60.     return GLOBAL_REFCOUNT ? S_FALSE : S_OK;
  61. }
  62. /*-----------------------------------------------------------------------------
  63. / DllGetClassObject
  64. / -----------------
  65. /   Given a class ID and an interface ID, return the relevant object.  This used
  66. /   by the outside world to access the objects contained here in.
  67. /
  68. / In:
  69. /   rCLISD = class ID required
  70. /   riid = interface within that class required
  71. /   ppVoid -> receives the newly created object.
  72. /
  73. / Out:
  74. /   -
  75. /----------------------------------------------------------------------------*/
  76. STDAPI DllGetClassObject(REFCLSID rCLSID, REFIID riid, LPVOID* ppVoid)
  77. {
  78.     HRESULT hr = E_OUTOFMEMORY;
  79.     *ppVoid = NULL;
  80.     if ( IsEqualIID(rCLSID, CLSID_ExampleQueryForm) )
  81.     {
  82.         CExampleQueryFormClassFactory* pClassFactory = new CExampleQueryFormClassFactory;
  83.         if ( !pClassFactory )
  84.             ExitGracefully(hr, E_OUTOFMEMORY, "Failed to get CExampleQueryForm's class factory");
  85.         hr = pClassFactory->QueryInterface(riid, ppVoid);
  86.         if ( FAILED(hr) )
  87.             delete pClassFactory;
  88.     }
  89.     else
  90.     {
  91.         ExitGracefully(hr, E_NOINTERFACE, "Object doesn't support the given CLISD");
  92.     }
  93. exit_gracefully:
  94.     return hr;
  95. }
  96. /*-----------------------------------------------------------------------------
  97. / Dll[Unregister/Register]Server
  98. / ------------------------------
  99. /   Called to allow us to setup the registry entries that we use, this
  100. /   takes advantage of the ADVPACK APIs and loads our .inf data from
  101. /   our resource block.
  102. /
  103. / In:
  104. /   -
  105. / Out:
  106. /   HRESULT
  107. /----------------------------------------------------------------------------*/
  108. STDAPI DllRegisterServer(VOID)
  109. {
  110.     return CallRegInstall("RegDll");
  111. }
  112. STDAPI DllUnregisterServer(VOID)
  113. {
  114.     return CallRegInstall("UnRegDll");
  115. }    
  116. /*-----------------------------------------------------------------------------
  117. / CallRegInstall
  118. / --------------
  119. /   Call ADVPACK for the given section of our resource based INF>
  120. /
  121. / In:
  122. /   szSection = section name to invoke
  123. /
  124. / Out:
  125. /   HRESULT:
  126. /----------------------------------------------------------------------------*/
  127. HRESULT CallRegInstall(LPSTR szSection)
  128. {
  129.     HRESULT hr = E_FAIL;
  130.     HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  131.     if (hinstAdvPack)
  132.     {
  133.         REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, "RegInstall");
  134. #ifdef UNICODE
  135.         if ( pfnri )
  136.         {
  137.             STRENTRY seReg[] = 
  138.             {
  139.                 // These two NT-specific entries must be at the end
  140.                 { "25", "%SystemRoot%" },
  141.                 { "11", "%SystemRoot%\system32" },
  142.             };
  143.             STRTABLE stReg = { ARRAYSIZE(seReg), seReg };
  144.             
  145.             hr = pfnri(GLOBAL_HINSTANCE, szSection, &stReg);
  146.         }
  147. #else
  148.         if (pfnri)
  149.         {
  150.             hr = pfnri(GLOBAL_HINSTANCE, szSection, NULL);
  151.         }
  152. #endif
  153.         FreeLibrary(hinstAdvPack);
  154.     }
  155.     return hr;
  156. }