我想設置一個郵筒,我可以寫入和讀取。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'的值是什麼? –
你的演員讓我感到不安。你爲什麼不想讓編譯器幫助你? –