📄 mailslot.cpp
字号:
// MailSlot.cpp: implementation of the CMailSlot class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MailSlot.h"
#include "NTService.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//#define MAILSLOT_NAME _T("WINNOT\\SERVER")
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//CMailSlot::CMailSlot()
// : m_serverName = NULL, m_slotName = NULL;
//{
// CMailSlot("", "");
//}
CMailSlot::CMailSlot(LPCSTR serverName, LPCSTR slotName)
{
memset(m_serverName,0,MAX_NAME_SIZE+1);
memset(m_slotName,0,MAX_NAME_SIZE+1);
strcpy(m_serverName, serverName);
strcpy(m_slotName, slotName);
m_mailslot = INVALID_HANDLE_VALUE;
}
CMailSlot::~CMailSlot()
{
Close();
if(m_mailslot!=NULL)
CloseHandle(m_mailslot);
}
//void CMailSlot::SetServerName(LPCSTR serverName)
void CMailSlot::SetServerName(LPCSTR serverName)
{
strcpy(m_serverName, serverName);
}
//void CMailSlot::SetMailSlotName(LPCSTR slotName)
void CMailSlot::SetMailSlotName(LPCSTR slotName)
{
strcpy(m_slotName, slotName);
}
//HANDLE CMailSlot::GetHandle()
HANDLE CMailSlot::GetHandle()
{
return m_mailslot;
}
//BOOL CMailSlot::Create(LPCSTR serverName)
BOOL CMailSlot::Create(LPCSTR serverName)
{
if (serverName != NULL)
strcpy(m_serverName, serverName);
if ( IsOpen() ) // close old
Close();
//strcpy(m_slotName, slotName);
TCHAR pszMailslotName[512];
_tcscpy(pszMailslotName, _T("\\\\"));
_tcscat(pszMailslotName, m_serverName);
_tcscat(pszMailslotName, _T("\\mailslot\\"));
_tcscat(pszMailslotName, m_slotName);
m_mailslot = CreateFile(
pszMailslotName,
GENERIC_WRITE,
FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (m_mailslot == INVALID_HANDLE_VALUE)
{
//DWORD err = GetLastError();
//showMessError(err);
//TRACE1("CMailSlot::Open() failed, GetLastError returned %d\n", err);
}
return (m_mailslot != INVALID_HANDLE_VALUE);
}
//void CMailSlot::showMessError(DWORD errNum)
void CMailSlot::showMessError(DWORD errNum)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
::MessageBox( ((CBQQApp*)AfxGetApp())->m_pMainWnd->GetSafeHwnd(), (LPCTSTR)lpMsgBuf, "错误提示", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
//BOOL CMailSlot::Write(LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite)
BOOL CMailSlot::Write(LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite)
{
ASSERT(IsOpen());
DWORD dwNumberOfBytesWritten;
BOOL bSuccess = WriteFile(m_mailslot, lpBuffer, dwNumberOfBytesToWrite,
&dwNumberOfBytesWritten, (LPOVERLAPPED) NULL);
if ( (!bSuccess) || (dwNumberOfBytesToWrite != dwNumberOfBytesWritten) )
{
DWORD err = GetLastError();
showMessError(err);
TRACE1("CMailSlot::Write() failed, GetLastError returned %d\n", err);
}
return bSuccess;
}
//BOOL CMailSlot::IsOpen() const
BOOL CMailSlot::IsOpen() const
{
return (m_mailslot != INVALID_HANDLE_VALUE);
}
//BOOL CMailSlot::Close()
BOOL CMailSlot::Close()
{
BOOL bSuccess = TRUE;
if (IsOpen())
{
bSuccess = CloseHandle(m_mailslot);
m_mailslot = INVALID_HANDLE_VALUE;
}
if (!bSuccess)
{
DWORD err = GetLastError();
showMessError(err);
TRACE1("CMailSlot::Close() failed, GetLastError returned %d\n", err);
}
return bSuccess;
}
//BOOL CMailSlot::CreateServer()
BOOL CMailSlot::CreateServer()
{
TCHAR pszMailslotName[512];
_tcscpy(pszMailslotName, _T("\\\\.\\mailslot\\"));
_tcscat(pszMailslotName, m_slotName);
if ( CNTService::IsWindowsNT() )
{
// Get computer name
TCHAR sComputerName[MAX_NAME_SIZE+1];
DWORD nSize = MAX_NAME_SIZE;
if ( !GetComputerName( sComputerName, &nSize) )
strcpy(sComputerName, "unknown");
if ( CNTService::IsServiceStarted(sComputerName, MESSENGER_SERVICE_NAME)==1 )
{
//int result = ::MessageBox( ((CBQQApp*)AfxGetApp())->m_pMainWnd->GetSafeHwnd(), "Windows NT messenger service 正在运行. 关掉它吗?", "提示", MB_YESNO | MB_ICONSTOP );
//if ( result == IDYES )
{
CNTService::StopService(sComputerName, MESSENGER_SERVICE_NAME);// stop it
Sleep(3000);
}
}
else if ( CNTService::IsServiceStarted(sComputerName, MESSENGER_SERVICE_NAME)==3 )
{
::MessageBox( ((CBQQApp*)AfxGetApp())->m_pMainWnd->GetSafeHwnd(), "Windows NT messenger service 状态未知,请稍后重试运行!\r\n否则,您只能发送消息", "提示", MB_ICONINFORMATION|MB_OK );
return FALSE;
}
}
m_mailslot = CreateMailslot(
pszMailslotName,
0,
MAILSLOT_WAIT_FOREVER,
NULL);
if (m_mailslot == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
//showMessError(err);
::MessageBox(NULL, "系统存在另外一个信使在运行,请关掉它\r\n否则,您只能发送消息", "提示", MB_OK | MB_ICONSTOP );
TRACE1("CMailSlot::Open() failed, GetLastError returned %d\n", err);
//return FALSE;
}
//return TRUE;
return (m_mailslot != INVALID_HANDLE_VALUE);
}
//BOOL CMailSlot::Read(LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, DWORD &dwNumberOfBytesRead)
BOOL CMailSlot::Read(LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, DWORD &dwNumberOfBytesRead)
{
ASSERT(IsOpen());
BOOL bSuccess = ReadFile(m_mailslot, lpBuffer, dwNumberOfBytesToRead, &dwNumberOfBytesRead, (LPOVERLAPPED) NULL);
if (!bSuccess)
TRACE1("CMailslot::Read() failed, GetLastError returned %d\n", GetLastError());
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -