简体   繁体   English

尝试从邮筒读取时出现C读取文件错误87

[英]C Readfile error 87 when trying to read from mailslot

I am trying to set up a mailslot that i can write to and read from. 我正在尝试设置一个我可以写入和读取的邮筒。

But when calling ReadFile to i get error 87. I have tried different approaces from MSDN and i still get error 87. 但是,当我调用ReadFile时出现错误87。我尝试了来自MSDN的不同方法,但仍然收到错误87。

I have removed a lot of error handling in my code in order to shorten it a bit. 为了简化代码,我在代码中删除了很多错误处理。

This are calls i do from main. 这是我主要打来的电话。

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); //?

Here is a shortened version of my code. 这是我的代码的简化版本。

#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);
}

As per Microsoft, Error 87 is ERROR_INVALID_PARAMETER from ReadFile() or WriteFile() 根据Microsoft, Error 87是ReadFile()或WriteFile()的ERROR_INVALID_PARAMETER

So something is wrong with those parameters. 因此,这些参数有问题。

Also from MS: CreateMailSlot() , CreateFile() and ReadFile() definition 同样来自MS: CreateMailSlot()CreateFile()ReadFile()定义

I've been looking at Using Mailslots and trying to compare it to what you have. 我一直在研究“ 使用邮槽”,并试图将其与您所拥有的进行比较。 About the only difference I can see is that even though they create the file with FILE_ATTRIBUTE_NORMAL , and write to it with (LPOVERLAPPED) NULL , they still supply an OVERLAPPED ov when reading the file - even though their documentation says that it is not required. 我能看到的唯一区别是,即使他们使用FILE_ATTRIBUTE_NORMAL创建文件并使用(LPOVERLAPPED) NULL写入文件,他们在读取文件时仍会提供OVERLAPPED ov即使他们的文档说它不是必需的。

However I don't know enough to know if that is the actual issue 但是我不知道这是否是实际问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM