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.

201 lines
4.5 KiB

  1. #include "privcpp.h"
  2. #include "ids.h"
  3. #include <advpub.h>
  4. #define INITGUID
  5. #include <initguid.h>
  6. #include "packguid.h"
  7. #define DECL_CRTFREE
  8. #include <crtfree.h>
  9. LONG g_cRefThisDll = 0;
  10. HINSTANCE g_hinst = NULL;
  11. HINSTANCE g_hinstResDLL = NULL;
  12. STDAPI_(BOOL) DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
  13. {
  14. switch(dwReason) {
  15. case DLL_PROCESS_ATTACH:
  16. g_hinst = hDll;
  17. g_hinstResDLL = hDll;
  18. #ifdef DEBUG
  19. CcshellGetDebugFlags();
  20. #endif // DEBUG
  21. break;
  22. default:
  23. break;
  24. }
  25. return TRUE;
  26. }
  27. class CMyClassFactory : public IClassFactory
  28. {
  29. public:
  30. CMyClassFactory(REFCLSID rclsid);
  31. ~CMyClassFactory() { g_cRefThisDll--; }
  32. // IUnKnown
  33. STDMETHODIMP QueryInterface(REFIID,void **);
  34. STDMETHODIMP_(ULONG) AddRef(void);
  35. STDMETHODIMP_(ULONG) Release(void);
  36. // IClassFactory
  37. STDMETHODIMP CreateInstance(IUnknown* pUnkOuter, REFIID riid, void **ppvObject);
  38. STDMETHODIMP LockServer(BOOL fLock);
  39. protected:
  40. LONG _cRef;
  41. CLSID _clsid;
  42. };
  43. CMyClassFactory::CMyClassFactory(REFCLSID rclsid) : _cRef(1), _clsid(rclsid)
  44. {
  45. g_cRefThisDll++;
  46. }
  47. HRESULT CMyClassFactory::QueryInterface(REFIID riid, void **ppvObject)
  48. {
  49. HRESULT hres;
  50. if (IsEqualGUID(riid, IID_IClassFactory) || IsEqualGUID(riid, IID_IUnknown))
  51. {
  52. DebugMsg(DM_TRACE, "pack cf - QueryInterface called");
  53. _cRef++;
  54. *ppvObject = (IClassFactory *)this;
  55. hres = NOERROR;
  56. }
  57. else
  58. {
  59. *ppvObject = NULL;
  60. hres = E_NOINTERFACE;
  61. }
  62. return hres;
  63. }
  64. ULONG CMyClassFactory::AddRef(void)
  65. {
  66. DebugMsg(DM_TRACE, "pack cf - AddRef called");
  67. return ++_cRef;
  68. }
  69. ULONG CMyClassFactory::Release(void)
  70. {
  71. DebugMsg(DM_TRACE, "pack cf - Release called");
  72. LONG cRef = InterlockedDecrement( &_cRef );
  73. if ( 0 == cRef )
  74. {
  75. delete this;
  76. }
  77. return cRef;
  78. }
  79. HRESULT CMyClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid, void **ppvObject)
  80. {
  81. DebugMsg(DM_TRACE, "CMyClassFactory::CreateInstance called");
  82. if (pUnkOuter)
  83. return CLASS_E_NOAGGREGATION;
  84. IUnknown* punk;
  85. HRESULT hres;
  86. if (IsEqualGUID(_clsid, CLSID_CPackage))
  87. {
  88. hres = CPackage_CreateInstance(&punk);
  89. }
  90. else
  91. {
  92. return E_UNEXPECTED;
  93. }
  94. if (SUCCEEDED(hres))
  95. {
  96. hres = punk->QueryInterface(riid, ppvObject);
  97. punk->Release();
  98. }
  99. return hres;
  100. }
  101. HRESULT CMyClassFactory::LockServer(BOOL fLock)
  102. {
  103. DebugMsg(DM_TRACE, "pack cf - LockServer called");
  104. return S_OK;
  105. }
  106. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID FAR* ppvOut)
  107. {
  108. DebugMsg(DM_TRACE, "pack - DllGetClassObject called");
  109. if (IsEqualGUID(rclsid,CLSID_CPackage))
  110. {
  111. CMyClassFactory *pmycls = new CMyClassFactory(rclsid);
  112. if (pmycls)
  113. {
  114. HRESULT hres = pmycls->QueryInterface(riid, ppvOut);
  115. pmycls->Release();
  116. return hres;
  117. }
  118. return E_OUTOFMEMORY;
  119. }
  120. return CLASS_E_CLASSNOTAVAILABLE;
  121. }
  122. STDAPI DllCanUnloadNow(void)
  123. {
  124. DebugMsg(DM_TRACE, "pack - DllCanUnloadNow called");
  125. if (g_cRefThisDll)
  126. {
  127. return S_FALSE;
  128. }
  129. DebugMsg(DM_TRACE, "pack - DllCanUnloadNow returning S_OK (bye, bye...)");
  130. return S_OK;
  131. }
  132. // Call ADVPACK for the given section of our resource based INF>
  133. // hInstance = resource instance to get REGINST section from
  134. // szSection = section name to invoke
  135. HRESULT CallRegInstall(HINSTANCE hInstance, LPCSTR szSection)
  136. {
  137. HRESULT hr = E_FAIL;
  138. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  139. if (hinstAdvPack)
  140. {
  141. REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, "RegInstall");
  142. if ( pfnri )
  143. {
  144. #ifdef WINNT
  145. STRENTRY seReg[] =
  146. {
  147. // These two NT-specific entries must be at the end
  148. { "25", "%SystemRoot%" },
  149. { "11", "%SystemRoot%\\system32" },
  150. };
  151. STRTABLE stReg = { ARRAYSIZE(seReg), seReg };
  152. hr = pfnri(hInstance, szSection, &stReg);
  153. #else
  154. hr = pfnri(hInstance, szSection, NULL);
  155. #endif
  156. }
  157. FreeLibrary(hinstAdvPack);
  158. }
  159. return hr;
  160. }
  161. STDAPI DllRegisterServer(void)
  162. {
  163. CallRegInstall(g_hinst, "RegDll");
  164. return S_OK;
  165. }
  166. STDAPI DllUnregisterServer(void)
  167. {
  168. CallRegInstall(g_hinst, "UnregDll");
  169. return S_OK;
  170. }