C++托管代码使用句柄调用非托管代码

发布于 2024-10-07 15:03:15 字数 1285 浏览 6 评论 0原文

经过一下午的搜索,这个问题把我难住了!我正在编写一个托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文