📄 mailslotserverview.cpp
字号:
// MailslotServerView.cpp : implementation of the CMailslotServerView class
//
#include "stdafx.h"
#include "MailslotServer.h"
#include "MailslotServerDoc.h"
#include "MailslotServerView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT ReadProc(LPVOID lpParam)
{
// 数据接收缓存
char buffer[256];
// 实际接收到的字节数
DWORD dwNumberOfBytesRead;
// 得到视指针
CMailslotServerView* pView = (CMailslotServerView*)lpParam;
// 持续对邮槽进行读取
while (ReadFile(pView->m_hMailSlot, buffer, 256, &dwNumberOfBytesRead, NULL) != 0)
{
// 显示从邮槽接收到的数据
buffer[dwNumberOfBytesRead] = '\0';
pView->m_sMessage = CString(buffer);
pView->Invalidate();
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView
IMPLEMENT_DYNCREATE(CMailslotServerView, CView)
BEGIN_MESSAGE_MAP(CMailslotServerView, CView)
//{{AFX_MSG_MAP(CMailslotServerView)
ON_COMMAND(ID_CREATE_MAILSLOT, OnCreateMailslot)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView construction/destruction
CMailslotServerView::CMailslotServerView()
{
// TODO: add construction code here
}
CMailslotServerView::~CMailslotServerView()
{
}
BOOL CMailslotServerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView drawing
void CMailslotServerView::OnDraw(CDC* pDC)
{
CMailslotServerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->TextOut(10, 10, m_sMessage);
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView printing
BOOL CMailslotServerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMailslotServerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMailslotServerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView diagnostics
#ifdef _DEBUG
void CMailslotServerView::AssertValid() const
{
CView::AssertValid();
}
void CMailslotServerView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMailslotServerDoc* CMailslotServerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMailslotServerDoc)));
return (CMailslotServerDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView message handlers
void CMailslotServerView::OnCreateMailslot()
{
// 创建邮槽
m_hMailSlot = CreateMailslot("\\\\.\\Mailslot\\Test", 0, MAILSLOT_WAIT_FOREVER, NULL);
if (m_hMailSlot == INVALID_HANDLE_VALUE)
{
m_sMessage.Format("创建邮槽失败,失败代码:%d", GetLastError());
Invalidate();
return;
}
else
{
m_sMessage = "成功创建邮槽!";
Invalidate();
}
// 开启数据接收线程
AfxBeginThread(ReadProc, this);
}
BOOL CMailslotServerView::DestroyWindow()
{
// 关闭邮槽句柄
CloseHandle(m_hMailSlot);
return CView::DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -