Source code of Windows XP (NT5)
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.

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