olemon.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_OLE_SEG
  12. #pragma code_seg(AFX_OLE_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. #pragma inline_depth(0)
  20. /////////////////////////////////////////////////////////////////////////////
  21. // _AfxBindHost for CMonikerFile implementation
  22. class _AfxBindHost: public IBindHost
  23. {
  24. private:
  25. long m_dwRef;
  26. public:
  27. inline _AfxBindHost() : m_dwRef(0) {}
  28. inline ~_AfxBindHost() { ASSERT(m_dwRef == 0); }
  29. STDMETHOD_(ULONG, AddRef)()
  30. {
  31. return InterlockedIncrement(&m_dwRef);
  32. }
  33. STDMETHOD_(ULONG, Release)()
  34. {
  35. unsigned long lResult = InterlockedDecrement(&m_dwRef);
  36. if (lResult == 0)
  37. delete this;
  38. return lResult;
  39. }
  40. STDMETHOD(QueryInterface)(REFIID iid, void** ppvObject)
  41. {
  42. if (!ppvObject)
  43. return E_POINTER;
  44. // check for the interfaces this object knows about
  45. if (iid == IID_IUnknown || iid == IID_IBindHost)
  46. {
  47. *ppvObject = (IBindHost*)this;
  48. InterlockedIncrement(&m_dwRef);
  49. return S_OK;
  50. }
  51. // otherwise, incorrect IID, and thus error
  52. return E_NOINTERFACE;
  53. }
  54. STDMETHOD(CreateMoniker)(
  55. /* [in] */ LPOLESTR szName,
  56. /* [in] */ IBindCtx __RPC_FAR *pBC,
  57. /* [out] */ IMoniker __RPC_FAR *__RPC_FAR *ppmk,
  58. /* [in] */ DWORD dwReserved)
  59. {
  60. UNUSED_ALWAYS(dwReserved);
  61. UNUSED_ALWAYS(pBC);
  62. if (!szName || !ppmk) return E_POINTER;
  63. if (!*szName) return E_INVALIDARG;
  64. *ppmk = NULL;
  65. HRESULT hr = S_OK;
  66. hr = CreateURLMoniker(NULL, szName, ppmk);
  67. if (SUCCEEDED(hr) && !*ppmk)
  68. hr = E_FAIL;
  69. return hr;
  70. }
  71. STDMETHOD(MonikerBindToStorage)(
  72. /* [in] */ IMoniker __RPC_FAR *pMk,
  73. /* [in] */ IBindCtx __RPC_FAR *pBC,
  74. /* [in] */ IBindStatusCallback __RPC_FAR *pBSC,
  75. /* [in] */ REFIID riid,
  76. /* [out] */ void __RPC_FAR *__RPC_FAR *ppvObj)
  77. {
  78. if (!pMk || !ppvObj) return E_POINTER;
  79. *ppvObj = NULL;
  80. HRESULT hr = S_OK;
  81. IPTR(IBindCtx) BindCtx;
  82. if (pBC)
  83. {
  84. BindCtx = pBC;
  85. if (pBSC)
  86. {
  87. #ifdef _DEBUG
  88. IPTR(IBindStatusCallback) pBSCPrev;
  89. #endif
  90. hr = RegisterBindStatusCallback(BindCtx, pBSC,
  91. #ifdef _DEBUG
  92. &pBSCPrev,
  93. #else
  94. NULL,
  95. #endif
  96. 0);
  97. ASSERT(!pBSCPrev);
  98. if (FAILED(hr))
  99. return hr;
  100. }
  101. }
  102. else
  103. {
  104. if (pBSC)
  105. hr = CreateAsyncBindCtx(0, pBSC, NULL, &BindCtx);
  106. else
  107. hr = CreateBindCtx(0, &BindCtx);
  108. if (SUCCEEDED(hr) && !BindCtx)
  109. hr = E_FAIL;
  110. if (FAILED(hr))
  111. return hr;
  112. }
  113. return pMk->BindToStorage(BindCtx, NULL, riid, ppvObj);
  114. }
  115. private:
  116. STDMETHOD(MonikerBindToObject)(
  117. /* [in] */ IMoniker __RPC_FAR *pMk,
  118. /* [in] */ IBindCtx __RPC_FAR *pBC,
  119. /* [in] */ IBindStatusCallback __RPC_FAR *pBSC,
  120. /* [in] */ REFIID riid,
  121. /* [out] */ void __RPC_FAR *__RPC_FAR *ppvObj)
  122. {
  123. ASSERT(FALSE);
  124. UNUSED_ALWAYS(pMk);
  125. UNUSED_ALWAYS(pBC);
  126. UNUSED_ALWAYS(pBSC);
  127. UNUSED_ALWAYS(riid);
  128. UNUSED_ALWAYS(ppvObj);
  129. return E_NOTIMPL;
  130. }
  131. };
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CMonikerFile implementation
  134. CMonikerFile::~CMonikerFile()
  135. {
  136. ASSERT_VALID(this);
  137. Close();
  138. }
  139. void CMonikerFile::Flush()
  140. {
  141. ASSERT_VALID(this);
  142. ASSERT(GetStream() != NULL);
  143. GetStream()->Commit(0);
  144. }
  145. BOOL CMonikerFile::Open(LPCTSTR lpszUrl, IBindHost* pBindHost,
  146. IBindStatusCallback* pBSC, IBindCtx* pBindCtx, CFileException* pError)
  147. {
  148. ASSERT_VALID(this);
  149. Close(); // These objects are reopenable
  150. return Attach(lpszUrl, pBindHost, pBSC, pBindCtx, pError);
  151. }
  152. BOOL CMonikerFile::Attach(LPCTSTR lpszUrl, IBindHost* pBindHost,
  153. IBindStatusCallback* pBSC, IBindCtx* pBindCtx, CFileException* pError)
  154. {
  155. ASSERT(!m_Moniker);
  156. ASSERT(!GetStream());
  157. USES_CONVERSION;
  158. ASSERT(NULL == lpszUrl || AfxIsValidString(lpszUrl));
  159. ASSERT(NULL == pError ||
  160. AfxIsValidAddress(pError, sizeof(CFileException)));
  161. ASSERT(NULL != pBindHost);
  162. // Check for empty path
  163. if (!lpszUrl || !*lpszUrl)
  164. {
  165. if (pError)
  166. {
  167. pError->m_cause=CFileException::badPath;
  168. pError->m_strFileName=lpszUrl;
  169. }
  170. return FALSE;
  171. }
  172. // Create the moniker
  173. HRESULT hr;
  174. IPTR(IMoniker) pMoniker;
  175. hr = pBindHost->CreateMoniker(T2OLE((LPTSTR) lpszUrl), pBindCtx,
  176. reinterpret_cast<IMoniker**>(&pMoniker), 0);
  177. if (FAILED(hr))
  178. {
  179. if (pError) _AfxFillOleFileException(pError, hr);
  180. return FALSE;
  181. }
  182. return Attach(pMoniker, pBindHost, pBSC, pBindCtx, pError);
  183. }
  184. BOOL CMonikerFile::Open(LPCTSTR lpszUrl, CFileException* pError)
  185. {
  186. IPTR(IBindHost) pBindHost(CreateBindHost(), FALSE);
  187. IPTR(IBindCtx) pBindCtx(CreateBindContext(pError), FALSE);
  188. return Open(lpszUrl, pBindHost, NULL, pBindCtx, pError);
  189. }
  190. BOOL CMonikerFile::Open(IMoniker* pMoniker, CFileException* pError)
  191. {
  192. Close(); // These objects are reopenable
  193. IPTR(IBindHost) pBindHost(CreateBindHost(), FALSE);
  194. IPTR(IBindCtx) pBindCtx(CreateBindContext(pError), FALSE);
  195. return Attach(pMoniker, pBindHost, NULL, pBindCtx, pError);
  196. }
  197. BOOL CMonikerFile::Open(IMoniker* pMoniker, IBindHost* pBindHost,
  198. IBindStatusCallback* pBSC, IBindCtx* pBindCtx, CFileException* pError)
  199. {
  200. Close();
  201. return Attach(pMoniker, pBindHost, pBSC, pBindCtx, pError);
  202. }
  203. BOOL CMonikerFile::Attach(IMoniker* pMoniker, IBindHost* pBindHost,
  204. IBindStatusCallback* pBSC, IBindCtx* pBindCtx, CFileException* pError)
  205. {
  206. ASSERT_VALID(this);
  207. ASSERT(pMoniker);
  208. ASSERT(!m_Moniker);
  209. ASSERT(!GetStream());
  210. m_Moniker=pMoniker;
  211. IPTR(IStream) pStream;
  212. HRESULT hr=pBindHost->MonikerBindToStorage(pMoniker, pBindCtx, pBSC,
  213. IID_IStream, reinterpret_cast<void**>(&pStream));
  214. if (FAILED(hr))
  215. {
  216. if (pError) _AfxFillOleFileException(pError, hr);
  217. return FALSE;
  218. }
  219. // If this is really a CAsyncMonikerFile, then we may have attached to the stream
  220. // within MonikerBindToStorage, in which case we want to avoid doing so here.
  221. if (pStream.GetInterfacePtr() && !GetStream())
  222. {
  223. // Attach this to the stream, transferring the reference
  224. // COleStreamFile::Attach doesn't increment pStream's refcount
  225. COleStreamFile::Attach(pStream);
  226. pStream.Detach();
  227. }
  228. return PostBindToStream(pError);
  229. }
  230. void CMonikerFile::Close()
  231. {
  232. if (m_Moniker.GetInterfacePtr())
  233. m_Moniker.Release();
  234. COleStreamFile::Close();
  235. }
  236. BOOL CMonikerFile::Detach(CFileException* pError)
  237. {
  238. ASSERT_VALID(this);
  239. ASSERT(!!m_Moniker);
  240. TRY
  241. {
  242. Close();
  243. }
  244. CATCH (CFileException, e)
  245. {
  246. if (pError)
  247. {
  248. pError->m_cause=e->m_cause;
  249. pError->m_lOsError=e->m_lOsError;
  250. pError->m_strFileName=e->m_strFileName;
  251. }
  252. DELETE_EXCEPTION(e);
  253. return FALSE;
  254. }
  255. END_CATCH;
  256. return TRUE;
  257. }
  258. IBindHost* CMonikerFile::CreateBindHost()
  259. {
  260. IBindHost* pBindHost = new _AfxBindHost();
  261. pBindHost->AddRef();
  262. return pBindHost;
  263. }
  264. IBindCtx* CMonikerFile::CreateBindContext(CFileException* pError)
  265. {
  266. UNUSED_ALWAYS(pError);
  267. return NULL;
  268. }
  269. // So that CMonikerFile can check for a null pStream and CAsyncMonikerFile can ignore it
  270. BOOL CMonikerFile::PostBindToStream(CFileException* pError)
  271. {
  272. if (!GetStream())
  273. {
  274. if (pError) _AfxFillOleFileException(pError, E_UNEXPECTED);
  275. TRY
  276. {
  277. Close();
  278. }
  279. CATCH_ALL(e)
  280. {
  281. DELETE_EXCEPTION(e);
  282. }
  283. END_CATCH_ALL
  284. return FALSE;
  285. }
  286. return TRUE;
  287. }
  288. /////////////////////////////////////////////////////////////////////////////
  289. // CMonikerFile diagnostics
  290. #ifdef _DEBUG
  291. void CMonikerFile::AssertValid() const
  292. {
  293. COleStreamFile::AssertValid();
  294. }
  295. void CMonikerFile::Dump(CDumpContext& dc) const
  296. {
  297. COleStreamFile::Dump(dc);
  298. dc << "\nm_Moniker = " << m_Moniker.GetInterfacePtr();
  299. dc << "\n";
  300. }
  301. #endif
  302. ////////////////////////////////////////////////////////////////////////////
  303. #ifndef _AFX_ENABLE_INLINES
  304. // expand inlines for OLE general APIs
  305. static char _szAfxOleInl[] = "afxole.inl";
  306. #undef THIS_FILE
  307. #define THIS_FILE _szAfxOleInl
  308. #define _AFXOLEMONIKER_INLINE
  309. #include "afxole.inl"
  310. #endif //!_AFX_ENABLE_INLINES
  311. #ifdef AFX_INIT_SEG
  312. #pragma code_seg(AFX_INIT_SEG)
  313. #endif
  314. IMPLEMENT_DYNAMIC(CMonikerFile, COleStreamFile)
  315. ////////////////////////////////////////////////////////////////////////////