Write - Read-Create MailSlot (Data Flow)

Posts 1–3 of 3 · Page 1 of 1
Write - Read-Create MailSlot (Data Flow)
Hi Guys,
These codes provide the data flow..
WriteSlot = Writes the data flow
ReadSlot = Reads the data flow

WriteSlot;

Code:
BOOL __stdcall WriteSlot(char*  Slot,LPCVOID buffer,size_t size){
char SlotName[80];
HANDLE hFile; 
sprintf(SlotName,"\\\\.\\mailslot\\%s",Slot);
char empery[50];
hFile = CreateFile(SlotName,GENERIC_WRITE,FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);    
if (hFile == INVALID_HANDLE_VALUE){ 
sprintf(empery,"CreateFile failed with%d.\n",GetLastError()); 
MessageBox(0,empery,"",0);
return FALSE; 
} 
BOOL fResult; 
DWORD cbWritten; 
fResult = WriteFile(hFile,Buffer,size, &cbWritten,(LPOVERLAPPED) NULL); 
if (!fResult){
sprintf(empery,"Error = MailSlot %d.\n", GetLastError());
MessageBox(0,empery,"Error",0);
return FALSE; 
} 
sprintf(empery,"Slot written to successfully.\n"); 
CloseHandle(hFile); 
return TRUE;
}
ReadSlot;

Code:
BOOL ReadSlot(){
DWORD cbMessage, cMessage, cbRead; 
BOOL fResult; 
BYTE Buffer[1024]; 
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("GetMailslotInfo failed with %d.\n", GetLastError()); 
return FALSE; 
} 
if (cbMessage == MAILSLOT_NO_MESSAGE){ 
return TRUE; 
} 
cAllMessages = cMessage; 
while (cMessage != 0){ 
fResult = ReadFile(hSlot,Buffer,cbMessage,&cbRead,&ov); 
if (!fResult){ 
printf("ReadFile failed with %d.\n", GetLastError()); 
return FALSE; 
} 
fResult = GetMailslotInfo( hSlot,(LPDWORD) NULL,&cbMessage,&cMessage,(LPDWORD) NULL); 
if (!fResult){ 
printf("GetMailslotInfo failed with %d.\n", GetLastError()); 
return FALSE; 
} 
} 
CloseHandle(hEvent);
return TRUE; 
}
Create Slot;

Code:
BOOL WINAPI MakeSlot(LPTSTR SlotName){ 
hSlot = CreateMailslot(SlotName,0,MAILSLOT_WAIT_FOREVER,(LPSECURITY_ATTRIBUTES) NULL);  
if (hSlot == INVALID_HANDLE_VALUE){
printf("CreateMailslot failed with %d\n", GetLastError());
return FALSE; 
}
Regards...
Thanks for sharing
Good job if you wrote this
Quote Originally Posted by Margherita View Post
Thanks for sharing
Good job if you wrote this
thanx mate
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?