将指针从托管 C++/CLI 传递到 ActiveX C++成分
我有一个用 C++ 构建的 ActiveX 组件。它的方法之一具有以下签名:
short Component::Method(short FAR* ptr) {}
当我将 ActiveX 添加到我的 C++/CLI 应用程序中时 方法签名显示为:
short Compnenet::Method(short% ptr) {}
我希望能够正确传递短* pSomething;此方法的变量值。 当然,新签名不接受传递短参数* 即使您尝试转换为short%,它也不会给出正确的结果。
注意:我无权访问要更改的 activeX 控件。我只能确认 activeX 方法收到的地址值。该方法打印传递的值,如下所示:
short Component::Method(short FAR* ptr) {
char buffer[128];
sprintf_s(buffer, "address of ptr = %p\n", ptr);
OutputDebugString(buffer);
}
I have an ActiveX component built in C++. One of its methods has this signature:
short Component::Method(short FAR* ptr) {}
When the I add the ActiveX into my C++/CLI application
the method signature shows as:
short Compnenet::Method(short% ptr) {}
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.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该函数签名对于 ActiveX 自动化无效,数组必须作为 SAFEARRAY 传递。事实上,该函数不能由本机 C/C++ 以外的任何代码调用。类型库转换器也有同样的问题,函数签名与通过引用传递参数的函数签名相同。它无法猜测它实际上是一个数组。这就是为什么你得到了short%类型。
如果无法更改本机组件,则必须编辑 Tlbimp.exe 生成的互操作库。这需要运行 ildasm.exe 将 DLL 反编译为 IL。编辑函数的 IL 声明。将 humpty-dumpty 与 ilasm.exe 放回一起。查看一个小测试函数的反汇编,该函数具有您需要了解如何编辑 IL 的签名。您需要将参数作为 IntPtr 传递并传递固定数组。使用 pin_ptr<>得到那个指针。
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.
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.