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.

226 lines
4.5 KiB

  1. /* cat_dll.cpp */
  2. #include <windows.h>
  3. #include <comdef.h>
  4. const GUID CLSID_COMCatalog = { 0x00000346, 0, 0, 0xC0,0,0,0,0,0,0,0x46 };
  5. HRESULT __stdcall GetCatalogObject
  6. (
  7. /* [in] */ REFIID riid,
  8. /* [out, iis_is(riid)] */ void ** ppv
  9. )
  10. {
  11. return CoCreateInstance (CLSID_COMCatalog, NULL, CLSCTX_INPROC, riid, ppv);
  12. }
  13. #define CLSID_CATALOG "{1A26EFEF-9F04-11D1-8F36-00C04FD8FF5E}"
  14. const CLSID g_clsid_catalog =
  15. {0x1A26EFEF,0x9F04,0x11D1,{0x8F,0x36,0x00,0xC0,0x4F,0xD8,0xFF,0x5E}};
  16. class CClassFactory : public IClassFactory
  17. {
  18. public:
  19. CClassFactory(void);
  20. STDMETHODIMP QueryInterface(REFIID riid, LPVOID FAR* ppvObj);
  21. STDMETHODIMP_(ULONG) AddRef(void);
  22. STDMETHODIMP_(ULONG) Release(void);
  23. STDMETHODIMP CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID FAR* ppvObject);
  24. STDMETHODIMP LockServer(BOOL fLock);
  25. private:
  26. long m_cRef;
  27. };
  28. CClassFactory::CClassFactory(void) : m_cRef(0) { }
  29. STDMETHODIMP CClassFactory::QueryInterface(
  30. REFIID riid,
  31. LPVOID FAR* ppvObj)
  32. {
  33. *ppvObj = NULL;
  34. if ((riid == IID_IClassFactory) || (riid == IID_IUnknown))
  35. {
  36. *ppvObj = (LPVOID) (IClassFactory *) this;
  37. }
  38. if (*ppvObj != NULL)
  39. {
  40. ((LPUNKNOWN)*ppvObj)->AddRef();
  41. return S_OK;
  42. }
  43. return E_NOINTERFACE;
  44. }
  45. STDMETHODIMP_(ULONG) CClassFactory::AddRef(void)
  46. {
  47. long cRef;
  48. cRef = InterlockedIncrement(&m_cRef);
  49. return(cRef);
  50. }
  51. STDMETHODIMP_(ULONG) CClassFactory::Release(void)
  52. {
  53. long cRef;
  54. cRef = InterlockedDecrement(&m_cRef);
  55. if (cRef == 0)
  56. {
  57. delete this;
  58. }
  59. return(cRef);
  60. }
  61. STDMETHODIMP CClassFactory::CreateInstance(
  62. LPUNKNOWN pUnkOuter,
  63. REFIID riid,
  64. LPVOID FAR* ppvObj)
  65. {
  66. HRESULT hr;
  67. *ppvObj = NULL;
  68. if (pUnkOuter)
  69. {
  70. return(CLASS_E_NOAGGREGATION);
  71. }
  72. hr = CoCreateInstance (CLSID_COMCatalog, NULL, CLSCTX_INPROC, riid, ppvObj);
  73. return(hr);
  74. }
  75. STDMETHODIMP CClassFactory::LockServer(BOOL fLock)
  76. {
  77. return(S_OK);
  78. }
  79. static HINSTANCE g_hInst = NULL;
  80. STDAPI_(BOOL) APIENTRY DllMain
  81. (
  82. HINSTANCE hInst,
  83. DWORD dwReason,
  84. LPVOID lpReserved
  85. )
  86. {
  87. g_hInst = hInst;
  88. return(TRUE);
  89. }
  90. STDAPI DllCanUnloadNow(void)
  91. {
  92. return( S_FALSE );
  93. }
  94. STDAPI DllGetClassObject
  95. (
  96. REFCLSID rclsid,
  97. REFIID riid,
  98. LPVOID FAR* ppvObj
  99. )
  100. {
  101. if (rclsid != g_clsid_catalog)
  102. {
  103. return(E_FAIL);
  104. }
  105. if (ppvObj == NULL)
  106. {
  107. return E_INVALIDARG;
  108. }
  109. *ppvObj = NULL;
  110. CClassFactory *pcf = new CClassFactory;
  111. if (pcf == NULL)
  112. {
  113. return(E_OUTOFMEMORY);
  114. }
  115. pcf->AddRef();
  116. HRESULT hRes = pcf->QueryInterface(riid, ppvObj);
  117. pcf->Release();
  118. return(hRes);
  119. }
  120. typedef struct
  121. {
  122. char *KeyName;
  123. char *ValueName;
  124. char *Value;
  125. } REGISTRATION_ENTRY;
  126. REGISTRATION_ENTRY registration[] =
  127. {
  128. { "CLSID\\" CLSID_CATALOG, NULL, "COM+ Catalog Queries" },
  129. { "CLSID\\" CLSID_CATALOG "\\InprocServer32", NULL, /* dynamic */ },
  130. { "CLSID\\" CLSID_CATALOG "\\InprocServer32", "ThreadingModel", "Both" }
  131. };
  132. #define NUM_REGISTRATION_VALUES (sizeof(registration) / sizeof(registration[0]))
  133. #define REGISTRATION_SERVER (1)
  134. STDAPI DllRegisterServer(void)
  135. {
  136. HRESULT hr;
  137. char szModuleName[MAX_PATH+1];
  138. GetModuleFileNameA(g_hInst, szModuleName, sizeof(szModuleName)/sizeof(szModuleName[0]));
  139. szModuleName[sizeof(szModuleName)/sizeof(szModuleName[0])-1]= '\0';
  140. registration[REGISTRATION_SERVER].Value = szModuleName;
  141. for (int i = 0; i < NUM_REGISTRATION_VALUES; i++)
  142. {
  143. HKEY hKey;
  144. hr = RegCreateKeyA(HKEY_CLASSES_ROOT, registration[i].KeyName, &hKey);
  145. if (hr == ERROR_SUCCESS)
  146. {
  147. hr = RegSetValueExA(hKey,
  148. registration[i].ValueName,
  149. 0,
  150. REG_SZ,
  151. (BYTE *) registration[i].Value,
  152. strlen(registration[i].Value) + 1);
  153. RegCloseKey(hKey);
  154. }
  155. if (hr != ERROR_SUCCESS)
  156. {
  157. DllUnregisterServer();
  158. return(SELFREG_E_CLASS);
  159. }
  160. }
  161. return(S_OK);
  162. }
  163. STDAPI DllUnregisterServer(void)
  164. {
  165. for (int i = NUM_REGISTRATION_VALUES - 1; i >= 0; i--)
  166. {
  167. RegDeleteKeyA(HKEY_CLASSES_ROOT, registration[i].KeyName);
  168. }
  169. return(S_OK);
  170. }