📄 pipeservdlg.cpp
字号:
// PipeServDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PipeServ.h"
#include "PipeServDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BUFFER_SIZE 512
#define WM_USER_UPDATE_RECV_DATA WM_USER+200
#define WM_USER_CLEAR_SEND_DATA WM_USER+201
#define WM_USER_ENABLE_SEND_BUTTON WM_USER+202
#define WM_USER_TERMINATE_APP WM_USER+203
struct ThreadData {
HANDLE hINPipe;
HANDLE hONPipe;
HANDLE hIEvent;
HANDLE hOEvent;
HWND hWnd;
CString data;
};
LONG ReadClientData( LPVOID pTD);
LONG WriteClientData( LPVOID pTD);
LONG WaitForClientIConnect(LPVOID pTD);
LONG WaitForClientOConnect(LPVOID pTD);
/////////////////////////////////////////////////////////////////////////////
// CPipeServDlg dialog
CPipeServDlg::CPipeServDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPipeServDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPipeServDlg)
m_sendmsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPipeServDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPipeServDlg)
DDX_Control(pDX, IDC_RECEIVEDMSG, m_rcvdmsg);
DDX_Text(pDX, IDC_SENDMSG, m_sendmsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPipeServDlg, CDialog)
//{{AFX_MSG_MAP(CPipeServDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnSend)
ON_MESSAGE(WM_USER_UPDATE_RECV_DATA, OnUpdateRecvData)
ON_MESSAGE(WM_USER_CLEAR_SEND_DATA, OnClearSendData)
ON_MESSAGE(WM_USER_ENABLE_SEND_BUTTON, OnEnableSendButton)
ON_MESSAGE(WM_USER_TERMINATE_APP, OnCancel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPipeServDlg message handlers
BOOL CPipeServDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
GetDlgItem(IDOK)->EnableWindow(FALSE);
// TODO: Add extra initialization here
hIEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hOEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ThreadData *td1 = new ThreadData;
ThreadData *td2 = new ThreadData;
ThreadData *td3 = new ThreadData;
td1->hINPipe = td2->hINPipe = td3->hINPipe = hINPipe;
td1->hONPipe = td2->hONPipe = td3->hONPipe = hONPipe;
td1->hIEvent = td2->hIEvent = td3->hIEvent = hIEvent;
td1->hOEvent = td2->hOEvent = td3->hOEvent = hOEvent;
td1->hWnd = td2->hWnd = td3->hWnd = GetSafeHwnd();
td1->data = td2->data = td3->data = "";
DWORD dwThreadClientIConnect, dwThreadClientOConnect, dwThreadReadClient;
ThreadClientIConnect = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)WaitForClientIConnect, (LPVOID)td1, 0, &dwThreadClientIConnect);
ThreadClientOConnect = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)WaitForClientOConnect, (LPVOID)td2, 0, &dwThreadClientOConnect);
ThreadReadClient = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)ReadClientData, (LPVOID)td3, 0, &dwThreadReadClient);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPipeServDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPipeServDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPipeServDlg::OnSend()
{
// TODO: Add your control notification handler code here
ThreadData *td = new ThreadData;
UpdateData();
td->hONPipe = hONPipe;
td->hOEvent = hOEvent;
td->hWnd = GetSafeHwnd();
td->data = m_sendmsg;
DWORD dwThreadWriteClient;
ThreadWriteClient = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)WriteClientData, (LPVOID)td, 0, &dwThreadWriteClient);
}
void CPipeServDlg::OnCancel()
{
// TODO: Add extra cleanup here
TerminateThread (ThreadReadClient, -1);
TerminateThread (ThreadWriteClient, -1);
TerminateThread (ThreadClientIConnect, -1);
TerminateThread (ThreadClientOConnect, -1);
DisconnectNamedPipe(hINPipe);
DisconnectNamedPipe(hONPipe);
CloseHandle(hIEvent);
CloseHandle(hOEvent);
CloseHandle(hINPipe);
CloseHandle(hONPipe);
CDialog::OnCancel();
}
LRESULT CPipeServDlg::OnUpdateRecvData(WPARAM wParam, LPARAM lParam)
{
char szAppendBuf[BUFFER_SIZE + 10];
sprintf(szAppendBuf, "%s\r\n", (LPCTSTR)lParam);
m_rcvdmsg.SetSel(-1, 0);
m_rcvdmsg.ReplaceSel(szAppendBuf);
return 0;
}
LRESULT CPipeServDlg::OnClearSendData(WPARAM wParam, LPARAM lParam)
{
m_sendmsg = "";
UpdateData(FALSE);
return 0;
}
LRESULT CPipeServDlg::OnEnableSendButton(WPARAM wParam, LPARAM lParam)
{
GetDlgItem(IDOK)->EnableWindow(TRUE);
return 0;
}
void CPipeServDlg::SetIPipeHandle(HANDLE hHandle)
{
hINPipe = hHandle;
}
void CPipeServDlg::SetOPipeHandle(HANDLE hHandle)
{
hONPipe = hHandle;
}
LONG ReadClientData( LPVOID pTD)
{
ThreadData *td = (ThreadData *)pTD;
HANDLE hINPipe = td->hINPipe;
HANDLE hIEvent = td->hIEvent;
HWND hWnd = td->hWnd;
BOOL rc;
char * szBuf = new char [BUFFER_SIZE];
DWORD dwBytesRead;
DWORD dwRC;
WaitForSingleObject(hIEvent, INFINITE);
while (TRUE)
{
dwBytesRead = 0;
rc = ReadFile (hINPipe, szBuf, BUFFER_SIZE, &dwBytesRead, NULL);
dwRC = GetLastError();
if (dwBytesRead)
{
szBuf[dwBytesRead] = 0;
SendMessage((HWND)hWnd, WM_USER_UPDATE_RECV_DATA, NULL, (LPARAM)szBuf);
}
if (dwRC != ERROR_SUCCESS)
{
char szErrorMsg[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRC, 0, szErrorMsg, 1024, NULL);
AfxMessageBox(szErrorMsg);
break;
}
}
delete [] szBuf;
delete td;
PostMessage((HWND)hWnd, WM_USER_TERMINATE_APP, NULL, NULL);
return 0;
}
LONG WriteClientData( LPVOID pTD)
{
DWORD dwBytesWritten = 0;
BOOL rc;
ThreadData *td = (ThreadData *)pTD;
HANDLE hONPipe = td->hONPipe;
HWND hWnd = td->hWnd;
HANDLE hOEvent = td->hOEvent;
CString Clientdata = td->data;
WaitForSingleObject(hOEvent, INFINITE);
rc = WriteFile (hONPipe, (LPCVOID)((LPCTSTR)(td->data)),
(td->data).GetLength(), &dwBytesWritten, NULL);
if (!rc)
{
DWORD dwRC;
char szErrorMsg[1024];
dwRC = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRC, 0, szErrorMsg, 1024, NULL);
AfxMessageBox(szErrorMsg);
}
SendMessage((HWND)hWnd, WM_USER_CLEAR_SEND_DATA, NULL, NULL);
FlushFileBuffers(hONPipe);
delete td;
return 0;
}
LONG WaitForClientIConnect(LPVOID pTD)
{
BOOL rc;
ThreadData *td = (ThreadData *)pTD;
HANDLE hINPipe = td->hINPipe;
HANDLE hIEvent = td->hIEvent;
rc = ConnectNamedPipe(hINPipe, NULL);
if (rc != 0 || GetLastError() == ERROR_PIPE_CONNECTED)
{
SetEvent(hIEvent);
}
delete td;
return 0;
}
LONG WaitForClientOConnect(LPVOID pTD)
{
BOOL rc;
ThreadData *td = (ThreadData *)pTD;
HANDLE hONPipe = td->hONPipe;
HANDLE hOEvent = td->hOEvent;
HWND hWnd = td->hWnd;
rc = ConnectNamedPipe(hONPipe, NULL);
if (rc != 0 || GetLastError() == ERROR_PIPE_CONNECTED)
{
SetEvent(hOEvent);
}
SendMessage((HWND)hWnd, WM_USER_ENABLE_SEND_BUTTON, NULL, NULL);
delete td;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -