命名管道 跨进程通信实例
2009-11-12 16:42
513 查看
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);
}
}
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);
}
}
相关文章推荐
- 命名管道跨进程通信实例2(转)
- System.IO系列:局域网内多线程使用命名管道在进程之间通信实例
- 多线程编程实例----进程通信之命名管道
- System.IO系列:局域网内多线程使用命名管道在进程之间通信实例
- 命名管道跨进程通信实例1(转)
- Linux c 匿名管道实例—创建两个子进程与父进程进行管道通信
- Linux进程通信之管道(匿名,命名)
- linux进程通信--有名管道(FIFO)(含实例代码)
- [置顶] 【Linux】 进程通信--命名管道
- 9、进程通信之命名管道
- 命名管道进程通信
- C++和C#进程间通过命名管道来通信(附源码)—上
- Linux进程通信 命名管道
- 进程间命名管道通信示例
- Python进程通信之匿名管道实例讲解
- 采用虚拟命名管道的字符设备和阻塞型I/O实现进程间的通信实现KWIC程序
- linux进程间的通信(C): 命名管道
- Linux进程通信:命名管道FIFO小结
- 进程通信1——管道、命名管道
- C++和C#进程间通过命名管道来通信(附源码)—上