我想设置一个邮筒,我可以写入和读取。C读取文件错误87当试图从邮筒读取
但调用的ReadFile时,我得到错误87.我已经从MSDN尝试不同的approaces和我仍然得到错误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);
}
你'mailslotRead'被定义为接受1个参数'INT mailslotRead(HANDLE邮箱)'但你传递3个参数' mailslotRead(hMailslot,W,I)'。 – rullof
从'GetMailslotInfo'返回的'cMessage'的值是什么? –
你的演员让我感到不安。你为什么不想让编译器帮助你? –