在两个程序之间使用 CreateFileMapping - C
我有两个用 C 编写的窗口窗体应用程序,一个包含一个由两个整数组成的结构,另一个将使用 CreateFileMapping 接收它。
虽然没有直接相关,但我希望有三个事件,以便每个进程都可以相互“对话”,一个说第一个程序有东西要传递给第二个,一个说第一个程序已关闭,另一个说第二个已经关门了。
做到这一点的最佳方法是什么?我已经查看了 CreateFileMapping 操作的 MSDN 条目,但我仍然不确定应该如何完成它。
我不想在没有清楚地了解我需要做什么的情况下开始实施它。
感谢您抽出时间。
I have two window form applications written in C, one holds a struct consisting of two integers, another will receive it using the CreateFileMapping.
Although not directly related I want to have three events in place so each of the processes can "speak" to each other, one saying that the first program has something to pass to the second, one saying the first one has closed and another saying the second one has closed.
What would be the best way about doing this exactly? I've looked at the MSDN entry for the CreateFileMapping operation but I'm still not sure as to how it should be done.
I didn't want to start implementing it without having some sort of clear idea as to what I need to do.
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文件映射似乎不是处理此问题的最佳方法。仅在一个方向发送两个整数就会产生大量开销。对于这样的东西,我会考虑像管道这样的东西。管道会自动执行大多数其他详细信息,因此(例如)尝试读取或写入另一端已关闭的管道将失败,并且
GetLastError()
将返回ERROR_BROKEN_PIPE
。为了获得与第三个事件相同的事件(表示有东西在等待),您可以在重叠模式下使用管道。您可以等待管道句柄本身(请参阅文档中的注意事项)或使用 OVERLAPPED 结构,其中包含事件句柄。A file mapping does not seem like the best way to handle this. It has a lot of overhead for simply sending two integers in one direction. For something like that, I'd consider something like a pipe. A pipe automates most of the other details, so (for example) attempting to read or write a pipe that's been closed on the other end will fail and
GetLastError()
will returnERROR_BROKEN_PIPE
. To get the equivalent of the third event (saying there's something waiting) you work with the pipe in overlapped mode. You can wait on the pipe handle itself (see caveats in the documentation) or use anOVERLAPPED
structure, which includes a handle for an event.为了回答您的问题:如果您想使用共享内存,您将如何做到这一点,您可以使用共享内存中的一个字节在两个进程之间进行通信。这是一些示例代码。您可以轻松地用信号量替换等待循环
/
客户端进程将是等效的,但调用 MapViewOfFile() 之后的 else 情况下的代码将如下所示:
In answer to your question of how you WOULD do it if you wanted to used Shared Memory, you could use a byte in the shared memory to communicate between the two processes. Here is some sample code. You can easily replace the wait loops with semaphores
/
The client process would be equivalent but the code in the else case following the call to MapViewOfFile() would look something like this:
您可以使用 CreateSemaphore 并为要创建的最后一个参数提供名称一个命名信号量。进程可以共享该信号量(其他进程将使用 OpenSemaphore)。当数据准备好时,一个进程会发出信号,而另一个进程可以等待。
话虽如此,我必须同意杰瑞的观点,即使用管道可能会更简单地使其工作。另一方面,如果有必要移植的话,使用带有信号量(或事件)的共享内存方法可以更简单地转换到其他平台(例如Linux)。
You can use CreateSemaphore and provide a name for the last parameter to create a named semaphore. Processes can share that semaphore (the other process would use OpenSemaphore). One process signals when the data is ready and the other can wait on it.
Having said this, I have to agree with Jerry that using a pipe might be a lot simpler to get it working. On the other hand, using the shared memory approach with semaphores (or events) may translate more simply to other platforms (e.g., Linux) if it becomes necessary to port it.