C++托管代码使用句柄调用非托管代码
经过一下午的搜索,这个问题把我难住了!我正在编写一个托管 C++ 应用程序,它需要调用一些非托管代码。这是我需要调用的函数:
public: void Connect(
[out] LONG* pCookieID,
[out] LONG* pNumberOfStreams,
[out] VARIANT* pMediaType
);
文档中有这样的说明:参数 pMediaType
必须在 C#
中设置为 null,而在 VB .NET
中则不设置任何内容code> 在调用 Connect 之前,否则将抛出错误代码为 DISP_E_TYPEMISMATCH = 0x80020005
的异常。
所以我需要将 3 个句柄传递给该函数,以便它返回其中的数据。两个 LONG 句柄看起来相当简单,但我不知道如何传递变体的句柄。
这就是我必须要做的:
int ^cookieID;
int ^numberOfStreams;
System::Object ^buffer;
GCHandle hcookieID = GCHandle::Alloc(cookieID, GCHandleType::Pinned);
GCHandle hnumberOfStreams = GCHandle::Alloc(numberOfStreams,
GCHandleType::Pinned);
GCHandle hbuffer = GCHandle::Alloc(buffer, GCHandleType::Pinned);
parser->Connect(hcookieID.AddrOfPinnedObject().ToInt32(),
hnumberOfStreams.AddrOfPinnedObject().ToInt32(),
(System::Object^)hbuffer.AddrOfPinnedObject().ToInt32());
hcookieID.Free();
hnumberOfStreams.Free();
hbuffer.Free();
编译器接受这一点,但是当调用 Connect
方法时,我收到以下错误:
“类型不匹配。(HRESULT 异常:0x80020005
(DISP_E_TYPEMISMATCH))"} System::Exception^
我到处搜索并尝试了各种解决方案,但找不到任何解决方案。
After an afternoon of searching this problem has got me stumped! I'm writing a managed C++ application which needs to call some unmanaged code. This is the function I need to call:
public: void Connect(
[out] LONG* pCookieID,
[out] LONG* pNumberOfStreams,
[out] VARIANT* pMediaType
);
And the documentation has this note: Parameter pMediaType
must be set to null in C#
and nothing in VB .NET
before calling Connect, otherwise an exception with error code DISP_E_TYPEMISMATCH = 0x80020005
will be thrown.
So I need to pass 3 handles to the function for it to return data in them. The two LONG
handles seem reasonably straightforward but I can't figure out how to pass a handle for a variant.
This is where I've got to:
int ^cookieID;
int ^numberOfStreams;
System::Object ^buffer;
GCHandle hcookieID = GCHandle::Alloc(cookieID, GCHandleType::Pinned);
GCHandle hnumberOfStreams = GCHandle::Alloc(numberOfStreams,
GCHandleType::Pinned);
GCHandle hbuffer = GCHandle::Alloc(buffer, GCHandleType::Pinned);
parser->Connect(hcookieID.AddrOfPinnedObject().ToInt32(),
hnumberOfStreams.AddrOfPinnedObject().ToInt32(),
(System::Object^)hbuffer.AddrOfPinnedObject().ToInt32());
hcookieID.Free();
hnumberOfStreams.Free();
hbuffer.Free();
The compiler accepts this but when the Connect
method is called I get the following error:
"Type mismatch. (Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))"} System::Exception^
I've searched high and low and tried all sorts of solutions but I can't find any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论