Skip to content

Instantly share code, notes, and snippets.

@Dobby233Liu
Created November 11, 2023 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobby233Liu/4e327fdf156fb438d7fb06c121f9c0b3 to your computer and use it in GitHub Desktop.
Save Dobby233Liu/4e327fdf156fb438d7fb06c121f9c0b3 to your computer and use it in GitHub Desktop.
// heavily modified version of https://copyprogramming.com/howto/using-mailslots#reading-from-a-mailslot
#include <iostream>
#include <Windows.h>
#define csMe "WinlogMonitor"
#define csMailslotName "WinLog"
int main()
{
HANDLE hSlot = CreateMailslotW(TEXT("\\\\.\\mailslot\\" csMailslotName), -1, MAILSLOT_WAIT_FOREVER, NULL);
if (hSlot == INVALID_HANDLE_VALUE) {
printf("<CreateMailslot failed with %d>\n", GetLastError());
return 1;
}
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT(csMe));
if (!hEvent) {
printf("<CreateEvent failed with %d>\n", GetLastError());
CloseHandle(hSlot);
return 1;
}
OVERLAPPED hOverlapped = {};
hOverlapped.Offset = 0;
hOverlapped.OffsetHigh = 0;
hOverlapped.hEvent = hEvent;
printf("<Waiting for messages in mailslot %s>\n\n", csMailslotName);
DWORD cbNextSize = 0;
for (;;) { // keep looking for messages (so it will use a bit of cpu)
if (!GetMailslotInfo(hSlot, NULL, &cbNextSize, NULL, NULL)) {
printf("<GetMailslotInfo failed with %d>\n", GetLastError());
continue;
}
if (cbNextSize == MAILSLOT_NO_MESSAGE)
continue;
std::string lpszBuffer(cbNextSize + 1, '\0');
if (!ReadFile(hSlot, &lpszBuffer[0], cbNextSize, NULL, &hOverlapped)) {
printf("<ReadFile failed with %d>\n", GetLastError());
continue;
}
std::cout << lpszBuffer;
}
CloseHandle(hSlot);
CloseHandle(hEvent);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment