We created a .NET 8 library DllExportTest.dll in C#. This library exports two functions (AddOne and AddOneWithHelper) to be called from native code.
The export is done with the DLLExport 1.8 package.
There is also a console application written in C++ that calls these functions.
The AddOne function is fully implemented in DllExportTest.dll, and the AddOneWithHelper function calls a method from the other .NET 8 library HelperLib.dll.
Both libraries (DllExportTest.dll and HelperLib.dll) are located in the same folder as the console application.
The AddOne function works fine when called from the console application.
However, when trying to call the AddOneWithHelper function, this exception occurs:
Unhandled exception at 0x769BB5E2 (KernelBase.dll) in ConsoleApp.exe: 0xE0434352 (parameters: 0x80070002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x61500000)
To make sure that the DllExportTest.dll + HelperLib.dll bundle works, we added to DllExportTest.dll a non-exportable function AddOneWithHelperNoExport, which also calls a method from the HelperLib.dll library. If we call the AddOneWithHelperNoExport function from the Winforms application, it works.
So the problem is that we cannot call another library method from the exported function.
How can I call methods of other .NET 8 libraries from functions exported for native applications?
DllExportTest.dll code:
using System.Runtime.InteropServices; using HelperLib; namespace DllExportTest { public class Class1 { // works when called from console app [DllExport(CallingConvention = CallingConvention.Cdecl)] public static int AddOne(int n) { var result = n + 1; MessageBox.Show("AddOne called.\nResult = " + result.ToString()); return result; } // not works when called from console app [DllExport(CallingConvention = CallingConvention.Cdecl)] public static int AddOneWithHelper(int n) { var result = HelperClass.AddOne(n); MessageBox.Show("AddOneWithHelper called.\nResult = " + result.ToString()); return result; } // works when called from WinForms app public static int AddOneWithHelperNoExport(int n) { var result = HelperClass.AddOne(n); MessageBox.Show("AddOneWithHelperNoExport called.\nResult = " + result.ToString()); return result; } } } DllExportTest.dll export settings:
<DllExportDDNSCecil>true</DllExportDDNSCecil> <PlatformTarget>x86</PlatformTarget> <DllExportOrdinalsBase>1</DllExportOrdinalsBase> <DllExportGenExpLib>false</DllExportGenExpLib> <DllExportOurILAsm>false</DllExportOurILAsm> <DllExportSysObjRebase>false</DllExportSysObjRebase> <DllExportLeaveIntermediateFiles>false</DllExportLeaveIntermediateFiles> <DllExportTimeout>30000</DllExportTimeout> <DllExportPeCheck>6</DllExportPeCheck> <DllExportPatches>0</DllExportPatches> <DllExportRefreshObj>false</DllExportRefreshObj> <DllExportILAsmExternAsm /> <DllExportILAsmTypeRef /> <DllExportTypeRefOptions>0</DllExportTypeRefOptions> <DllExportRefPackages /> <DllExportPreProcType>9</DllExportPreProcType> <DllExportILMerge /> <DllExportPostProcType>0</DllExportPostProcType> HelperLib.dll code:
namespace HelperLib { public class HelperClass { public static int AddOne(int n) { return n + 1; } } } Console app code:
#include <iostream> #include <wtypes.h> typedef int(__cdecl* f_funci)(int); int main() { HINSTANCE hGetProcIDDLL = LoadLibrary(L"DllExportTest.dll"); if (!hGetProcIDDLL) { MessageBox(0, L"could not load the dynamic library", L"error", MB_OK); return 1; } f_funci add_one = (f_funci)GetProcAddress(hGetProcIDDLL, "AddOne"); f_funci add_one_with_helper = (f_funci)GetProcAddress(hGetProcIDDLL, "AddOneWithHelper"); int n = add_one(1); std::cout << "n = " << n << "\n"; n = add_one_with_helper(10); std::cout << "n = " << n << "\n"; return 0; }