C# 和 C++ 之间的进程间通信
我正在为游戏编写一个机器人,它具有 C++ API 接口(即,当事件发生时,游戏会调用 Cpp dll 中的方法,该 dll 可以回调游戏中的方法来触发操作)。
我真的不想用 C++ 编写我的机器人,我是一个相当有经验的 C# 程序员,但我根本没有 C++ 经验。因此,显而易见的解决方案是使用 ipc 将事件发送到 C# 程序,并将操作发送回 C++ 程序,这样我需要用 C++ 编写一个用于调用方法和发送事件的基本框架。
最好的方法是什么?示例代码将不胜感激,因为我此时没有特别想要学习 C++ 的愿望!
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).
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Windows 中执行 IPC 的不同方法。对于 C# 到 C++,我很想使用 Sockets 作为 C++ 下的 API(一旦你了解了 WinSock 就可以了),而 C# 则非常简单。
命名管道可能会更好,如果你不这样做的话不想使用套接字,并且是专门为IPC设计的。 C++下的API看起来很简单,例如这里 。
There are lots of different ways of doing IPC in Windows. For C# to C++, I'd be tempted to use Sockets as the API under both C++ (WinSock is OK once you get your head around it) and C# is pretty easy.
Named Pipes might be better though if you don't want to use sockets, and were designed specifically for IPC. The API under C++ seems pretty simple, example here.
一种解决方案是使用常规 __declspec(dllexport) 函数创建托管 C++ 类库,这些函数调用引用的 C# 类库中的托管方法。
示例 - 托管 C++ 项目中的 C++ 代码文件:
C# 模块(解决方案中的单独项目):
请注意,我正在使用
System.Windows.Forms.MessageBox.Show
演示这是一个实际的 .NET 调用> 打电话。基本(非 CLR)Win32 控制台应用程序示例:
请记住将 Win32 控制台应用程序与生成托管 C++ 项目所生成的
.lib
文件链接起来。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.Example - C++ code file in managed C++ project:
C# Module (separate project in solution):
Note that I am demonstrating that this is an actual .NET call by using a
System.Windows.Forms.MessageBox.Show
call.Sample basic (non-CLR) Win32 console application:
Remember to link the Win32 console application with the
.lib
file resulting from the build of the managed C++ project.在这种情况下,我希望看到一个 C++/CLI 聚会和一个使用 .NET Framework 命名管道的 C# 聚会。
In such occasion, I would like to see a C++/CLI party and a C# one using the .NET Framework's named pipes.
这个问题可以通过在单独的线程中运行阻塞管道函数来解决。
我发现这个问题正在寻找更好的解决方案,但这是我目前的基本想法:
需要注意的事项:
相反,您可以使用队列系统,其中通过管道发送的数据包从管道循环中排队,然后在主游戏线程中进行处理(这正是游戏本身的做法!)
This issue is solved by running the blocking pipe functions in a separate thread.
I found this issue looking for a better solution, but here is the basic idea of what I currently have:
Things to note:
Instead you can use a Queue system where packets sent through the pipe are queued up from your pipe loop, and then processed in the main game thread (this is exactly how the games themselves do it!)