DllNanny.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // DllNanny.cpp
  2. /*
  3. Configure the following registry entries:
  4. [HKEY_CLASSES_ROOTCLSID{10000002-0000-0000-0000-000000000001}]
  5. "AppID"="{10000002-0000-0000-0000-000000000001}"
  6. [HKEY_CLASSES_ROOTAppID{10000002-0000-0000-0000-000000000001}]
  7. @="Inside DCOM Sample"
  8. "DllSurrogate"="C:\MyDirectory\DllNanny.exe"
  9.   or use the following line to use the system-supplied default surrogate (dllhost.exe)
  10. "DllSurrogate"=""
  11.   The surrogate.reg file has a template for these registry entries
  12. */
  13. #define _WIN32_DCOM
  14. #include <windows.h>
  15. #include <iostream.h>  // For cout
  16. #include <conio.h>     // For _getch
  17. HANDLE g_hEvent;
  18. class CGenericFactory;
  19. class CSurrogate : public ISurrogate
  20. {
  21. public:
  22. // IUnknown
  23. ULONG __stdcall AddRef();
  24. ULONG __stdcall Release();
  25. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  26. // ISurrogate
  27. HRESULT __stdcall LoadDllServer(REFCLSID rclsid);
  28. HRESULT __stdcall FreeSurrogate();
  29. private:
  30. CGenericFactory* m_pcf;
  31. };
  32. class CGenericFactory : public IClassFactory
  33. {
  34. public:
  35. // IUnknown
  36. ULONG __stdcall AddRef();
  37. ULONG __stdcall Release();
  38. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  39. // IClassFactory
  40. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
  41. HRESULT __stdcall LockServer(BOOL bLock);
  42. CGenericFactory(REFCLSID rclsid) : m_cRef(0) { m_clsid = rclsid; }
  43. ~CGenericFactory() { }
  44. private:
  45. ULONG m_cRef;
  46. DWORD m_dwRegister;
  47. friend class CSurrogate;
  48. CLSID m_clsid;
  49. };
  50. ULONG CSurrogate::AddRef()
  51. {
  52. return 2;
  53. }
  54. ULONG CSurrogate::Release()
  55. {
  56. return 1;
  57. }
  58. HRESULT CSurrogate::QueryInterface(REFIID riid, void** ppv)
  59. {
  60. if(riid == IID_IUnknown)
  61. *ppv = reinterpret_cast<IUnknown*>(this);
  62. else if(riid == IID_ISurrogate)
  63. *ppv = (ISurrogate*)this;
  64. else
  65. {
  66. *ppv = NULL;
  67. return E_NOINTERFACE;
  68. }
  69. AddRef();
  70. return S_OK;
  71. }
  72. HRESULT CSurrogate::LoadDllServer(REFCLSID rclsid)
  73. {
  74. cout << "Surrogate: ISurrogate::LoadDllServer" << endl;
  75. m_pcf = new CGenericFactory(rclsid);
  76. return CoRegisterClassObject(rclsid, (IClassFactory*)m_pcf, CLSCTX_LOCAL_SERVER, REGCLS_SURROGATE, &m_pcf->m_dwRegister);
  77. }
  78. HRESULT CSurrogate::FreeSurrogate()
  79. {
  80. cout << "Surrogate: ISurrogate::FreeSurrogate" << endl;
  81. HRESULT hr = CoRevokeClassObject(m_pcf->m_dwRegister);
  82. SetEvent(g_hEvent);
  83. return hr;
  84. }
  85. ULONG CGenericFactory::AddRef()
  86. {
  87. cout << "Surrogate: CGenericFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  88. return ++m_cRef;
  89. }
  90. ULONG CGenericFactory::Release()
  91. {
  92. cout << "Surrogate: CGenericFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  93. if(--m_cRef != 0)
  94. return m_cRef;
  95. delete this;
  96. return 0;
  97. }
  98. HRESULT CGenericFactory::QueryInterface(REFIID riid, void** ppv)
  99. {
  100. if(riid == IID_IUnknown || riid == IID_IClassFactory)
  101. *ppv = (IClassFactory*)this;
  102. else
  103. {
  104. *ppv = NULL;
  105. return E_NOINTERFACE;
  106. }
  107. AddRef();
  108. return S_OK;
  109. }
  110. HRESULT CGenericFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  111. {
  112. return CoCreateInstance(m_clsid, pUnknownOuter, CLSCTX_INPROC_SERVER, riid, ppv);
  113. }
  114. HRESULT CGenericFactory::LockServer(BOOL bLock)
  115. {
  116. return S_OK;
  117. }
  118. void main(int argc, char** argv)
  119. {
  120. if(argc < 2)
  121. {
  122. cout << "DllNanny must be properly registered as a DLL Surrogate" << endl;
  123. return;
  124. }
  125. cout << "Surrogate: CoInitializeEx()" << endl;
  126. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  127. CSurrogate surrogate;
  128. CoRegisterSurrogate(&surrogate);
  129. cout << "Surrogate: The DLL's CLSID is " << argv[1] << endl;
  130. OLECHAR wszCLSID[39];
  131. mbstowcs(wszCLSID, argv[1], 39);
  132. CLSID clsid;
  133. CLSIDFromString(wszCLSID, &clsid);
  134. surrogate.LoadDllServer(clsid);
  135. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  136. WaitForSingleObject(g_hEvent, INFINITE);
  137. CloseHandle(g_hEvent);
  138. CoUninitialize();
  139. _getch();
  140. }