Source code of Windows XP (NT5)
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.

379 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: rpc16.cxx
  7. //
  8. // Contents: Maps the 16-bit IRpcChannel implementation to a 16-bit
  9. // implementation of IRpcChannelBuffer.
  10. //
  11. // This is required to support custom interface marshalling.
  12. //
  13. // History: 30-Mar-94 BobDay Adapted from OLELRPC.CPP
  14. //
  15. //--------------------------------------------------------------------------
  16. #include <headers.cxx>
  17. #pragma hdrstop
  18. #include <ole2ver.h>
  19. #include <ole2sp.h>
  20. #include "rpc16.hxx"
  21. interface IRpcChannel : public IUnknown
  22. {
  23. public:
  24. virtual HRESULT STDMETHODCALLTYPE GetStream
  25. (
  26. REFIID riid,
  27. int iMethod,
  28. BOOL fSend,
  29. BOOL fNoWait,
  30. DWORD size,
  31. IStream FAR * FAR *ppIStream
  32. ) = 0;
  33. virtual HRESULT STDMETHODCALLTYPE Call
  34. (
  35. IStream FAR * pIStream
  36. ) = 0;
  37. virtual HRESULT STDMETHODCALLTYPE GetDestCtx
  38. (
  39. DWORD *pdwDestContext,
  40. void **ppvDestContext
  41. ) = 0;
  42. virtual HRESULT STDMETHODCALLTYPE IsConnected
  43. (
  44. void
  45. ) = 0;
  46. };
  47. //
  48. // 16-bit IRpcChannel interface, stream-based
  49. //
  50. // This is the interface seen by the 16-bit proxy implementations
  51. //
  52. class CRpcChannel : public IRpcChannel
  53. {
  54. public:
  55. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,void **ppvObject);
  56. ULONG STDMETHODCALLTYPE AddRef(void);
  57. ULONG STDMETHODCALLTYPE Release(void);
  58. STDMETHOD(GetStream)(REFIID iid, int iMethod, BOOL fSend,
  59. BOOL fNoWait, DWORD size, IStream FAR* FAR* ppIStream);
  60. STDMETHOD(Call)(IStream FAR* pIStream);
  61. STDMETHOD(GetDestCtx)(DWORD FAR* lpdwDestCtx, LPVOID FAR* lplpvDestCtx);
  62. STDMETHOD(IsConnected)(void);
  63. CRpcChannel FAR *CRpcChannel::Create( CRpcChannelBuffer FAR *prcb );
  64. private:
  65. CRpcChannel::CRpcChannel( CRpcChannelBuffer FAR *prcb );
  66. CRpcChannel::~CRpcChannel( void );
  67. ULONG m_refs; // Reference count
  68. CRpcChannelBuffer FAR * m_prcb; // ChannelBuffer to talk through
  69. };
  70. //+-------------------------------------------------------------------------
  71. //
  72. // Implementation of CRpcChannel
  73. //
  74. //--------------------------------------------------------------------------
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Function: Create, public
  78. //
  79. // Synopsis: Creates a 16-bit CRpcChannel and gives it the
  80. // IRpcChannelBuffer that in needs to call.
  81. //
  82. // Arguments: [prcb] - IRpcChannelBuffer to call when called
  83. //
  84. // Returns: IRpcChannel *, Null on failure
  85. //
  86. // History: 30-Mar-94 BobDay Created
  87. //
  88. //----------------------------------------------------------------------------
  89. CRpcChannel FAR *CRpcChannel::Create( CRpcChannelBuffer FAR *prcb )
  90. {
  91. CRpcChannel FAR *pRC;
  92. pRC = new CRpcChannel( prcb );
  93. return( pRC );
  94. }
  95. //+---------------------------------------------------------------------------
  96. //
  97. // Function: Constructor, private
  98. //
  99. // Synopsis: Creates a 16-bit CRpcChannel and gives it the
  100. // IRpcChannelBuffer that in needs to call.
  101. //
  102. // Arguments: [prcb] - IRpcChannelBuffer to call when called
  103. //
  104. // Returns: IRpcChannel *, Null on failure
  105. //
  106. // History: 30-Mar-94 BobDay Created
  107. //
  108. //----------------------------------------------------------------------------
  109. CRpcChannel::CRpcChannel( CRpcChannelBuffer FAR *prcb )
  110. {
  111. m_refs = 1;
  112. m_prcb = prcb; // Save it and addref it
  113. if ( m_prcb != NULL )
  114. {
  115. m_prcb->AddRef();
  116. }
  117. }
  118. //+---------------------------------------------------------------------------
  119. //
  120. // Function: Destructor, private
  121. //
  122. // Synopsis: Destroys the CRpcChannel object
  123. //
  124. // Arguments: none
  125. //
  126. // Returns: nothing
  127. //
  128. // History: 30-Mar-94 BobDay Created
  129. //
  130. //----------------------------------------------------------------------------
  131. CRpcChannel::~CRpcChannel( void )
  132. {
  133. if ( m_prcb != NULL )
  134. {
  135. m_prcb->Release(); // Release it and zero it
  136. m_prcb = NULL;
  137. }
  138. }
  139. //+---------------------------------------------------------------------------
  140. //
  141. // Function: QueryInterface, public
  142. //
  143. // Synopsis: Allows querying for other interfaces supported by this
  144. // object
  145. //
  146. // Arguments: [iidInterface] - iid to query for
  147. // [ppv] - interface pointer returned
  148. //
  149. // Returns: nothing
  150. //
  151. // History: 30-Mar-94 BobDay Created
  152. //
  153. //----------------------------------------------------------------------------
  154. STDMETHODIMP CRpcChannel::QueryInterface(
  155. REFIID iidInterface,
  156. void FAR* FAR* ppvObj )
  157. {
  158. HRESULT hresult;
  159. // Two interfaces supported: IUnknown, IRpcChannel
  160. if (iidInterface == IID_IUnknown || iidInterface == IID_IRpcChannel)
  161. {
  162. m_refs++; // A pointer to this object is returned
  163. *ppvObj = this;
  164. hresult = NOERROR;
  165. }
  166. else
  167. {
  168. *ppvObj = NULL;
  169. hresult = ResultFromScode(E_NOINTERFACE);
  170. }
  171. return hresult;
  172. }
  173. //+---------------------------------------------------------------------------
  174. //
  175. // Function: AddRef, public
  176. //
  177. // Synopsis: Increments object reference count, called when object is
  178. // referenced by another pointer/client.
  179. //
  180. // Arguments: none
  181. //
  182. // Returns: new reference count
  183. //
  184. // History: 30-Mar-94 BobDay Created
  185. //
  186. //----------------------------------------------------------------------------
  187. STDMETHODIMP_(ULONG) CRpcChannel::AddRef(void)
  188. {
  189. return ++m_refs;
  190. }
  191. //+---------------------------------------------------------------------------
  192. //
  193. // Function: Release, public
  194. //
  195. // Synopsis: Decrements object reference count and handles necessary
  196. // self-destruction and sub-releasing, called when object is
  197. // no longer referenced by another pointer/client.
  198. //
  199. // Arguments: none
  200. //
  201. // Returns: new reference count
  202. //
  203. // History: 30-Mar-94 BobDay Created
  204. //
  205. //----------------------------------------------------------------------------
  206. STDMETHODIMP_(ULONG) CRpcChannel::Release(void)
  207. {
  208. if (--m_refs != 0) // Object still alive?
  209. {
  210. return m_refs;
  211. }
  212. delete this; // Suicide
  213. return 0;
  214. }
  215. //+---------------------------------------------------------------------------
  216. //
  217. // Function: GetStream, public
  218. //
  219. // Synopsis: Create an IStream onto which the client can write in
  220. // preparation for making an RPC call with the stream contents
  221. // as parameters.
  222. //
  223. // Arguments: [iid] - IID of interface being RPC'd
  224. // [iMethod] - Method # within interface
  225. // [fSend] - Should we use SendMessage?
  226. // [fNoWait] - Should we be asynchronous?
  227. // [size] - Initial size of stream
  228. // [ppIStream] - Output IStream
  229. //
  230. // Returns: HRESULT for success/failure
  231. // [ppIStream] - IStream for writing parameters
  232. //
  233. // History: 30-Mar-94 BobDay Created
  234. //
  235. //----------------------------------------------------------------------------
  236. STDMETHODIMP CRpcChannel::GetStream(
  237. REFIID iid,
  238. int iMethod,
  239. BOOL fSend,
  240. BOOL fNoWait,
  241. DWORD size,
  242. IStream FAR* FAR* ppIStream )
  243. {
  244. CPkt FAR* pCPkt;
  245. // no point in allowing this to succeed if we are not connected
  246. if ( IsConnected() != NOERROR )
  247. {
  248. // Connection terminated (server died or disconnectd)
  249. return ResultFromScode(RPC_E_CONNECTION_TERMINATED);
  250. }
  251. pCPkt = CPkt::CreateForCall(this,iid,iMethod,fSend,fNoWait,size);
  252. if (pCPkt == NULL)
  253. {
  254. *ppIStream = NULL;
  255. return ResultFromScode(E_OUTOFMEMORY);
  256. }
  257. if (pCPkt->QueryInterface(IID_IStream,(void FAR* FAR*) ppIStream)
  258. != NOERROR)
  259. {
  260. pCPkt->Release();
  261. return ResultFromScode(E_OUTOFMEMORY);
  262. }
  263. pCPkt->SetRpcChannelBuffer( m_prcb );
  264. pCPkt->Release();
  265. return NOERROR;
  266. }
  267. //+---------------------------------------------------------------------------
  268. //
  269. // Function: Call, public
  270. //
  271. // Synopsis: Sends a call to a remote procedure. Previously, all of the
  272. // parameters for the procedure were serialized into the IStream
  273. // which was passed in.
  274. //
  275. // Arguments: [pIStream] - IStream for parameters to procedure
  276. //
  277. // Returns: HRESULT for success/failure of call or procedure
  278. //
  279. // History: 30-Mar-94 BobDay Created
  280. //
  281. //----------------------------------------------------------------------------
  282. STDMETHODIMP CRpcChannel::Call(
  283. IStream FAR * pIStream )
  284. {
  285. HRESULT hresult;
  286. CPkt FAR * pCPkt;
  287. hresult = pIStream->QueryInterface(IID_CPkt,(void FAR* FAR*) &pCPkt);
  288. if (hresult != NOERROR)
  289. {
  290. return ResultFromScode(E_INVALIDARG);
  291. }
  292. return pCPkt->CallRpcChannelBuffer();
  293. }
  294. //+---------------------------------------------------------------------------
  295. //
  296. // Function: GetDestCtx, public
  297. //
  298. // Synopsis: According to the code in OLELRPC.CPP, this is all this code
  299. // does, presumably, it might be expanded in the future.
  300. //
  301. // Arguments:
  302. //
  303. // Returns:
  304. //
  305. // History: 30-Mar-94 BobDay Created
  306. //
  307. //----------------------------------------------------------------------------
  308. STDMETHODIMP CRpcChannel::GetDestCtx(
  309. DWORD FAR * lpdwDestCtx,
  310. LPVOID FAR * lplpvDestCtx )
  311. {
  312. *lpdwDestCtx = NULL;
  313. if (lplpvDestCtx)
  314. {
  315. *lplpvDestCtx = NULL;
  316. }
  317. return NOERROR;
  318. }
  319. //+---------------------------------------------------------------------------
  320. //
  321. // Function: IsConnected, public
  322. //
  323. // Synopsis: According to the code in OLELRPC.CPP, this is all this code
  324. // does, presumably, it might be expanded in the future.
  325. //
  326. // Arguments: none
  327. //
  328. // Returns: HRESULT indicating connection status
  329. //
  330. // History: 30-Mar-94 BobDay Created
  331. //
  332. //----------------------------------------------------------------------------
  333. STDMETHODIMP CRpcChannel::IsConnected( void )
  334. {
  335. if ( m_prcb == NULL )
  336. {
  337. return ResultFromScode(E_UNEXPECTED);
  338. }
  339. return m_prcb->IsConnected();
  340. }