COM 使用 CoMarshalInterThreadInterfaceInStream 编组接口
COM using CoMarshalInterThreadInterfaceInStream to marshal interface
我有一个在 C++ 中注册函数和接口的方法,我正在使用 CoMarshalInterThreadInterfaceInStream
将接口指针编组到另一个线程中的方法。
在RegisterFunction
中:
HRESULT hr = CoMarshalInterThreadInterfaceInStream(IID_Function, function, &stream);
在 GetFunction
发布:
HRESULT hr = CoGetInterfaceAndReleaseStream(stream, IID_Function, reinterpret_cast<void**>(&function));
由于我会多次调用 GetFunction
而 CoGetInterfaceAndReleaseStream
只释放一次流,我如何保存流以再次使用它?
我尝试使用 IGlobalInterfaceTable
,但无法正常工作。
我在RegisterFunction
成功注册了界面:
DWORD dwCookie = 0;
int a = 0;
if (pGIT == NULL) {
HRESULT hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&pGIT);
if (hr != S_OK) {
throw _com_error(hr);
}
}
if (dwCookie == 0) {
HRESULT hr = pGIT->RegisterInterfaceInGlobal(function, IID_Function, &dwCookie);
if (hr != S_OK) {
throw _com_error(hr);
}
}
但是当我试图在 GetFunction
中检索它时:
IGlobalInterfaceTable* GIT = NULL;
if (GIT == NULL) {
hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&GIT);
if (FAILED(hr)) {
exit(0);
}
}
hr = pGIT->GetInterfaceFromGlobal(dwCookie, IID_Function, (void**)&function);
if (FAILED(hr)) {
exit(0);
}
当我尝试 RegisterInterfaceInGlobal 时收到一个 HR 错误 invalidArg(尽管它使用与 CoMarshalInterThreadInterfaceInStream 相同的参数,除了 cookie)
你不能用 CoMarshalInterThreadInterfaceInStream 来做,它是接口指针的一种便捷方法,只会被提取一次。
改为使用 CoMarshalInterface/CoUnmarshalInterface 并使用通过 ShCreateMemStream 创建的流。
正如 SoronelHaetir 所说,CoMarshalInterThreadInterfaceInStream
无法做到这一点。它只能(取消)编组一个接口一次。
考虑使用 IGlobalInterfaceTable
。您可以在 table 中注册您的 COM 对象一次,然后在任何 thread/apartment 中多次从 table 中检索该对象,直到您从 table.
我有一个在 C++ 中注册函数和接口的方法,我正在使用 CoMarshalInterThreadInterfaceInStream
将接口指针编组到另一个线程中的方法。
在RegisterFunction
中:
HRESULT hr = CoMarshalInterThreadInterfaceInStream(IID_Function, function, &stream);
在 GetFunction
发布:
HRESULT hr = CoGetInterfaceAndReleaseStream(stream, IID_Function, reinterpret_cast<void**>(&function));
由于我会多次调用 GetFunction
而 CoGetInterfaceAndReleaseStream
只释放一次流,我如何保存流以再次使用它?
我尝试使用 IGlobalInterfaceTable
,但无法正常工作。
我在RegisterFunction
成功注册了界面:
DWORD dwCookie = 0;
int a = 0;
if (pGIT == NULL) {
HRESULT hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&pGIT);
if (hr != S_OK) {
throw _com_error(hr);
}
}
if (dwCookie == 0) {
HRESULT hr = pGIT->RegisterInterfaceInGlobal(function, IID_Function, &dwCookie);
if (hr != S_OK) {
throw _com_error(hr);
}
}
但是当我试图在 GetFunction
中检索它时:
IGlobalInterfaceTable* GIT = NULL;
if (GIT == NULL) {
hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&GIT);
if (FAILED(hr)) {
exit(0);
}
}
hr = pGIT->GetInterfaceFromGlobal(dwCookie, IID_Function, (void**)&function);
if (FAILED(hr)) {
exit(0);
}
当我尝试 RegisterInterfaceInGlobal 时收到一个 HR 错误 invalidArg(尽管它使用与 CoMarshalInterThreadInterfaceInStream 相同的参数,除了 cookie)
你不能用 CoMarshalInterThreadInterfaceInStream 来做,它是接口指针的一种便捷方法,只会被提取一次。
改为使用 CoMarshalInterface/CoUnmarshalInterface 并使用通过 ShCreateMemStream 创建的流。
正如 SoronelHaetir 所说,CoMarshalInterThreadInterfaceInStream
无法做到这一点。它只能(取消)编组一个接口一次。
考虑使用 IGlobalInterfaceTable
。您可以在 table 中注册您的 COM 对象一次,然后在任何 thread/apartment 中多次从 table 中检索该对象,直到您从 table.