Question

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.

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);
}
Was it helpful?

Solution

As per Microsoft, Error 87 is ERROR_INVALID_PARAMETER from ReadFile() or WriteFile()

So something is wrong with those parameters.

Also from MS: CreateMailSlot(), CreateFile() and ReadFile() definition

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.

However I don't know enough to know if that is the actual issue

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top