命名管道 跨进程通信实例

CLIENT:

 DWORD WINAPI PipeThreadProc(LPVOID lpParameter)
{
 HANDLE hPipe;
 
 //Connect to the server pipe using CreateFile()
 hPipe = CreateFile(
  g_szPipeName,   // pipe name
  GENERIC_READ |  // read and write access
  GENERIC_WRITE,
  0,              // no sharing
  NULL,           // default security attributes
  OPEN_EXISTING,  // opens existing pipe
  0,              // default attributes
  NULL);          // no template file
 
 if (INVALID_HANDLE_VALUE == hPipe)
 {
   
 }
 else
 {
  
 }
 
 //We are done connecting to the server pipe,
 //we can start communicating with the server using ReadFile()/WriteFile()
 //on handle - hPipe
 
 char szBuffer[1024]={"ExitProcess"};
 

 
 DWORD cbBytes;
 
 //Send the message to server
 BOOL bResult = WriteFile(
  hPipe,                // handle to pipe
  szBuffer,             // buffer to write from
  strlen(szBuffer)+1,   // number of bytes to write, include the NULL
  &cbBytes,             // number of bytes written
  NULL);                // not overlapped I/O
 
 if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
 {
 
  CloseHandle(hPipe);
  return 1;  //Error
 }
 else
 {
  OutputDebugString("WriteFile() was successful.");
 }
 

 
     CloseHandle(hPipe);
 
 return 0;
}

SERVER:

DWORD WINAPI PipeThreadProc(LPVOID lpParameter)
{
 HANDLE hPipe;
 char szBuffer[1024];
 DWORD cbBytes;
 BOOL bResult;
 
 hPipe = CreateNamedPipe(
  g_szPipeName,             // pipe name
  PIPE_ACCESS_DUPLEX,       // read/write access
  PIPE_TYPE_MESSAGE |       // message type pipe
  PIPE_READMODE_MESSAGE |   // message-read mode
  PIPE_WAIT,                // blocking mode
  PIPE_UNLIMITED_INSTANCES, // max. instances 
  1024,              // output buffer size
  1024,              // input buffer size
  NMPWAIT_USE_DEFAULT_WAIT, // client time-out
  NULL);                    // default security attribute
 
 if (INVALID_HANDLE_VALUE == hPipe)
 {
    ShowErrorMessage("CreateNamedPipe");
  return 1;  //Error
 }
 else
 {
  printf("/nCreateNamedPipe() was successful.");
 }
 
 printf("/nWaiting for client connection...");
 
 //Wait for the client to connect
 while (1)
 {
   OutputDebugString("/nWaiting for client connection...");
 if (FALSE == ConnectNamedPipe(hPipe, NULL))
 {
     ShowErrorMessage("ConnectNamedPipe");
  CloseHandle(hPipe);
  return 1;  //Error
 }
 else
 {
  OutputDebugString("/nConnectNamedPipe() was successful.");
 }
 

 
 //We are connected to the client.
 //To communicate with the client we will use ReadFile()/WriteFile()
 //on the pipe handle - hPipe
 
 //Read client message
  bResult = ReadFile(
  hPipe,                // handle to pipe
  szBuffer,             // buffer to receive data
  sizeof(szBuffer),     // size of buffer
  &cbBytes,             // number of bytes read
  NULL);                // not overlapped I/O
 
 if ( (!bResult) || (0 == cbBytes))
 {
 // printf("/nError occurred while reading from the client: %d", GetLastError());
  CloseHandle(hPipe);
  return 1;  //Error
 }
 else
 {
 // printf("/nReadFile() was successful.");
  
  OutputDebugString(szBuffer);
  if (lstrcmpi(szBuffer,"ExitProcess")==0)
  {
   OutputDebugString("SnedMessage");
   SendMessage(_hThisWnd, WM_DESTROY, 0, 0L);
   
  }else if (lstrcmpi(szBuffer,"LoadSettings")==0)
  {
   OutputDebugString("LoadSettings command");
   //LoadAllSettings();
   LoadQuickScrllSetting();
  }
  
  
    OutputDebugString("pelmiser------PipeTimerProc end");
 }
 


    FlushFileBuffers(hPipe);
    DisconnectNamedPipe(hPipe);
   // CloseHandle(hPipe);
   }

 
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值