Closed robigit closed 4 years ago
@robigit
I'm not sure what are you trying, but, I'm watching IUnknown interface in your src. Is there any reason why not to make it initially as COM visible component if you've plan to use this anyway? Or try this if you just need complex types etc.
I think this is related to the calling convention stdcall
In any case, our tool is configured for __cdecl by default [?]
Thank you for your answer. What I'd want to do i substitute an old c++ dll, that exports the ItfNavig I put in the example (not INavig, that was a typo cleaning the code) with a new c# managed dll. However I discovered that in c# there is an automatic signature conversion of the methods of the com interfaces. Infact they should always export an HRESULT. So if in c# you write
int CreateNavig(int p);
in c++ it should become
HRESULT CreateNavig (int *result, int p)
But this is not my case and the reason of the exception.
As I can't change the c++ interface, to solve it has been sufficient tell to c# to not change the signature, so in c# the method changes in
[PreserveSig]
int CreateNavig(int p);
And all worked fine.
I hope this can be useful to someone else
All correct. We need to use PreserveSig to avoid mentioned transformation in COM interop.
Otherwise you can also try with some offset to the specific field like ~ prm->CreateNavig(0, &result)
Moreover, in past, I already voiced about hack with offsets for IUnknown https://github.com/3F/Conari/issues/8 when raw access to data https://github.com/3F/Conari/issues/2#issuecomment-238394036
That can be considered in the manner of Conari: https://github.com/3F/Examples/blob/master/DllExport/BasicExport/UnmanagedCppConsole/UnmanagedCppConsole.cpp#L59 and so on
I hope this can be useful to someone else
Yes! Thank you for the tips in our Q&A list.
From a c++ program I want call some functions of an interface into a managed class library in c#. The call works but I got an exception when the caller exit from the function and it happens only if and when the called function has a parameter. I think this is related to the calling convention stdcall, but I can't figure out how to correctly configure the interfaces. May you give me an hint? Thank you
This is the calling c++ part
Interface
This is the call
This is how it is implemented in c #
DllExport
The Interface
and the implementation