2016年11月14日 星期一

[C++]Interprocess Communications (MailSlot)

#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>
#include <stdio.h>

HANDLE hSlot;
LPTSTR Slot = TEXT("\\\\.\\mailslot\\test_mailslot");

BOOL ReadSlot()
{
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, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);

if (!fResult)
{
printf("Get MailSlot failed %d.\n", GetLastError());
return FALSE;
}

if (cbMessage == MAILSLOT_NO_MESSAGE)
{
printf("Waiting for a message\n");
return TRUE;
}

cAllMessages = cMessage;

while (cMessage != 0)
{
StringCchPrintf((LPTSTR)achID, 80, TEXT("\nMessage #%d of %d\n"), cAllMessages - cMessage + 1, cAllMessages);

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("Read Fail %d\n", GetLastError());
GlobalFree((HGLOBAL)lpszBuffer);
return FALSE;
}

StringCbCat(lpszBuffer, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage, (LPTSTR)achID);

_tprintf(TEXT("Contents of the mail:%s\n"), lpszBuffer);

GlobalFree((HGLOBAL)lpszBuffer);


fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);

if (!fResult)
{
printf("Get Mail Slot Info %d\n", GetLastError());
}

}
CloseHandle(hEvent);
return TRUE;
}

int main()
{



hSlot = CreateMailslot(Slot, 0, MAILSLOT_WAIT_FOREVER, (LPSECURITY_ATTRIBUTES) NULL);

if (hSlot == INVALID_HANDLE_VALUE)
{
printf("Create Mail Slot Failed:%d \n", GetLastError()); 
return 0;
}
else printf("Mail slot create ok!!\n");

while (TRUE)
{
ReadSlot();
Sleep(3000);
}
}



=============================================================================================

#include "stdafx.h"

#include  <windows.h>
#include <stdio.h>


LPTSTR SlotName = TEXT("\\\\.\\mailslot\\test_mailslot");

int main()
{
    
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;

fResult = WriteFile(hfile, SlotName, (DWORD)(lstrlen(SlotName) + 1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED)NULL);

if (!fResult)
{
printf("Write file failed:%d \n", GetLastError());

return FALSE;
}

printf("Slot written to successfully \n");


CloseHandle(hfile);


for (int h = 0; h < 10000; h++) Sleep(2000);

return 0;

}



執行結果:











參考文章:
https://msdn.microsoft.com/zh-tw/library/windows/desktop/dd374074(v=vs.85).aspx
https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms682396(v=vs.85).aspx
https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms647541(v=vs.85).aspx
http://www.cnblogs.com/songliquan/p/3396201.html

沒有留言:

張貼留言