0

I write a C# dll to use in C++. But it has some error: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

In my C# Dll project, i use another library call DllExport: DllExport. Here is my C++ code:

#include<stdio.h>
#include<iostream>
#include<Windows.h>
#include<string>

using namespace std;

int main() {
    const char* exportDll = "C:\\Users\\source\\repos\\MacroLib\\MacroLib\\bin\\x64\\Release\\net6.0\\MacroLib.dll";
    HMODULE hexportDll = LoadLibraryA(exportDll);

    typedef int(__stdcall *AntiMacro)(string path);
    typedef void(__cdecl* ScanMacro)(string path, bool isFolder);
    typedef int(__cdecl* add)(int a, int b);

    auto padd = (add)GetProcAddress(hexportDll, "_add");
    int m = padd((int)5, (int)6);
    cout << m << endl; 
    return 0;
}

Note that, when i put a breakpoint on this line: auto padd = (add)GetProcAddress(hexportDll, "_add"); I can see the exact address that I observe in CFF Explorer. Can anyone suggest some ways to fix it?

TomC
  • 33
  • 6
  • That's a C# error, not a C++ error. It's telling you that it cannot load a dependency of your MacroLib.dll, probably because it's not in the path. – ChrisMM Jun 12 '23 at 11:04
  • 2
    You're probably better of using C++/cli and make an explicity wrapper then trying to do all the binding manually. – Pepijn Kramer Jun 12 '23 at 11:25
  • .Net code is *managed*, i.e. it needs a runtime to load, compile and execute code. So you cannot call managed code from unmanaged code directly, since there is just a bunch of CIL code that the processor cannot make any sense of. Changing your project to c++/cli is probably the simplest solution, since that will load the .net runtime for you. – JonasH Jun 12 '23 at 12:38

0 Answers0