将指针从托管C ++ / CLI传递到ActiveX C ++组件 [英] Passing pointer from managed C++/CLI to ActiveX C++ component

查看:237
本文介绍了将指针从托管C ++ / CLI传递到ActiveX C ++组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++中内置的ActiveX组件。它的一个方法有这个签名:

I have an ActiveX component built in C++. One of its methods has this signature:

short Component::Method(short FAR* ptr) {}



当我将ActiveX添加到我的C ++ / CLI应用程序
时,方法签名显示为:

When the I add the ActiveX into my C++/CLI application the method signature shows as:

short Compnenet::Method(short% ptr) {}

我想能够正确传递short * pSomething;变量值到这个方法。
当然,新的签名不接受传递参数为短*
,即使你尝试转换为短%它不会给正确的结果。

I want to be able to correctly pass short* pSomething; variable value to this method. of course, the new signature doesn't accept passing arguments as short* and even if you try to cast to short% it doesn't give right results.

注意:我没有访问activeX控件来改变。我只能确认activeX方法接收的地址的值。该方法打印传递的值如下:

Note: I don't have access to the activeX control to change. I can only confirm the value of address that that the activeX method received. The method prints the passed value as follows:

short Component::Method(short FAR* ptr) {
    char buffer[128];
    sprintf_s(buffer, "address of ptr = %p\n", ptr);
    OutputDebugString(buffer);
}


推荐答案

函式签名无效对于ActiveX自动化,数组必须作为SAFEARRAY传递。因此,该函数不能由除本地C / C ++之外的任何代码调用。类型库转换器具有相同的问题,函数签名与参数通过引用传递的函数签名相同。它没有办法猜测它实际上是一个数组。这是为什么你得到了短的%类型。

The function signature is not valid for ActiveX automation, arrays must be passed as a SAFEARRAY. As is, the function cannot be called by any code other than native C/C++. The type library converter has the same problem, the function signature is identical to one where the argument is passed by reference. It has no way to guess that it is actually an array. Which is why you got the short% type.

如果你不能更改本机组件,那么你将必须编辑由Tlbimp.exe生成的互操作库。这需要运行ildasm.exe来反编译DLL到IL。编辑函数的IL声明。将humpty-dumpty和ilasm.exe一起放回。看看一个小测试函数的反汇编,它有你需要知道如何编辑IL的签名。你需要传递参数作为IntPtr并传递pinned数组。使用pin_ptr<>获取该指针。

If you can't change the native component then you will have to edit the interop library that's generated by Tlbimp.exe. That requires running ildasm.exe to decompile the DLL to IL. Edit the IL declaration of the function. Put humpty-dumpty back together with ilasm.exe. Look at the disassembly of a little test function that has the signature you need to know how to edit the IL. You'll need to pass the argument as an IntPtr and pass the pinned array. Use pin_ptr<> to get that pointer.

这篇关于将指针从托管C ++ / CLI传递到ActiveX C ++组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆