Still working on a problem that started from here
Calling C++ dll function from C#: Of structs, strings and wchar_t arrays., but with a different approach.
Following the example Calling Managed Code from Unmanaged Code and vice-versa I wrote a managed wrapper in C++ to access the unmanages class in the unmanaged C++ dll.
It looks like this:
//in header file
public __gc class TSSLDllWrapper
{
public:
TSSLDllWrapper();
//this is the unmanaged class
CcnOCRsdk * _sdk;
bool convertHKID_Name(char *code, RECO_DATA *o_data);
};
//in .cpp file
TSSLDllWrapper::TSSLDllWrapper(void)
{
_sdk = new CcnOCRsdk();
}
bool TSSLDllWrapper::convertHKID_Name(char *code, RECO_DATA *o_data)
{
return _sdk->convertHKID_Name(code, o_data);
}
//C++ RECO_DATA structure definition:
struct RECO_DATA{
wchar_t FirstName[200];
wchar_t Surname[200];
};
Now I have a dll that I can import into my C# project.
Here is the problem however:
When I want to call the method from the dll file, like this:
TSSLDllWrapper wrapper = new TSSLDllWrapper();
bool res = wrapper.convertHKID_NameSimple( //need to pass parameters here );
It expects the C++ parameters – pointers to char and RECO_DATA.
How can I fix this and pass C++ types from C# code?
Best Solution
One way to convert most C data types is to use the PInvoke Interop Assitant. It will create proper C# / VB.Net types for most C structures. Here is the output for RECO_DATA
For the char* parameter, you can pass IntPtr.Zero or use Marshal.StringToCoTaskMemAnsi to get the job done.