I have a function in my C++ project that I want to export:
extern "C" __declspec(dllexport) bool __stdcall DoExport(wchar_t* dataSource)
If I import it using DllImport attribute in C#, it works fine:
[DllImport(@"mydll", CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Unicode)]
private static extern bool DoExport(string dataSource);
Now I need to load my dll explicitly, because I'm getting the path to the dll at runtime and unfortunately I can't use relative paths too. So I call it like:
var dllPath = ResolveDllPath();
var libraryHandle = LoadLibraryEx(dllPath, IntPtr.Zero,
LOAD_WITH_ALTERED_SEARCH_PATH);
if (libraryHandle != IntPtr.Zero)
{
var procAddress = GetProcAddress(libraryHandle, "DoExport");
if (procAddress != IntPtr.Zero) // here I'm getting 0 address.
{
...
GetProcAddress returns me 0. I collected dll exports with dumpbin.exe and found my function with name: _DoExport@20
So it looks like I need to specify a __stdcall convention to get my function, because I'm getting correct address if I pass this decorated name as an argument.
How do I do this in C#? Is there a better way to coordinate names?