gcc DLL from C#: "DLL initialization routine failed" on Windows 10

1

I am developing C# DLL that needs a code I have as a C++ DLL, build by gcc. For this, I have written a C wrapper and compiled it using gcc (TDM-GCC MinGW-w64) as a DLL. In other words, I have:

  • C++.dll built by gcc; plus C++_test.exe that uses the DLL, so I know it works.
  • C.dll built by gcc, calling C++.dll; plus C_test.exe that uses the DLL...
  • C#.dll built by Visual Studio, calling C.dll; plus C#_test.exe

The whole chain is built as 64-bit code.

My problem is that while this setup worked fine on my old Windows 7 box, on my new machine with Windows 10 (and also newer versions of software and libraries), C#_test.exe fails at the point it calls a function from C.dll, with the following message: Unable to load DLL 'C.dll': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

In the C# code, the function is defined as:

    [DllImport("C.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern int pg_generate(..);

I have all the other DLLs in the same folder (otherwise I would get another error message about missing DLL).

Any idea how to find out what the problem is and how to fix it? I know that it would probably help to build the whole chain in VS, but I do not have the required project files and would also build several libraries that C++.dll depends on, so I would rather avoid that - especially since it worked before...

Update: When I debug C#_test.exe in VS, it throws from pg_generate(): Exception thrown at 0x000000006E0436B0 (C++.dll) in C#_test.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFB64927F5.

Update 2: I get the same error (DLL initialization routine failed) also when I used C#_test.exe (and all dependencies) built and tested on my old box. This suggests that the problem is in Windows 10 vs. Windows 7. (It also means that once the client upgrades to Windows 10, the code we delivered will stop to work...)

c#
c++
visual-studio
gcc
dll
asked on Stack Overflow Sep 19, 2017 by Michal Kaut • edited Sep 19, 2017 by Michal Kaut

1 Answer

0

I had the same issue. It turned out that the source of it were the new/delete operators. As soon as I implemented my own operators the application works fine. With third party libs this isn't so easy however!

Here's the minimal example reproducing the error including a workaround (if AddNewOperator is defined operator new[] will be defined and the resulting .dll will work fine):

Test.cs (compiled/run with Visual Studio 2017):

using System;
using System.Runtime.InteropServices;
class Program
{
    [DllImport("libTest", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
    public static extern int TestFunction();
    static void Main(string[] args)
    {
        Console.WriteLine("!!" + TestFunction());
    }
}

Test.cpp compiled with mingw:

#include <new>
#include <cstdlib>

#ifdef AddNewOperator // This will fix the issue
void* operator new[](std::size_t sz){
    return std::malloc(sz);
}
#end

extern "C" {
int __stdcall __declspec(dllexport) TestFunction() {
        int* test = new int[3]; // removing this line will make everything work when building
        return test[2];
}

And here's the build script:

# Remove the following # and the compiled dll will work just fine
g++ -g -s -Wall -c -fmessage-length=0 Test.cpp  #-DAddNewOperator
g++ -g -shared -o libTest.dll *.o -Wl,--subsystem,windows
answered on Stack Overflow Apr 19, 2018 by kunzej • edited Apr 23, 2018 by kunzej

User contributions licensed under CC BY-SA 3.0