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.

228 lines
4.6 KiB

  1. // TSSecurity.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "RemotePage.h"
  4. #include "registry.h"
  5. #include "resource.h"
  6. // our globals
  7. // {F0152790-D56E-4445-850E-4F3117DB740C}
  8. GUID CLSID_CTSRemotePage =
  9. { 0xf0152790, 0xd56e, 0x4445, { 0x85, 0xe, 0x4f, 0x31, 0x17, 0xdb, 0x74, 0xc } };
  10. static HINSTANCE g_hinst = NULL;
  11. static ULONG g_uSrvLock = 0;
  12. ULONG g_uObjects = 0;
  13. //Class factory definition
  14. class CClassFactory : public IClassFactory
  15. {
  16. private:
  17. ULONG m_cref;
  18. public:
  19. CClassFactory();
  20. ~CClassFactory();
  21. ///////////////////////////////
  22. // Interface IUnknown
  23. ///////////////////////////////
  24. STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppv);
  25. STDMETHODIMP_(ULONG) AddRef();
  26. STDMETHODIMP_(ULONG) Release();
  27. ///////////////////////////////
  28. // Interface IClassFactory
  29. ///////////////////////////////
  30. STDMETHODIMP CreateInstance(LPUNKNOWN, REFIID, LPVOID *);
  31. STDMETHODIMP LockServer(BOOL);
  32. };
  33. BOOL WINAPI
  34. DllMain(HINSTANCE hinstDLL,
  35. DWORD fdwReason,
  36. void* lpvReserved)
  37. {
  38. if (fdwReason == DLL_PROCESS_ATTACH) {
  39. g_hinst = hinstDLL;
  40. }
  41. return TRUE;
  42. }
  43. /**************************************************************************
  44. Exported functions
  45. ***************************************************************************/
  46. STDAPI
  47. DllGetClassObject(
  48. REFCLSID rclsid,
  49. REFIID riid,
  50. LPVOID *ppvObj)
  51. {
  52. if ((rclsid != CLSID_CTSRemotePage))
  53. return CLASS_E_CLASSNOTAVAILABLE;
  54. if (!ppvObj)
  55. return E_FAIL;
  56. *ppvObj = NULL;
  57. // We can only hand out IUnknown and IClassFactory pointers. Fail
  58. // if they ask for anything else.
  59. if (!IsEqualIID(riid, IID_IUnknown) && !IsEqualIID(riid, IID_IClassFactory))
  60. return E_NOINTERFACE;
  61. CClassFactory *pFactory = NULL;
  62. // make the factory passing in the creation function for the type of object they want
  63. if (rclsid == CLSID_CTSRemotePage)
  64. pFactory = new CClassFactory;
  65. if (NULL == pFactory)
  66. return E_OUTOFMEMORY;
  67. HRESULT hr = pFactory->QueryInterface(riid, ppvObj);
  68. pFactory->Release();
  69. return hr;
  70. }
  71. STDAPI
  72. DllCanUnloadNow()
  73. {
  74. if (g_uObjects == 0 && g_uSrvLock == 0)
  75. return S_OK;
  76. else
  77. return S_FALSE;
  78. }
  79. //
  80. // Server registration
  81. //
  82. STDAPI
  83. DllRegisterServer()
  84. {
  85. return RegisterServer(g_hinst);
  86. }
  87. STDAPI
  88. DllUnregisterServer()
  89. {
  90. return UnregisterServer();
  91. }
  92. /**************************************************************************
  93. Class CClassFactory
  94. ***************************************************************************/
  95. CClassFactory::CClassFactory()
  96. {
  97. m_cref = 1;
  98. g_uObjects++;
  99. }
  100. CClassFactory::~CClassFactory()
  101. {
  102. g_uObjects--;
  103. }
  104. STDMETHODIMP
  105. CClassFactory::QueryInterface(
  106. REFIID riid,
  107. LPVOID *ppv)
  108. {
  109. if (!ppv)
  110. return E_FAIL;
  111. *ppv = NULL;
  112. if (IsEqualIID(riid, IID_IUnknown))
  113. *ppv = static_cast<IClassFactory *>(this);
  114. else
  115. if (IsEqualIID(riid, IID_IClassFactory))
  116. *ppv = static_cast<IClassFactory *>(this);
  117. if (*ppv)
  118. {
  119. AddRef();
  120. return S_OK;
  121. }
  122. return E_NOINTERFACE;
  123. }
  124. STDMETHODIMP_(ULONG)
  125. CClassFactory::AddRef()
  126. {
  127. return ++m_cref;
  128. }
  129. STDMETHODIMP_(ULONG)
  130. CClassFactory::Release()
  131. {
  132. m_cref--;
  133. if (!m_cref)
  134. {
  135. delete this;
  136. return 0;
  137. }
  138. return m_cref;
  139. }
  140. STDMETHODIMP
  141. CClassFactory::CreateInstance(
  142. LPUNKNOWN pUnkOuter,
  143. REFIID riid,
  144. LPVOID * ppvObj)
  145. {
  146. HRESULT hr;
  147. void* pObj;
  148. if (!ppvObj)
  149. return E_FAIL;
  150. *ppvObj = NULL;
  151. // Our object does does not support aggregation, so we need to
  152. // fail if they ask us to do aggregation.
  153. if (pUnkOuter)
  154. return CLASS_E_NOAGGREGATION;
  155. pObj = new CRemotePage(g_hinst);
  156. if (!pObj)
  157. return E_OUTOFMEMORY;
  158. // QueryInterface will do the AddRef() for us, so we do not
  159. // do it in this function
  160. hr = ((LPUNKNOWN)pObj)->QueryInterface(riid, ppvObj);
  161. ((LPUNKNOWN)pObj)->Release();
  162. return hr;
  163. }
  164. STDMETHODIMP
  165. CClassFactory::LockServer(
  166. BOOL fLock)
  167. {
  168. if (fLock)
  169. {
  170. g_uSrvLock++;
  171. }
  172. else
  173. {
  174. if(g_uSrvLock>0)
  175. {
  176. g_uSrvLock--;
  177. }
  178. }
  179. return S_OK;
  180. }