1

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?

username
  • 3,018
  • 5
  • 38
  • 69
  • Did you study [this](http://stackoverflow.com/questions/2804893/c-dll-export-decorated-mangled-names) question? You can find something useful in the answers there. – Lol4t0 Jun 07 '13 at 17:49
  • Take a look at this question: [C# GetProcAddress Returns Zero](http://stackoverflow.com/questions/3754264/c-sharp-getprocaddress-returns-zero/3754403#comment23243538_3754403) – Thomas C. G. de Vilhena Jun 07 '13 at 18:38

0 Answers0