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.

204 lines
4.5 KiB

  1. //
  2. // standard inprocserver DLL code, you should not need to mess with this
  3. //
  4. #include "project.h"
  5. HANDLE g_hInst = NULL;
  6. LONG g_cRefDll = 0; // Number of locks on this DLL
  7. STDAPI_(void) DllAddRef()
  8. {
  9. InterlockedIncrement(&g_cRefDll);
  10. }
  11. STDAPI_(void) DllRelease()
  12. {
  13. ASSERT( 0 != g_cRefDll );
  14. InterlockedDecrement(&g_cRefDll);
  15. }
  16. STDAPI_(BOOL) DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID reserved)
  17. {
  18. if (fdwReason == DLL_PROCESS_ATTACH)
  19. {
  20. g_hInst = hInstDll;
  21. }
  22. return TRUE;
  23. }
  24. STDMETHODIMP CSampleClassFactory::QueryInterface(REFIID riid, void **ppvObject)
  25. {
  26. if (IsEqualIID(riid, IID_IUnknown) ||
  27. IsEqualIID(riid, IID_IClassFactory)) {
  28. *ppvObject = (void *)this;
  29. AddRef();
  30. return NOERROR;
  31. }
  32. *ppvObject = NULL;
  33. return E_NOINTERFACE;
  34. }
  35. STDMETHODIMP_(ULONG) CSampleClassFactory::AddRef(void)
  36. {
  37. DllAddRef();
  38. return 2;
  39. }
  40. STDMETHODIMP_(ULONG) CSampleClassFactory::Release(void)
  41. {
  42. DllRelease();
  43. return 1;
  44. }
  45. STDMETHODIMP CSampleClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
  46. {
  47. *ppvObject = NULL;
  48. if (NULL != pUnkOuter)
  49. return CLASS_E_NOAGGREGATION;
  50. CSampleObtainRating *pObj = new CSampleObtainRating; /* doing this does implicit AddRef() */
  51. if (NULL == pObj)
  52. return E_OUTOFMEMORY;
  53. HRESULT hr = pObj->QueryInterface(riid, ppvObject);
  54. pObj->Release();
  55. return hr;
  56. }
  57. STDMETHODIMP CSampleClassFactory::LockServer(BOOL fLock)
  58. {
  59. if (fLock)
  60. DllAddRef();
  61. else
  62. DllRelease();
  63. return NOERROR;
  64. }
  65. //
  66. // standard COM DLL self registering entry point
  67. //
  68. STDAPI DllRegisterServer(void)
  69. {
  70. HKEY hkeyCLSID;
  71. LONG err;
  72. TCHAR szPath[MAX_PATH];
  73. // get path to this DLL
  74. GetModuleFileName(g_hInst, szPath, MAX_PATH);
  75. /* First register our CLSID under HKEY_CLASSES_ROOT. */
  76. err = ::RegOpenKey(HKEY_CLASSES_ROOT, "CLSID", &hkeyCLSID);
  77. if (err == ERROR_SUCCESS) {
  78. HKEY hkeyOurs;
  79. err = ::RegCreateKey(hkeyCLSID, ::szOurGUID, &hkeyOurs);
  80. if (err == ERROR_SUCCESS) {
  81. HKEY hkeyInproc;
  82. err = ::RegCreateKey(hkeyOurs, "InProcServer32", &hkeyInproc);
  83. if (err == ERROR_SUCCESS) {
  84. err = ::RegSetValueEx(hkeyInproc, NULL, 0, REG_SZ,
  85. (LPBYTE)szPath, lstrlen(szPath) + 1);
  86. if (err == ERROR_SUCCESS) {
  87. err = ::RegSetValueEx(hkeyInproc, "ThreadingModel", 0,
  88. REG_SZ, (LPBYTE)"Apartment", 10);
  89. }
  90. ::RegCloseKey(hkeyInproc);
  91. }
  92. ::RegCloseKey(hkeyOurs);
  93. }
  94. ::RegCloseKey(hkeyCLSID);
  95. /* Now install ourselves as a ratings helper. */
  96. if (err == ERROR_SUCCESS) {
  97. err = ::RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Rating Helpers", &hkeyCLSID);
  98. if (err == ERROR_SUCCESS) {
  99. err = ::RegSetValueEx(hkeyCLSID, ::szOurGUID, 0, REG_SZ, (LPBYTE)"", 2);
  100. ::RegCloseKey(hkeyCLSID);
  101. }
  102. }
  103. }
  104. if (err == ERROR_SUCCESS)
  105. return S_OK;
  106. else
  107. return HRESULT_FROM_WIN32(err);
  108. }
  109. //
  110. // standard COM DLL self registering entry point
  111. //
  112. STDAPI DllUnregisterServer(void)
  113. {
  114. HKEY hkeyCLSID;
  115. LONG err;
  116. err = ::RegOpenKey(HKEY_CLASSES_ROOT, "CLSID", &hkeyCLSID);
  117. if (err == ERROR_SUCCESS) {
  118. HKEY hkeyOurs;
  119. err = ::RegOpenKey(hkeyCLSID, ::szOurGUID, &hkeyOurs);
  120. if (err == ERROR_SUCCESS) {
  121. err = ::RegDeleteKey(hkeyOurs, "InProcServer32");
  122. ::RegCloseKey(hkeyOurs);
  123. if (err == ERROR_SUCCESS)
  124. err = ::RegDeleteKey(hkeyCLSID, ::szOurGUID);
  125. }
  126. ::RegCloseKey(hkeyCLSID);
  127. if (err == ERROR_SUCCESS) {
  128. err = ::RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Rating Helpers", &hkeyCLSID);
  129. if (err == ERROR_SUCCESS) {
  130. err = ::RegDeleteValue(hkeyCLSID, ::szOurGUID);
  131. ::RegCloseKey(hkeyCLSID);
  132. }
  133. }
  134. }
  135. if (err == ERROR_SUCCESS)
  136. return S_OK;
  137. else
  138. return HRESULT_FROM_WIN32(err);
  139. }
  140. //
  141. // standard COM DLL entry point
  142. //
  143. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
  144. {
  145. if (IsEqualCLSID(rclsid, CLSID_Sample))
  146. {
  147. static CSampleClassFactory cf; /* note, declaring this doesn't constitute a reference */
  148. return cf.QueryInterface(riid, ppv); /* will AddRef() if successful */
  149. }
  150. // to make this support more com objects add them here
  151. *ppv = NULL;
  152. return CLASS_E_CLASSNOTAVAILABLE;;
  153. }
  154. //
  155. // standard COM DLL entry point
  156. //
  157. STDAPI DllCanUnloadNow(void)
  158. {
  159. return g_cRefDll == 0 ? S_OK : S_FALSE;
  160. }