A couple of points I stumbled on while creating a dll wrapper:
- I had to call a member function of the unmanaged class. Took some time to find out that I can not do this by using DllImport directly, but have to write a 'wrapper' myself.
- In the wrapper itself, it is also not enough to wrap just the member function. I have to be able to create a pointer to the C++ class, so I have to export the pointer to the constructor (at least, that's how I understand it, maybe that's not exactly correct.). I tried just exporting the member function initially, it compiled but returned "AccessViolationException" at runtime. Got stuck there for a while too.
Therefore, my wrapper looks like this (as suggested in using a class defined in a c++ dll in c# code):
public class __declspec(dllexport) Wrapper
{
public:
CcnOCRsdk* SDKCreate()
{
return new CcnOCRsdk();
}
bool CcnOCRsdk_HKID(CcnOCRsdk* pSDK, char *code, RECO_DATA *o_data)
{
return pSDK->convertHKID_Name(code, o_data);
}
void SDKDelete(CcnOCRsdk* pSDK)
{
delete pSDK;
}
};
__declspec(dllexport) on the class level exports all public members of the class.
SDKCreate() returns a pointer to that CcnOCRsdk class from the unmanaged dll which member function I have to call.
CcnOCRsdk_HKID calls that member function. Note that the pointer to the CcnOCRsdk is passed.
After the code builds into the dll, I have to use the DUMPBIN to find out what are the 'mangled' entry points for the wrapper dll I wrote.
For my wrapper, the results look like this
1 0 00001240 ??4Wrapper@TSSL@@QAEAAV01@ABV01@@Z = __t2m@??4Wrapper@TSSL@@QAEAAV01@ABV01@@Z ([T2M] public: class TSSL::Wrapper & __thiscall TSSL::Wrapper::operator=(class TSSL::Wrapper const &))
2 1 00001220 ?CcnOCRsdk_HKID@Wrapper@TSSL@@QAE_NPAVCcnOCRsdk@@PADPAURECO_DATA@@@Z = __t2m@?CcnOCRsdk_HKID@Wrapper@TSSL@@QAE_NPAVCcnOCRsdk@@PADPAURECO_DATA@@@Z ([T2M] public: bool __thiscall TSSL::Wrapper::CcnOCRsdk_HKID(class CcnOCRsdk *,char *,struct RECO_DATA *))
3 2 00001200 ?SDKCreate@Wrapper@TSSL@@QAEPAVCcnOCRsdk@@XZ = ?SDKCreate@Wrapper@TSSL@@QAEPAVCcnOCRsdk@@XZ (public: class CcnOCRsdk * __thiscall TSSL::Wrapper::SDKCreate(void))
4 3 00001410 ?SDKDelete@Wrapper@TSSL@@QAEXPAVCcnOCRsdk@@@Z = ?SDKDelete@Wrapper@TSSL@@QAEXPAVCcnOCRsdk@@@Z (public: void __thiscall TSSL::Wrapper::SDKDelete(class CcnOCRsdk *))
Now I'm finally ready to use my wrapper in the C#. When I did this without specifying the entry point exactly as in my output from dumpbin, I got 'unable to find entry point' error.
[DllImport(@"TSSLWrapper.dll", EntryPoint = "?SDKCreate@Wrapper@TSSL@@QAEPAVCcnOCRsdk@@XZ")]
public static extern IntPtr SDKCreate();
[DllImport(@"TSSLWrapper.dll", EntryPoint = "?CcnOCRsdk_HKID@Wrapper@TSSL@@QAE_NPAVCcnOCRsdk@@PADPAURECO_DATA@@@Z")]
public static extern bool CcnOCRsdk_HKID(IntPtr ptr, string num, out RECO_DATA o_data);
The RECO_DATA is defined as JaredPar suggested.
And the last step is to enjoy the results.
First I have to call the class constructor, and then pass the pointer to the actual call to the function
RECO_DATA recoData = new cnOCRsdk.RECO_DATA();
string num = "262125355174";
IntPtr ptr = cnOCRsdk.SDKCreate();
bool res = cnOCRsdk.CcnOCRsdk_HKID(ptr, num, out recoData);
My res returns true, and I get the results I expected in recoData.