简体   繁体   English

Win32 进程间通信分配

[英]Win32 Inter Process Communication Assignment

I need to do a C++ assignment, but I'm pretty much new to Win32 IPC.我需要做一个 C++ 任务,但我对 Win32 IPC 还很陌生。 I did a little bit of research yesterday, but I cannot find the thing I'm searching for.我昨天做了一些研究,但找不到我要找的东西。

Basically, I need two programs, the first creates a FileMapping with paging file, waits a buffer, display the buffer, and closes it.基本上,我需要两个程序,第一个创建一个带有分页文件的 FileMapping,等待缓冲区,显示缓冲区,然后关闭它。

The second connects to the Communication Channel, writes the buffer to the first program, and then closes.第二个连接到通信通道,将缓冲区写入第一个程序,然后关闭。

The closest thing I've come to is this resource: IPC Communication but the guy there uses pipes instead of communication channels using paging file.我最接近的是这个资源: IPC 通信但是那里的人使用管道而不是使用页面文件的通信通道。

Also, I found that I can open a FileMapping with paging file pretty much like this:另外,我发现我可以打开一个带有分页文件的 FileMapping,就像这样:

TCHAR szMapFileName[] = _T("Local\\HelloWorld"); 

HANDLE hMapFile = CreateFileMapping( 
       INVALID_HANDLE_VALUE,       

       NULL,                      // Default security 
       PAGE_READWRITE,            // Read/write access 
       0,                         // Max. object size 
       BUFFER_SIZE,               // Buffer size  
       szMapFileName              // Name of mapping object);

If somebody can provide a little help that would be very valuable(maybe a skeleton of an app?).如果有人可以提供一些非常有价值的帮助(也许是应用程序的框架?)。 I tried to do some research yesterday, but in vain.昨天我试图做一些研究,但徒劳无功。

Thanks谢谢

CreateFileMapping() , OpenFileMapping() and MapViewOfFile() are indeed the functions you need to call to allocate a shared memory buffer. CreateFileMapping()OpenFileMapping()MapViewOfFile()确实是您需要调用以分配共享 memory 缓冲区的函数。

The first app must:第一个应用程序必须:

  • Create the File Mapping.创建文件映射。
  • Create a synchronization object, preferably an Event, to wait on it.创建一个同步object,最好是一个Event,等待它。
  • Create an additional thread, which will be waiting for the Event to be signaled.创建一个额外的线程,该线程将等待 Event 发出信号。 Doing this in the main (UI) thread would block the application.在主 (UI) 线程中执行此操作会阻塞应用程序。
  • When the Event is signaled, post a custom message ( WM_APP+nnn ) to the main thread, to display the contents of the buffer.当事件发出信号时,将自定义消息 ( WM_APP+nnn ) 发布到主线程,以显示缓冲区的内容。
  • It's a matter of specs or design what to do after this point, eg exit the application, just not be waiting for the buffer to receive data anymore, clear the event and wait again, etc.这是规范或设计的问题,在此之后要做什么,例如退出应用程序,只是不再等待缓冲区接收数据,清除事件并再次等待等。

The second app must:第二个应用程序必须:

  • Open the File Mapping and the Event.打开文件映射和事件。 If failed, display an error message or warning and exit.如果失败,则显示错误消息或警告并退出。
  • Write the data to the shared memory buffer.将数据写入共享的 memory 缓冲区。
  • Signal the Event.发出事件信号。
  • Exit.出口。

It could be further improved, eg the second app may not write to the buffer if the event is not in the nonsignaled state.它可以进一步改进,例如,如果事件不在未发出信号的 state 中,则第二个应用程序可能不会写入缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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