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.

305 lines
7.2 KiB

  1. //****************************************************************************
  2. //
  3. // BLClient sample for Microsoft Messenger SDK
  4. //
  5. // Module: BLClient.exe
  6. // File: clUtil.cpp
  7. // Content: Usefull clases for COM and Connection points
  8. //
  9. //
  10. // Copyright (c) Microsoft Corporation 1997-1998
  11. //
  12. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  13. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  14. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  15. // PARTICULAR PURPOSE.
  16. //****************************************************************************
  17. #include "pch.hxx"
  18. #include "clUtil.h"
  19. #include "msoert.h"
  20. // #include <instance.h>
  21. // #include "demand.h"
  22. #define ASSERT _ASSERTE
  23. //****************************************************************************
  24. //
  25. // CLASS RefCount
  26. //
  27. //****************************************************************************
  28. //****************************************************************************
  29. //
  30. // Constructor
  31. //
  32. //****************************************************************************
  33. RefCount::RefCount(void)
  34. {
  35. m_cRef = 1;
  36. // Assert(NULL != g_pInstance);
  37. // CoIncrementInit("RefCount::RefCount", MSOEAPI_START_SHOWERRORS, NULL, NULL);
  38. }
  39. //****************************************************************************
  40. //
  41. // Destructor
  42. //
  43. //****************************************************************************
  44. RefCount::~RefCount(void)
  45. {
  46. // CoDecrementInit("RefCount::RefCount", NULL);
  47. }
  48. //****************************************************************************
  49. //
  50. // ULONG STDMETHODCALLTYPE RefCount::AddRef(void)
  51. //
  52. //****************************************************************************
  53. ULONG STDMETHODCALLTYPE RefCount::AddRef(void)
  54. {
  55. ASSERT(m_cRef >= 0);
  56. InterlockedIncrement(&m_cRef);
  57. return (ULONG) m_cRef;
  58. }
  59. //****************************************************************************
  60. //
  61. // ULONG STDMETHODCALLTYPE RefCount::Release(void)
  62. //
  63. //****************************************************************************
  64. ULONG STDMETHODCALLTYPE RefCount::Release(void)
  65. {
  66. if (0 == InterlockedDecrement(&m_cRef))
  67. {
  68. delete this;
  69. return 0;
  70. }
  71. ASSERT(m_cRef > 0);
  72. return (ULONG) m_cRef;
  73. }
  74. //****************************************************************************
  75. //
  76. // CLASS CNotify
  77. //
  78. //****************************************************************************
  79. //****************************************************************************
  80. //
  81. // Constructor
  82. //
  83. //****************************************************************************
  84. CNotify::CNotify() :
  85. m_pcnpcnt(NULL),
  86. m_pcnp(NULL),
  87. m_dwCookie(0),
  88. m_pUnk(NULL)
  89. {
  90. }
  91. //****************************************************************************
  92. //
  93. // destructor
  94. //
  95. //****************************************************************************
  96. CNotify::~CNotify()
  97. {
  98. Disconnect(); // Make sure we're disconnected
  99. }
  100. //****************************************************************************
  101. //
  102. // HRESULT CNotify::Connect(IUnknown *pUnk, REFIID riid, IUnknown *pUnkN)
  103. //
  104. // Connects the sink to the container
  105. //
  106. //****************************************************************************
  107. HRESULT CNotify::Connect(IUnknown *pUnk, REFIID riid, IUnknown *pUnkN)
  108. {
  109. HRESULT hr;
  110. ASSERT(0 == m_dwCookie);
  111. // Get the connection container
  112. hr = pUnk->QueryInterface(IID_IConnectionPointContainer, (void **)&m_pcnpcnt);
  113. if (SUCCEEDED(hr))
  114. {
  115. // Find an appropriate connection point
  116. hr = m_pcnpcnt->FindConnectionPoint(riid, &m_pcnp);
  117. if (SUCCEEDED(hr))
  118. {
  119. ASSERT(NULL != m_pcnp);
  120. // Connect the sink object
  121. hr = m_pcnp->Advise((IUnknown *)pUnkN, &m_dwCookie);
  122. }
  123. }
  124. if (FAILED(hr))
  125. {
  126. m_dwCookie = 0;
  127. }
  128. else
  129. {
  130. m_pUnk = pUnk; // keep around for caller
  131. }
  132. return hr;
  133. }
  134. //****************************************************************************
  135. //
  136. // HRESULT CNotify::Disconnect (void)
  137. //
  138. // Disconnects the sink from the container
  139. //
  140. //****************************************************************************
  141. HRESULT CNotify::Disconnect (void)
  142. {
  143. if (0 != m_dwCookie)
  144. {
  145. // Disconnect the sink object
  146. m_pcnp->Unadvise(m_dwCookie);
  147. m_dwCookie = 0;
  148. m_pcnp->Release();
  149. m_pcnp = NULL;
  150. m_pcnpcnt->Release();
  151. m_pcnpcnt = NULL;
  152. m_pUnk = NULL;
  153. }
  154. return S_OK;
  155. }
  156. //****************************************************************************
  157. //
  158. // CLASS BSTRING
  159. //
  160. //****************************************************************************
  161. //****************************************************************************
  162. //
  163. // Constructor
  164. //
  165. //****************************************************************************
  166. // We don't support construction from an ANSI string in the Unicode build.
  167. #ifndef UNICODE
  168. BSTRING::BSTRING(LPCSTR lpcString)
  169. {
  170. m_bstr = NULL;
  171. // Compute the length of the required BSTR, including the null
  172. int cWC = MultiByteToWideChar(CP_ACP, 0, lpcString, -1, NULL, 0);
  173. if (cWC <= 0)
  174. return;
  175. // Allocate the BSTR, including the null
  176. m_bstr = SysAllocStringLen(NULL, cWC - 1); // SysAllocStringLen adds another 1
  177. ASSERT(NULL != m_bstr);
  178. if (NULL == m_bstr)
  179. {
  180. return;
  181. }
  182. // Copy the string
  183. MultiByteToWideChar(CP_ACP, 0, lpcString, -1, (LPWSTR) m_bstr, cWC);
  184. // Verify that the string is null terminated
  185. ASSERT(0 == m_bstr[cWC - 1]);
  186. }
  187. #endif // #ifndef UNICODE
  188. //****************************************************************************
  189. //
  190. // CLASS BTSTR
  191. //
  192. //****************************************************************************
  193. //****************************************************************************
  194. //
  195. // Constructor
  196. //
  197. //****************************************************************************
  198. BTSTR::BTSTR(BSTR bstr)
  199. {
  200. m_psz = LPTSTRfromBstr(bstr);
  201. }
  202. //****************************************************************************
  203. //
  204. // Destructor
  205. //
  206. //****************************************************************************
  207. BTSTR::~BTSTR()
  208. {
  209. if (NULL != m_psz)
  210. MemFree(m_psz);
  211. }
  212. //****************************************************************************
  213. //
  214. // LPTSTR LPTSTRfromBstr(BSTR bstr)
  215. //
  216. // Converts a BSTR to a LPTSTR
  217. //
  218. //****************************************************************************
  219. LPTSTR LPTSTRfromBstr(BSTR bstr)
  220. {
  221. if (NULL == bstr)
  222. return NULL;
  223. int cch = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)bstr, -1, NULL, 0, NULL, NULL);
  224. if (cch <= 0)
  225. return NULL;
  226. LPTSTR psz;
  227. if (!MemAlloc((void **)&psz, sizeof(TCHAR) * (cch+1)))
  228. return NULL;
  229. #ifndef UNICODE
  230. WideCharToMultiByte(CP_ACP, 0, (LPWSTR)bstr, -1, psz, cch+1, NULL, NULL);
  231. #else
  232. StrCpyN(psz, bstr, cch+1);
  233. #endif
  234. return psz;
  235. }