3F / DllExport

.NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat)
MIT License
960 stars 133 forks source link

Trying to call functions crashes app #232

Closed agaertner closed 5 months ago

agaertner commented 5 months ago

I am trying to call exported functions from my C++ program. However, it crashes/freezes on call.

I have setup a few test methods to check if the dll works at all and it already crashes on the very first test which is one without a signature.

code snippet .NET Framwork 4.7.2

[DllExport("TestFunc1", CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static string TestFunc1() {
    return string.Empty;
}

In C++:

// .h
    extern "C" {
        typedef char* (__cdecl *exTestFunc1)();
    }
    class DllMngr
    {
        public:
            char* testFunc1();
        private:
            HINSTANCE   m_dllHandle;
            exTestFunc1 m_exTestFunc1;
    }

// .cpp
    m_exTestFunc1 =reinterpret_cast<exTestFunc1>(fetchMethodHandle("TestFunc1", l_result));

    char* DllMngr::testFunc1() {
        if (!m_dllHandle) {
            return "Dll not loaded.";
        }
        return m_exTestFunc1();
    }

// call
   char* l_test1 = m_DllMngr->testFunc1();

I believe there might be some incompatibilities or the export settings are wrong. Do note that my dll handles are valid and my method handles are valid as well. Everything gets loaded correctly before trying to call it.

Any help would be appreciated.

The project I load the dll in is x86

. . .

The question is related to:

agaertner commented 5 months ago

I got it to work in a little test C++ project created with Visual Studio 2012. The project where it doesn't work was created with Embarcadero XE2.