1

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

foo(System::Object ^% theBuffer)

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

goo(VARIANT* theBuffer);

所以我试过了;

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

失败了。和

Bitmap ^b;
foo(b);

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

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

谢谢

4

3 回答 3

1

您不能VARIANT直接将 a 转换为缓冲区。

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

于 2009-02-04T17:21:03.067 回答
0

怎么样..

Bitmap ^b = gcnew Bitmap(...
foo(b);
于 2009-02-04T17:50:05.410 回答
0

好的,所以我实际上是在使用 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);

但显然它不起作用。

于 2009-02-04T19:02:13.317 回答