++ C#和C之间的进程间通信 [英] Interprocess communication between C# and C++
问题描述
我正在写一个僵尸的游戏,其中有一个C ++ API接口(即在一段cpp的方法DLL被调用由事件发生时,该dll可以调用后面的比赛方法来触发动作游戏)。
I'm writing a bot for a game, which has a C++ API interface (ie. methods in a Cpp dll get called by the game when events occur, the dll can call back methods in the game to trigger actions).
我真的不希望我写在C ++中的机器人,我是一个非常有经验的C#程序员,但我没有C ++的经验都没有。因此,显而易见的解决方案是使用IPC发送事件给一个C#程序,并发送行动回到C ++的,这样我需要用C写++是调用方法和发送事件的基本框架。
I don't really want to write my bot in C++, I'm a fairly experienced C# programmer but I have no C++ experience at all. So, the obvious solution is to use ipc to send event to a C# program, and send actions back to the C++ one, that way all I need to write in C++ is a basic framework for calling methods and sending events.
什么是做到这一点的最好方法是什么?样品code会大大AP preciated,因为我没有必须学习C ++在这一点上特别的愿望!
What would be the best way to do this? Sample code would be greatly appreciated as I no have particular desire to learn C++ at this point!
推荐答案
一种解决方案是创建一个托管C ++类库与普通的 __ declspec(dllexport)的
其中要求管理功能方法引用C#类库。
One solution is to create a managed C++ class library with regular __declspec(dllexport)
functions which call managed methods in a referenced C# class library.
示例 - C ++ code。在托管C ++项目文件:
Example - C++ code file in managed C++ project:
#include "stdafx.h"
__declspec(dllexport) int Foo(int bar)
{
csharpmodule::CSharpModule mod;
return mod.Foo(bar);
}
C#模块(在溶液中单独的项目):
C# Module (separate project in solution):
namespace csharpmodule
{
public class CSharpModule
{
public int Foo(int bar)
{
MessageBox.Show("Foo(" + bar + ")");
return bar;
}
}
}
请注意,我证明,这是通过使用 System.Windows.Forms.MessageBox.Show
调用实际.NET调用。
Note that I am demonstrating that this is an actual .NET call by using a System.Windows.Forms.MessageBox.Show
call.
样品基本(非CLR)Win32控制台应用程序:
Sample 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;
}
记住链接与的.lib
从托管C ++项目的生成生成的文件中Win32控制台应用程序。
Remember to link the Win32 console application with the .lib
file resulting from the build of the managed C++ project.
这篇关于++ C#和C之间的进程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!