Passing pointer from managed C++/CLI to ActiveX C++ component

岁酱吖の 提交于 2019-12-08 04:31:56

问题


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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/6144091/passing-pointer-from-managed-c-cli-to-activex-c-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!