简体   繁体   中英

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

I haven't found anything useful related to this problem after some serious google lurking, so I'll ask it here.

I have a program that's made in C# that injects a DLL into another process, fairly trivial. It Calls CreateRemoteThread and LoadLibrary from kernel32.dll with [DllImport].

My DLL once loaded then waits for authentication from the C# program, due to security reasons, I can't have this data transferred using sockets. So I have my DLL export a function that's planned to be called from the C# program with authentication data.

The exported function takes two arguments, it is as follows :

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

    // Do stuff

}

Since as the DLL isn't in the same address space as the C# program, I can't use [DllImport] to get and the call the exported function.

My second idea was to use CreateRemoteThread to call the function, though that can only be passed 1 argument whereas I need two, it'd also be difficult because I'd need to call the return of GetProcAddress, I can't simply call by exported function directly.

So, how would I go about achieving this?

Thanks

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


If you don't trust the DLL at all, then it's better to split de C# program in two. One program connected via Remoting/WCF/IPC to the second one, and the second one connects to the C DLL via DllImport. This solution is typically used when you don't trust C DLL stability or memory allocation because the second program (the one that call de C DLL) can be restarted without restarting the main program. Example of this pattern: some windows drivers (with the exception that they don't use C# at this time).

您可以使用非持久内存映射文件在应用程序和DLL之间交换数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM