Search code examples
c++windowsreadfile

Windows Readfile return ERROR_INVALID_PARAMETER 87


My program creates a mailslot, but when I call ReadFile I get ERROR_INVALID_PARAMETER.

What about the parameter is wrong?

program code:

SECURITY_DESCRIPTOR sd;
    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(&sd, true, NULL, false);

    SECURITY_ATTRIBUTES sa;
    sa.lpSecurityDescriptor=&sd;
    sa.bInheritHandle=true;

    msg_id_ = CreateMailslot( msg_key_
                                , 0
                                , MAILSLOT_WAIT_FOREVER
                                , &sa
                                );

msg_id_ is not INVALID_HANDLE_VALUE so this function succeeded. sa should allow both GENERIC_WRITE and GENERIC_READ.

receiver code:

msg_id_ = CreateFile( msg_key_
                            , GENERIC_WRITE | GENERIC_READ
                            , FILE_SHARE_WRITE | FILE_SHARE_READ
                            , 0
                            , OPEN_EXISTING
                            , FILE_FLAG_OVERLAPPED
                            , 0
                            );
ReadFile( msg_id_, (void*)msg, 1024, &byteLen, &ovRead);

The Readfile fails with error code ERROR_INVALID_PARAMETER.


Solution

  • You're trying to use the mailslot the wrong way around.

    The mailslot server handle, created by calling CreateMailslot, is for receiving messages.

    The mailslot client handle, created by calling CreateFile, is for sending messages.

    You cannot call ReadFile with the client handle. Mailslots are not bidirectional.