Share via


封送全局函数

这些函数支持封送数据,并将封送数据转换为接口指针。

重要

下表中列出的函数不能在 Windows 运行时执行的应用程序中使用。

名称 描述
AtlFreeMarshalStream 释放封送数据和 IStream 指针。
AtlMarshalPtrInProc 创建新的流对象并封送指定的接口指针。
AtlUnmarshalPtr 将流的封送数据转换为接口指针。

要求:

标头:atlbase.h

AtlFreeMarshalStream

释放流中的封送数据,然后释放流指针。

HRESULT AtlFreeMarshalStream(IStream* pStream);

参数

pStream
[in] 指向用于封送的流的 IStream 接口的指针。

示例

请参阅 AtlMarshalPtrInProc 的示例。

AtlMarshalPtrInProc

创建新的流对象,将代理的 CLSID 写入流,并通过将初始化代理所需的数据写入流来封送指定接口指针。

HRESULT AtlMarshalPtrInProc(
    IUnknown* pUnk,
    const IID& iid,
    IStream** ppStream);

参数

pUnk
[in] 指向要封送的接口的指针。

iid
[in] 要封送的接口的 GUID。

ppStream
[out] 指向用于封送处理的新流对象上的 IStream 接口的指针。

返回值

标准 HRESULT 值。

备注

设置 MSHLFLAGS_TABLESTRONG 标志,以便指针可以封送到多个流。 指针也可以多次取消封送处理。

如果封送处理失败,则会释放流指针。

AtlMarshalPtrInProc 只能在指向进程内对象的指针上使用。

示例

//marshaling interface from one thread to another

//IStream ptr to hold serialized presentation of interface ptr
IStream* g_pStm;

//forward declaration
DWORD WINAPI ThreadProc(LPVOID lpParameter);

HRESULT WriteInterfacePtrToStream(IMyCircle *pCirc)
{
   //marshal the interface ptr to another thread
   //pCirc has to be pointer to actual object & not a proxy
   HRESULT hr = AtlMarshalPtrInProc(pCirc, IID_IMyCircle, &g_pStm);

   //m_dwThreadID is a DWORD holding thread ID of thread being created.
   CreateThread(NULL, 0, ThreadProc, NULL, 0, &m_dwThreadID);
   return hr;
}

DWORD WINAPI ThreadProc(LPVOID /*lpParameter*/)
{
   // CoInitializeEx is per thread, so initialize COM on this thread
   // (required by AtlUnmarshalPtr)
   HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
   if (SUCCEEDED(hr))
   {
      IMyCircle* pCirc;

      //unmarshal IMyCircle ptr from the stream
      hr = AtlUnmarshalPtr(g_pStm, IID_IMyCircle, (IUnknown**)&pCirc);

      // use IMyCircle ptr to call its methods
      double center;
      pCirc->get_XCenter(&center);

      //release the stream if no other thread requires it 
      //to unmarshal the IMyCircle interface pointer
      hr = AtlFreeMarshalStream(g_pStm);

      CoUninitialize();
   }

   return hr;
}

AtlUnmarshalPtr

将流的封送数据转换为可由客户端使用的接口指针。

HRESULT AtlUnmarshalPtr(
    IStream* pStream,
    const IID& iid,
    IUnknown** ppUnk);

参数

pStream
[in] 指向未封送处理的流。

iid
[in] 未封送处理的接口的 GUID。

ppUnk
[out] 指向未封送处理接口的指针。

返回值

标准 HRESULT 值。

示例

请参阅 AtlMarshalPtrInProc 的示例。

另请参阅

函数