How to call exported C ++ function from C # in another process?

I did not find anything useful related to this problem after seriously hiding google, so I will ask for it here.

I have a program created in C # that inserts a DLL into another process, quite trivial. It calls CreateRemoteThread and LoadLibrary from kernel32.dll using [DllImport].

My DLL will load once and then wait for authentication from a C # program, due to security reasons I cannot transfer this data using sockets. Therefore, I have a DLL export that I plan to call from a C # program with authentication data.

The exported function takes two arguments:

extern "C" __declspec(dllexport) void DoStuff( const char* ccString1, const char* ccString2 ){ // Do stuff } 

Since the DLL is not in the same address space as the C # program, I cannot use [DllImport] to get and call the exported function.

My second idea was to use CreateRemoteThread to call the function, although this can only be passed to one argument, while I need two, it will also be difficult, because I will need to call GetProcAddress, I canโ€™t just call the exported function directly.

So how could I achieve this?

thanks

+4
source share
2 answers

[DllImport] is the same as GetProcAddress. C # does not load the DLL until the first call, so .NET can perform security checks before calling the DLL.


If you do not trust the DLL at all, it is better to split the C # program into two parts. One program is connected via Remoting / WCF / IPC to the second, and the second is connected to the C DLL via DllImport. This solution is usually used when you do not trust the stability of the DLL or the allocation of memory, because the second program (the one that calls the DLL DLL) can be restarted without restarting the main program. An example of this pattern: some Windows drivers (except that they do not use C # at this time).

0
source

You can use non-persistent memory mapped files to exchange data between the application and the DLL.

0
source

Source: https://habr.com/ru/post/1483960/


All Articles