c++ - 管道通信 C++

标签 c++ windows pipe

我正在编写两个必须通信的小型 C++ 应用程序。第一个将是一项服务,每隔一段时间,必须提醒用户注意某事。由于服务无法创建窗口,因此我将应用程序设计为两个独立的可执行文件。

该服务将使用通知程序进行通信。

该服务只需要向通知程序发送文本消息,通知程序会在系统托盘中显示一个气球。

我正在尝试使用命名管道,我想我已经差不多了,但还不够。到目前为止我所拥有的是:

在通知方:

  m_hInPipe = CreateNamedPipe(L"\\\\.\\pipe\\nhsupspipe", PIPE_ACCESS_INBOUND,
                PIPE_WAIT, 1, 1024, 1024, 60, NULL);

意思是我创建了一个名为 nhsupspipe 的管道,一个入站管道。

在服务端:

if (!WriteFile(m_hOutPipe, "My message to the user?", 23, &escritos, &o))
     std::cout << "ERROR: " << GetLastError();

调试我可以看到一切正常,管道已创建并且 WriteFile 将我的 23 个字节写入管道。

我的问题是:在通知方,我如何才能读取这些字节?是否有消息发送给进程?我必须为管道编写处理程序吗?有什么事吗?

最佳答案

来自客户端(您的服务)和服务器(通知程序)的一些简单片段 [注意:这是改编 self 前一段时间完成的一个项目,而该项目又受到 CreateNamedPipe & co 的 MSDN 示例的严重“影响”]:

服务器端:

HANDLE hPipe = INVALID_HANDLE_VALUE;
bool bConnected = false;

 hPipe = CreateNamedPipe( L"\\\\.\\pipe\\nhsupspipe",
                          PIPE_ACCESS_DUPLEX,
                          PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
                          PIPE_UNLIMITED_INSTANCES,
                          sizeof( Message ),
                          0,
                          0,
                          NULL );

 // failed to create pipe?
 if( hPipe == INVALID_HANDLE_VALUE ){
   return -1;
 }

 // Wait for the client to connect; if it succeeds, 
 // the function returns a nonzero value. If the function
 // returns zero, GetLastError returns ERROR_PIPE_CONNECTED. 
 bConnected = ConnectNamedPipe( hPipe, NULL ) ? true : ( GetLastError() == ERROR_PIPE_CONNECTED );

 if( bConnected ){
  while( true ){ 
   unsigned long ulBytesRead = 0;
   // read client requests from the pipe. 
   bool bReadOk = ReadFile( hPipe,
                            &message,
                            sizeof( message ),
                            &ulBytesRead,
                            NULL );

   // bail if read failed [error or client closed connection]
   if( !bReadOk || ulBytesRead == 0 )
    break;

   // all ok, process the message received

  }
 }
 else{
   // the client could not connect, so close the pipe. 
   CloseHandle( hPipe );
 }

 return 0;

客户端:

HANDLE hPipe = INVALID_HANDLE_VALUE;

 // create the named pipe handle
 hPipe = CreateFile( L"\\\\.\\pipe\\nhsupspipe",
                     GENERIC_READ | GENERIC_WRITE,
                     0, 
                     NULL, 
                     OPEN_EXISTING,
                     0,
                     NULL );

 // if everything ok set mode to message mode
 if( INVALID_HANDLE_VALUE != hPipe ){
  DWORD dwMode = PIPE_READMODE_MESSAGE;
  // if this fails bail out
  if( !SetNamedPipeHandleState( hPipe, &dwMode, NULL, NULL ) ){
   CloseHandle( hPipe ); 

   return -1;
  }
 }

 unsigned long ulBytesWritten = 0;
 bool bWriteOk = WriteFile( hPipe, 
                            ( LPCVOID )&message, 
                            sizeof( Message ), 
                            &ulBytesWritten, 
                            NULL );

 // check if the writing was ok
 if( !bWriteOk || ulBytesWritten < sizeof( Message ) ){
  return -1;
 }

 // written ok

 return 0;

上面提到的Message 是一个将成为您的消息的结构,您可能需要pack。 .

由于在您的场景中,客户端(服务)可能会在服务器(通知程序)之前启动并运行,因此您需要在客户端实现某种重新连接策略。

稍微不同的是,您应该仔细考虑 Osterman 先生在他的 reply 中所说的话(即使没有别的,只因为他是拉里奥斯特曼)。

关于c++ - 管道通信 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1849918/

相关文章:

c++ - sscanf 输入不工作

c - 如何创建未定义数量的线程并在 Windows 上的 c 中使用 WaitForMultipleObjects()

windows - Webrick 和 Thin 在 Windows 中提供静态文件的速度非常慢。我怎样才能加快他们的速度?

python - 无法打开由 Python 中的线程创建的管道描述符

c - 在 C 中使用多个管道时如何处理命令

c++ - Chrono 程序中永远不会触发条件语句

c++ - 我可以在 VS2010 中使用 C++ TR1 吗?

c - 多线程从一个管道读取

c++ - ICU 自定义货币格式 (C++)

c++ - 乘以定义的符号: mysqlclient, pthread-win32