1、服务端MFC
#define MY_PIPE_NAME L"\\\\.\\pipe\\__MY__PIPE__TEST__" #define BUFSIZE 1024 HANDLE m_hPipe; BOOL m_bConnected; int m_bMsgNum; //使用ConnectNamedPipe会阻塞,直到客户端进行连接 //不使用ConnectNamedPipe则不会阻塞,但一样可以做后续操作 void XXX::PipeCreate() { m_hPipe = CreateNamedPipe( MY_PIPE_NAME, // pipe name PIPE_ACCESS_DUPLEX, // read/write access PIPE_TYPE_MESSAGE | // message type pipe PIPE_READMODE_MESSAGE | // message-read mode PIPE_WAIT, // blocking mode 1, // max. instances BUFSIZE, // output buffer size BUFSIZE, // input buffer size 0, // client time-out NULL); // default security attribute if (m_hPipe == INVALID_HANDLE_VALUE) { ::MessageBox(NULL, L"CreateNamedPipe Error", L"CreateNamedPipe", MB_OK); } else { ::MessageBox(NULL, L"CreateNamedPipe OK", L"CreateNamedPipe", MB_OK); } m_bConnected = ConnectNamedPipe(m_hPipe, NULL); if (m_bConnected) { ::MessageBox(NULL, L"ConnectNamedPipe OK", L"ConnectNamedPipe", MB_OK); } else { ::MessageBox(NULL, L"ConnectNamedPipe Error", L"ConnectNamedPipe", MB_OK); } m_bMsgNum = 0; } //WriteFile会阻塞,等待客户端读取完毕 void XXX::PipeWrite() { DWORD dwWritten; TCHAR buffer[BUFSIZE]; int n = sizeof(buffer); _stprintf_s(buffer, L"This the %d message", m_bMsgNum++); if (!WriteFile(m_hPipe, buffer, n, &dwWritten, NULL)) { ::MessageBox(NULL, L"WriteFile Failed", L"WriteFile", MB_OK); } } //两边都关闭,才可以重新建立管道 void XXX::PipeClose() { if (m_bConnected) { DisconnectNamedPipe(m_hPipe); m_bConnected = FALSE; } if (m_hPipe!=NULL && m_hPipe != INVALID_HANDLE_VALUE) { CloseHandle(m_hPipe); m_hPipe=NULL; } }
2、客户端MFC
#define MY_PIPE_NAME L"\\\\.\\pipe\\__MY__PIPE__TEST__" #define BUFSIZE 1024 HANDLE m_hPipe; BOOL m_bConnected; //WaitNamedPipe会等待ConnectNamedPipe void XXX::PipeConnect() { if (WaitNamedPipe(MY_PIPE_NAME, NMPWAIT_WAIT_FOREVER) == 0) { MessageBox(L"WaitNamedPipe failed"); return; } m_hPipe = CreateFile(MY_PIPE_NAME, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hPipe == INVALID_HANDLE_VALUE) { m_bConnected = FALSE; ::MessageBox(NULL, L"CreateFile Error", L"CreateNamedPipe", MB_OK); } else { m_bConnected = TRUE; ::MessageBox(NULL, L"CreateFile OK", L"CreateNamedPipe", MB_OK); } } //ReadFile会阻塞等待写入 void XXX::PipeRead() { DWORD dwBytesRead; TCHAR buffer[BUFSIZE]; int bufsize = sizeof(buffer); memset(buffer, 0x00, bufsize); if (m_bConnected) { //C#程序不处理的话,第一次读会读到BOM(Byte Order Mark 0xfeff) if (ReadFile(m_hPipe, buffer, bufsize, &dwBytesRead, NULL)) { ::MessageBox(NULL, buffer, L"ReadFile", MB_OK); } } } //两边都关闭,才可以重新建立管道 void XXX::PipeClose() { if (m_hPipe!=NULL && m_hPipe != INVALID_HANDLE_VALUE) { CloseHandle(m_hPipe); m_hPipe=NULL; } }