Passing pointer from managed C++/CLI to ActiveX C++ component
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开发者_Python百科\n", ptr);
OutputDebugString(buffer);
}
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.
精彩评论