Just a test.高手无视.
服务端:
//Code by Pnig0s1992 //Date:2012,3,19 #include <stdio.h> #include <Windows.h> VOID UseMailSlot(LPTSTR lpMailSlotName); int main(int argc,char **argv) { LPTSTR lpSlotName = TEXT("\\\\.\\mailslot\\first_slot"); UseMailSlot(lpSlotName); return 0; } VOID UseMailSlot(LPTSTR lpMailSlotName) { HANDLE hMailSlot; BOOL bResult; DWORD dwMessageSize; DWORD dwMessageCount; DWORD dwHasReadBytes; hMailSlot = CreateMailslot(lpMailSlotName,0,MAILSLOT_WAIT_FOREVER,NULL); if(hMailSlot == INVALID_HANDLE_VALUE) { printf("\nCreate mailslot:%S failed.",lpMailSlotName); return; }else { printf("\nCreate mailslot successfully."); } int iCount = 0; int index = 0; while (1) { bResult = GetMailslotInfo(hMailSlot,NULL,&dwMessageSize,&dwMessageCount,NULL); if(!bResult) { printf("\nGetMailslotInfo failed with error:%d",GetLastError()); CloseHandle(hMailSlot); return; } if(dwMessageCount == 0) { printf("\nNo.%d wait for message.",iCount+1); iCount++; Sleep(2000); continue; } while(dwMessageCount != 0) { LPTSTR lpMessageBuffer = (LPTSTR)HeapAlloc(GetProcessHeap (),HEAP_ZERO_MEMORY,dwMessageSize); if(lpMessageBuffer == NULL) { printf("\nHeapAlloc failed with error:%d",GetLastError()); CloseHandle(hMailSlot); return; } bResult = ReadFile (hMailSlot,lpMessageBuffer,dwMessageSize,&dwHasReadBytes,NULL); if(!bResult) { printf("\nReadFile failed with error:%d",GetLastError()); HeapFree(GetProcessHeap(),0,lpMessageBuffer); CloseHandle(hMailSlot); return; } printf("\nReceive No.%d message from client.\nContent:%S",index+1,lpMessageBuffer); index++; HeapFree(GetProcessHeap(),0,lpMessageBuffer); bResult = GetMailslotInfo(hMailSlot,0,&dwMessageSize,&dwMessageCount,NULL); if(!bResult) { printf("\nGetMailslotInfo failed with error:%d",GetLastError()); return; } } } return; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索printf
, return
, 槽函数
, heapfree
GetLastError()
邮槽通信、winapi directx实例、windows 邮槽、c 串口通信实例、java调用api接口实例,以便于您获取更多的相关知识。
时间: 2024-09-11 06:22:53