#include <TCHAR.h> #include <windows.h> #include <stdio.h> #include <conio.h>
BOOL StopProcessing;
DWORD WINAPI ServeMailslot(LPVOID lpParameter); void SendMessageToMailslot(void);
int main(int argc, char* argv[]) {
DWORD ThreadId; HANDLE MailslotThread;
StopProcessing=FALSE; MailslotThread=CreateThread(NULL,0,ServeMailslot,NULL,0,&ThreadId); printf("Press a key to stop the server\n"); _getch(); StopProcessing=TRUE; if(WaitForSingleObject(MailslotThread,INFINITE)==WAIT_FAILED) { printf("WaitForsingleObject failed with error %d\n",GetLastError()); return 1; }
return 0; }
DWORD WINAPI ServeMailslot(LPVOID lpParameter) { char buffer[2048]; DWORD NumberOfBytesRead; DWORD Ret; HANDLE Mailslot; memset(buffer,0,2048); if((Mailslot=CreateMailslot(_T("\\\\.\\mailslot\\myslot"),2048,MAILSLOT_WAIT_FOREVER,NULL))==INVALID_HANDLE_VALUE) { printf("Failed to create a Mailslot %d\n",GetLastError()); return 0; } while((Ret=ReadFile(Mailslot,buffer,2048,&NumberOfBytesRead,NULL))!=0) { if(StopProcessing) break; printf("Received %d bytes \n",NumberOfBytesRead); printf("Data:%s\n",buffer); } CloseHandle(Mailslot); return 0; }
|