Interprocess communication between C # and C ++

I am writing a bot for a game that has a C ++ API (i.e. methods in the Cpp dll are called in the game when events occur, the DLL can redirect methods in the game to trigger actions).

I do not want to write my bot in C ++, I am quite an experienced C # programmer, but I have no experience in C ++ at all. So the obvious solution is to use ipc to send the event to the C # program and send the actions back to C ++, so all I need to write in C ++ is the basic structure for calling methods and sending events.

What would be the best way to do this? The sample code would be very grateful, as I have no particular desire to learn C ++ at this stage!

+7
c ++ c # ipc
source share
3 answers

One solution is to create a managed C ++ class library with regular __declspec(dllexport) functions that invoke managed methods in the C # class reference library.

An example is a C ++ code file in a managed C ++ project:

 #include "stdafx.h" __declspec(dllexport) int Foo(int bar) { csharpmodule::CSharpModule mod; return mod.Foo(bar); } 

C # module (a separate project in the solution):

 namespace csharpmodule { public class CSharpModule { public int Foo(int bar) { MessageBox.Show("Foo(" + bar + ")"); return bar; } } } 

Note that I demonstrate that this is a real .NET call with a call to System.Windows.Forms.MessageBox.Show .

An example of a basic (non-CLR) Win32 console application:

 __declspec(dllimport) int Foo(int bar); int _tmain(int argc, _TCHAR* argv[]) { std::cout << Foo(5) << std::endl; return 0; } 

Do not forget to associate the Win32 console application with the .lib file obtained from the assembly of the C ++ managed project.

+6
source share

There are many different ways to make IPC on Windows . For C # in C ++, I will be tempted to use Sockets as an API under both C ++ (WinSock is fine when you go around), and C # is pretty easy.

Named pipes might be better if you do not want to use sockets and were designed specifically for IPC. The C ++ API seems pretty simple, like here .

+6
source share

In that case, I would like to see a C ++ / CLI and C # group using the .NET Framework named pipe.

+3
source share

All Articles