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.

436 lines
9.6 KiB

  1. //+-------------------------------------------------------------------
  2. //
  3. // File: comclass.cpp
  4. //
  5. // Contents: Test DLL class code that can be used for both apt
  6. // model and single threaded application
  7. //
  8. // Classes:
  9. //
  10. // Functions:
  11. //
  12. // History: 03-Nov-94 Ricksa
  13. //
  14. //--------------------------------------------------------------------
  15. #undef _UNICODE
  16. #undef UNICODE
  17. #include <windows.h>
  18. #include <ole2.h>
  19. #include <comclass.h>
  20. // Global count of
  21. ULONG g_UseCount = 0;
  22. //+-------------------------------------------------------------------
  23. //
  24. // Class: CBasicBndCF
  25. //
  26. // Synopsis: Class Factory for CBasicBnd
  27. //
  28. // Methods: IUnknown - QueryInterface, AddRef, Release
  29. // IClassFactory - CreateInstance
  30. //
  31. // History: 03-Nov-94 Ricksa Created
  32. //
  33. //--------------------------------------------------------------------
  34. class FAR CBasicBndCF: public IClassFactory
  35. {
  36. public:
  37. // Constructor/Destructor
  38. CBasicBndCF(REFCLSID rclsid);
  39. ~CBasicBndCF();
  40. // IUnknown
  41. STDMETHODIMP QueryInterface(REFIID iid, void FAR * FAR * ppv);
  42. STDMETHODIMP_(ULONG) AddRef(void);
  43. STDMETHODIMP_(ULONG) Release(void);
  44. // IClassFactory
  45. STDMETHODIMP CreateInstance(
  46. IUnknown FAR* pUnkOuter,
  47. REFIID iidInterface,
  48. void FAR* FAR* ppv);
  49. STDMETHODIMP LockServer(BOOL fLock);
  50. private:
  51. ULONG _cRefs;
  52. REFCLSID _rclsid;
  53. DWORD _dwThreadId;
  54. };
  55. //+-------------------------------------------------------------------
  56. //
  57. // Class: CBasicBnd
  58. //
  59. // Synopsis: Test class CBasicBnd
  60. //
  61. // Methods:
  62. //
  63. // History: 03-Nov-94 Ricksa Created
  64. //
  65. //--------------------------------------------------------------------
  66. class FAR CBasicBnd: public IPersist
  67. {
  68. public:
  69. // Constructor/Destructor
  70. CBasicBnd(REFCLSID rclsd, DWORD dwThreadId);
  71. ~CBasicBnd();
  72. // IUnknown
  73. STDMETHODIMP QueryInterface(REFIID iid, void FAR * FAR * ppv);
  74. STDMETHODIMP_(ULONG) AddRef(void);
  75. STDMETHODIMP_(ULONG) Release(void);
  76. // IPersist - only thing we implement because it
  77. // gives us a nice way to figure who we are talking to.
  78. STDMETHODIMP GetClassID(LPCLSID lpClassID);
  79. private:
  80. ULONG _cRefs;
  81. REFCLSID _rclsid;
  82. DWORD _dwThreadId;
  83. };
  84. extern "C" BOOL WINAPI DllMain(
  85. HANDLE,
  86. DWORD,
  87. LPVOID)
  88. {
  89. static BOOL fFirst = TRUE;
  90. if (fFirst)
  91. {
  92. InitDll();
  93. fFirst = FALSE;
  94. }
  95. return TRUE;
  96. }
  97. //+-------------------------------------------------------------------
  98. //
  99. // Function: DllGetClassObject
  100. //
  101. // Synopsis: Called by client (from within BindToObject et al)
  102. // interface requested should be IUnknown or IClassFactory -
  103. // Creates ClassFactory object and returns pointer to it
  104. //
  105. // Arguments: REFCLSID clsid - class id
  106. // REFIID iid - interface id
  107. // void FAR* FAR* ppv- pointer to class factory interface
  108. //
  109. // Returns: TRUE
  110. //
  111. // History: 03-Nov-94 Ricksa Created
  112. //
  113. //--------------------------------------------------------------------
  114. STDAPI DllGetClassObject(REFCLSID clsid, REFIID iid, void FAR* FAR* ppv)
  115. {
  116. if (!IsEqualGUID(iid, IID_IUnknown)
  117. && !IsEqualGUID(iid, IID_IClassFactory))
  118. {
  119. return E_NOINTERFACE;
  120. }
  121. if (IsEqualGUID(clsid, clsidServer))
  122. {
  123. *ppv = new CBasicBndCF(clsidServer);
  124. return (*ppv != NULL) ? S_OK : E_OUTOFMEMORY;
  125. }
  126. return E_FAIL;
  127. }
  128. STDAPI DllCanUnloadNow(void)
  129. {
  130. return (g_UseCount == 0)
  131. ? S_OK
  132. : S_FALSE;
  133. }
  134. //+-------------------------------------------------------------------
  135. //
  136. // Member: CBasicBndCF::CBasicBndCF()
  137. //
  138. // Synopsis: The constructor for CBAsicBnd.
  139. //
  140. // Arguments: None
  141. //
  142. // History: 21-Nov-92 SarahJ Created
  143. //
  144. //--------------------------------------------------------------------
  145. CBasicBndCF::CBasicBndCF(REFCLSID rclsid)
  146. : _cRefs(1), _rclsid(rclsid), _dwThreadId(GetCurrentThreadId())
  147. {
  148. g_UseCount++;
  149. }
  150. //+-------------------------------------------------------------------
  151. //
  152. // Member: CBasicBnd::~CBasicBndCF()
  153. //
  154. // Synopsis: The destructor for CBasicCF.
  155. //
  156. // History: 21-Nov-92 SarahJ Created
  157. //
  158. //--------------------------------------------------------------------
  159. CBasicBndCF::~CBasicBndCF()
  160. {
  161. g_UseCount--;
  162. return;
  163. }
  164. //+-------------------------------------------------------------------
  165. //
  166. // Method: CBasicBndCF::QueryInterface
  167. //
  168. // Synopsis: Only IUnknown and IClassFactory supported
  169. //
  170. //--------------------------------------------------------------------
  171. STDMETHODIMP CBasicBndCF::QueryInterface(REFIID iid, void FAR * FAR * ppv)
  172. {
  173. if (IsEqualGUID(iid, IID_IUnknown) || IsEqualGUID(iid, IID_IClassFactory))
  174. {
  175. *ppv = this;
  176. AddRef();
  177. return S_OK;
  178. }
  179. else
  180. {
  181. *ppv = NULL;
  182. return E_NOINTERFACE;
  183. }
  184. }
  185. STDMETHODIMP_(ULONG) CBasicBndCF::AddRef(void)
  186. {
  187. return ++_cRefs;
  188. }
  189. STDMETHODIMP_(ULONG) CBasicBndCF::Release(void)
  190. {
  191. ULONG cRefs = --_cRefs;
  192. if (cRefs == 0)
  193. {
  194. delete this;
  195. }
  196. return cRefs;
  197. }
  198. //+-------------------------------------------------------------------
  199. //
  200. // Method: CBasicBndCF::CreateInstance
  201. //
  202. // Synopsis: This is called by Binding process to create the
  203. // actual class object
  204. //
  205. //--------------------------------------------------------------------
  206. STDMETHODIMP CBasicBndCF::CreateInstance(
  207. IUnknown FAR* pUnkOuter,
  208. REFIID iidInterface,
  209. void FAR* FAR* ppv)
  210. {
  211. if (GetCurrentThreadId() != _dwThreadId)
  212. {
  213. return E_UNEXPECTED;
  214. }
  215. HRESULT hresult = S_OK;
  216. if (pUnkOuter != NULL)
  217. {
  218. return CLASS_E_NOAGGREGATION;
  219. }
  220. CBasicBnd *pbb = new FAR CBasicBnd(_rclsid, _dwThreadId);
  221. if (pbb == NULL)
  222. {
  223. return E_OUTOFMEMORY;
  224. }
  225. hresult = pbb->QueryInterface(iidInterface, ppv);
  226. pbb->Release();
  227. return hresult;
  228. }
  229. //+-------------------------------------------------------------------
  230. //
  231. // Method: CBasicBndCF::LockServer
  232. //
  233. // Synopsis: Who knows what this is for?
  234. //
  235. //--------------------------------------------------------------------
  236. STDMETHODIMP CBasicBndCF::LockServer(BOOL fLock)
  237. {
  238. if (GetCurrentThreadId() != _dwThreadId)
  239. {
  240. return E_UNEXPECTED;
  241. }
  242. if (fLock)
  243. {
  244. g_UseCount++;
  245. }
  246. else
  247. {
  248. g_UseCount--;
  249. }
  250. return S_OK;
  251. }
  252. //+-------------------------------------------------------------------
  253. //
  254. // Member: CBasicBnd::CBasicBnd()
  255. //
  256. // Synopsis: The constructor for CBAsicBnd. I
  257. //
  258. // Arguments: None
  259. //
  260. // History: 21-Nov-92 SarahJ Created
  261. //
  262. //--------------------------------------------------------------------
  263. CBasicBnd::CBasicBnd(REFCLSID rclsid, DWORD dwThreadId)
  264. : _cRefs(1), _rclsid(rclsid), _dwThreadId(dwThreadId)
  265. {
  266. g_UseCount++;
  267. }
  268. //+-------------------------------------------------------------------
  269. //
  270. // Member: CBasicBnd::~CBasicBndObj()
  271. //
  272. // Synopsis: The destructor for CBAsicBnd.
  273. //
  274. // History: 21-Nov-92 SarahJ Created
  275. //
  276. //--------------------------------------------------------------------
  277. CBasicBnd::~CBasicBnd()
  278. {
  279. g_UseCount--;
  280. return;
  281. }
  282. //+-------------------------------------------------------------------
  283. //
  284. // Member: CBasicBnd::QueryInterface
  285. //
  286. // Returns: S_OK
  287. //
  288. // History: 21-Nov-92 SarahJ Created
  289. //
  290. //--------------------------------------------------------------------
  291. STDMETHODIMP CBasicBnd::QueryInterface(REFIID iid, void **ppv)
  292. {
  293. if (GetCurrentThreadId() != _dwThreadId)
  294. {
  295. return E_UNEXPECTED;
  296. }
  297. if (IsEqualGUID(iid, IID_IUnknown) || IsEqualGUID(iid, IID_IPersist))
  298. {
  299. *ppv = this;
  300. AddRef();
  301. return S_OK;
  302. }
  303. else
  304. {
  305. *ppv = NULL;
  306. return E_NOINTERFACE;
  307. }
  308. }
  309. //+-------------------------------------------------------------------
  310. //
  311. // Member: CBasicBnd::AddRef
  312. //
  313. // Synopsis: Standard stuff
  314. //
  315. // History: 21-Nov-92 SarahJ Created
  316. //
  317. //--------------------------------------------------------------------
  318. STDMETHODIMP_(ULONG) CBasicBnd::AddRef(void)
  319. {
  320. return _cRefs++;
  321. }
  322. //+-------------------------------------------------------------------
  323. //
  324. // Member: CBasicBnd::Release
  325. //
  326. // Synopsis: Standard stuff
  327. //
  328. // History: 21-Nov-92 SarahJ Created
  329. //
  330. //--------------------------------------------------------------------
  331. STDMETHODIMP_(ULONG) CBasicBnd::Release(void)
  332. {
  333. ULONG cRefs;
  334. if ((cRefs = --_cRefs) == 0)
  335. {
  336. delete this;
  337. }
  338. return cRefs;
  339. }
  340. //+-------------------------------------------------------------------
  341. //
  342. // Interface: IPersist
  343. //
  344. // Synopsis: IPersist interface methods
  345. // Need to return a valid class id here
  346. //
  347. // History: 21-Nov-92 SarahJ Created
  348. //
  349. STDMETHODIMP CBasicBnd::GetClassID(LPCLSID classid)
  350. {
  351. if (GetCurrentThreadId() != _dwThreadId)
  352. {
  353. return E_UNEXPECTED;
  354. }
  355. *classid = _rclsid;
  356. return S_OK;
  357. }