L
L
lightalex2017-12-11 23:59:09
C++ / C#
lightalex, 2017-12-11 23:59:09

What is the correct way to call a function from a loaded C# DLL in C++?

I'm trying to load a DLL written in C# in C++
Main program:

HINSTANCE hMyDLL = LoadLibrary(L"ClassLibrary1.dll");
if (NULL == hMyDLL) {
  std::cout << "LoadLibrary failed\n";
  getchar();
  return -1;
}

typedef int (WINAPI * function) ();
function f = (function)GetProcAddress(hMyDLL, "Class1.test");
if (NULL == f) {
  std::cout << ":(\n";
}

DLL file code:
namespace ClassLibrary1 {
    public class Class1 {
        public static int test()  {
            return 5;
        }
    }
}

What is the correct way to call a class method?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2017-12-12
@alexr64

https://github.com/3F/DllExport
"Out of the box" it seems like nothing, except for COM

A
Alexander Kuznetsov, 2017-12-12
@DarkRaven

Microsoft has an article on MSDN about this, https://support.microsoft.com/en-us/help/828736/ho... .
In short, they offer to work through COM.
Even earlier I came across something similar - https://www.codeproject.com/Tips/695387/Calling-Cs... .
The bottom line is that you can write managed code in C ++, and there, you can add fully exported functions, according to the author. I advise you to pay attention.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question