C++ CLI 神秘函数调用

发布于 2024-07-12 10:18:54 字数 462 浏览 9 评论 0原文

所以我试图调用一个函数,它是 OCX 对象的托管包装器。 有很大的困难。 功能是;

foo(System::Object ^% theBuffer)

其中“theBuffer”是字节数组。 'foo' 包装的非托管 OCX 的模板是

goo(VARIANT* theBuffer);

所以我尝试过;

System::Int32 buf[10];
foo(buf);

这失败了。 它

Bitmap ^b;
foo(b);

可以编译,但显然被调用的函数不会为我创建 .NET 位图。

所以我想问题是如何向这个函数传递一个它可以写入的内存块,然后在 .NET 世界中访问它。

谢谢

So I'm trying to call a function that is a manged wrapper around an OCX object. Having great difficulty. Function is;

foo(System::Object ^% theBuffer)

where 'theBuffer' is an array of bytes. The template for the unmanaged OCX which 'foo' wraps is

goo(VARIANT* theBuffer);

So I've tried;

System::Int32 buf[10];
foo(buf);

which fails. And

Bitmap ^b;
foo(b);

which compiles but obviously the called function is not going to create a .NET bitmap for me.

So I guess the question is how do I pass this function a block of memory it can write to and then get access to it back in .NET world.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失眠症患者 2024-07-19 10:18:54

您无法将 VARIANT 直接转换为缓冲区。

首先,您需要通过检查theBuffer->vt来检查其中存储的对象类型。 返回值的类型为 VARTYPE

You can't convert a VARIANT to the buffer directly.

First you need to check what kind of object is stored in it by checking theBuffer->vt. The returned value will be of the type VARTYPE.

守护在此方 2024-07-19 10:18:54

像这样的东西呢..

Bitmap ^b = gcnew Bitmap(...
foo(b);

What about something like..

Bitmap ^b = gcnew Bitmap(...
foo(b);
帥小哥 2024-07-19 10:18:54

好的,我实际上正在使用 Axis Media Control SDK 与网络摄像机连接( http://www.axis.com/techsup/cam_servers/dev/activex.htm)。 我通过包装器调用的 OCX 函数如下所示:

HRESULT GetCurrentImage(int theFormat,    [C++]
        VARIANT* theBuffer,
        LONG* theBufferSize
       );

包装器由 Axis 提供。 使用.NET Reflector我已经反汇编了包装函数;

public: virtual void __gc* GetCurrentImage(Int32 __gc* theFormat, [Out] Object __gc*   *theBuffer, [Out] Int32 __gc* *theBufferSize)
{
    if (this->ocx == 0)
    {
        throw __gc new InvalidActiveXStateException(S"GetCurrentImage", ActiveXInvokeKind::MethodInvoke);
    }
    this->ocx->GetCurrentImage(theFormat, out theBuffer, out theBufferSize);
}

所以它没有做任何聪明的事情,只是传递了一块内存。 在 C++ CLI 语法中,模板如下所示:

GetCurrentImage(int theFormat, System::Object ^% theBuffer, int % theBufferSize)

所以我的问题是如何传递这块内存来写入,然后将其恢复回 .NET 对象。 我尝试过这个;

unsigned char c[100];
GetCurrentImage(0, (System::Object)c, BufSize);

但显然它不起作用。

ok so I'm actually using the Axis Media Control SDK to interface with a network camera ( http://www.axis.com/techsup/cam_servers/dev/activex.htm ). The OCX function I am calling via the wrapper looks like;

HRESULT GetCurrentImage(int theFormat,    [C++]
        VARIANT* theBuffer,
        LONG* theBufferSize
       );

the wrapper is supplied by Axis. Using .NET Reflector I have disasembled the wrapper function;

public: virtual void __gc* GetCurrentImage(Int32 __gc* theFormat, [Out] Object __gc*   *theBuffer, [Out] Int32 __gc* *theBufferSize)
{
    if (this->ocx == 0)
    {
        throw __gc new InvalidActiveXStateException(S"GetCurrentImage", ActiveXInvokeKind::MethodInvoke);
    }
    this->ocx->GetCurrentImage(theFormat, out theBuffer, out theBufferSize);
}

So it's not doing anything smart, just passing a chunk of memory. In C++ CLI syntax the template looks like;

GetCurrentImage(int theFormat, System::Object ^% theBuffer, int % theBufferSize)

So my question becomes how do I pass this a chunk of memory to write to and then recover it back into a .NET object. I tried this;

unsigned char c[100];
GetCurrentImage(0, (System::Object)c, BufSize);

but obviously it dosn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文