#include "InterprocessCommunications.h"
#include <string>
MailSlot::MailSlot() {
slotName = TEXT("\\\\.\\mailslot\\sample_mailslot");
}
MailSlot::~MailSlot() {
}
bool MailSlot::createSlot() {
hSlot = CreateMailslot(slotName,
0,
MAILSLOT_WAIT_FOREVER,
(LPSECURITY_ATTRIBUTES)NULL);
if (hSlot == INVALID_HANDLE_VALUE) {
return false;
} else {
return true;
}
}
bool MailSlot::writeSlot(const char* message) {
int num = MultiByteToWideChar(0, 0, message, -1, NULL, 0);
wchar_t* lpszMessage = new wchar_t[num];
MultiByteToWideChar(0, 0, message, -1, lpszMessage, num);
HANDLE hFile;
hFile = CreateFile(slotName,
GENERIC_WRITE,
FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("CreateFile failed with %d.\n", GetLastError());
return FALSE;
}
BOOL fResult;
DWORD cbWritten;
//Writing mailslot is similar to writing a standard disk file
fResult = WriteFile(hFile,
lpszMessage,
(DWORD)(lstrlen(lpszMessage) + 1) * sizeof(TCHAR),
&cbWritten,
(LPOVERLAPPED)NULL);
if (!fResult) {
return false;
}
return true;
}
bool MailSlot::readSlot(std::string& str) {
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPTSTR lpszBuffer;
TCHAR achID[80];
DWORD cAllMessages;
HANDLE hEvent;
OVERLAPPED ov;
cbMessage = cMessage = cbRead = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
if (NULL == hEvent)
return FALSE;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.hEvent = hEvent;
fResult = GetMailslotInfo(hSlot, // mailslot handle
(LPDWORD)NULL, // no maximum message size
&cbMessage, // size of next message
&cMessage, // number of messages
(LPDWORD)NULL); // no read time-out
if (!fResult)
{
printf("GetMailslotInfo failed with %d.\n", GetLastError());
return FALSE;
}
if (cbMessage == MAILSLOT_NO_MESSAGE)
{
printf("Waiting for a message...\n");
return TRUE;
}
cAllMessages = cMessage;
while (cMessage != 0) // retrieve all messages
{
// Create a message-number string.
StringCchPrintf((LPTSTR)achID,
80,
TEXT("\nMessage #%d of %d\n"),
cAllMessages - cMessage + 1,
cAllMessages);
// Allocate memory for the message.
lpszBuffer = (LPTSTR)GlobalAlloc(GPTR,
lstrlen((LPTSTR)achID) * sizeof(TCHAR) + cbMessage);
if (NULL == lpszBuffer)
return FALSE;
lpszBuffer[0] = '\0';
fResult = ReadFile(hSlot,
lpszBuffer,
cbMessage,
&cbRead,
&ov);
if (!fResult)
{
printf("ReadFile failed with %d.\n", GetLastError());
GlobalFree((HGLOBAL)lpszBuffer);
return FALSE;
}
// Display the message.
_tprintf(TEXT("Contents of the mailslot: %s\n"), lpszBuffer);
int num = WideCharToMultiByte(CP_OEMCP, NULL, lpszBuffer, -1, NULL, 0, NULL, FALSE);
char* pchar = new char[num];
WideCharToMultiByte(CP_OEMCP, NULL, lpszBuffer, -1, pchar, num, NULL, FALSE);
str = std::string(pchar);
GlobalFree((HGLOBAL)lpszBuffer);
fResult = GetMailslotInfo(hSlot, // mailslot handle
(LPDWORD)NULL, // no maximum message size
&cbMessage, // size of next message
&cMessage, // number of messages
(LPDWORD)NULL); // no read time-out
if (!fResult)
{
printf("GetMailslotInfo failed (%d)\n", GetLastError());
return FALSE;
}
}
CloseHandle(hEvent);
return TRUE;
}
/* -------------------------------------------------------------------------------- */
MyIPC::MyIPC() {
}
评论0