我正在尝试设置一个可以写入和读取的邮槽。
但是当调用 ReadFile 时,我得到错误 87。我尝试了 MSDN 的不同方法,但仍然得到错误 87。
我已经在我的代码中删除了很多错误处理,以便稍微缩短它。
这是我从主要做的电话。
hMailslot= mailslotCreate("\\\\.\\mailslot\\myslot"); //works
hMailslot=mailslotConnect("\\\\.\\mailslot\\myslot"); //works
mailslotWrite(hMailslot,w, lstrlen(w)+1)*sizeof(CHAR); //works
mailslotRead(hMailslot); //Error 87 invalid parameter
mailslotClose(hMailslot); //?
这是我的代码的缩短版本。
#define TIME_OUT MAILSLOT_WAIT_FOREVER
HANDLE mailslotCreate (char *name) {
HANDLE H = (HANDLE)CreateMailslot(name,0,TIME_OUT,(LPSECURITY_ATTRIBUTES) NULL);
return H;
}
HANDLE mailslotConnect (char * name) {
HANDLE H = CreateFile(name,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
return H;
}
int mailslotWrite(HANDLE mailSlot,void *msg,int msgSize) {
DWORD cbWritten;
WriteFile(mailSlot, msg, msgSize, &cbWritten, (LPOVERLAPPED) NULL);
return cbWritten;
}
int mailslotRead (HANDLE mailbox) {
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPTSTR Message;
fResult =GetMailslotInfo(mailbox,(LPDWORD) NULL,&cbMessage,&cMessage,(LPDWORD)NULL);
if (!fResult) //Works
{
printf("GetMailslotInfo failed with %d.\n", GetLastError());
}
Message = (LPTSTR) calloc(cbMessage,sizeof(char));
Message[0] = '\0';
fResult = ReadFile((HANDLE)mailbox,(LPVOID)Message,(DWORD)cbMessage,LPDWORD)&cbRead,(LPOVERLAPPED) NULL);
if (!fResult) //Error 87
{
printf("ReadFile failed with %d.\n", GetLastError());
free(Message);
return 0;
}
return cbRead;
}
int mailslotClose(HANDLE mailSlot){
return CloseHandle(mailSlot);
}