Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win32 IPC MailSlot
Hello.

OS: WinXP SP3
IDE: MS Visual C++ 2010 Express Edition
Problem: Transmitter program can't write to mailslot. CreateFile works fine, WriteFile causes error №6 - "invalid handle". Receiver program works fine ("Process Explorer" tool shows me that mailslot exists).

Receiver code:
 
hMailslot = CreateMailslot(L"\\\\.\\mailslot\\MySendRecv", 0, 2000, NULL);
if (hMailslot == INVALID_HANDLE_VALUE)
  wcout << L"Can't start server" << endl;
else
{
  wcout << L"Server started successfully" << endl;
 
  DWORD NextSize;
  DWORD MessageCount;
  OVERLAPPED ol;
 
  while (true)
  {
    auto result = GetMailslotInfo(hMailslot, 0, &NextSize, &MessageCount, NULL);
 
<pre>
if (!result)
  wcout &lt;&lt; "Can't get mailslot info" &lt;&lt; endl;
else if (NextSize == MAILSLOT_NO_MESSAGE)
  ;//wcout &lt;&lt; "No message to show" &lt;&lt; endl;
else
  while (MessageCount &gt; 0)
  {
    wchar_t* sMsgBuffer = new wchar_t[NextSize+1];
    SecureZeroMemory(sMsgBuffer, sizeof(wchar_t)*(NextSize+1) );
 
    DWORD NumerOfBytesRead;
    result = ReadFile(hMailslot, sMsgBuffer, NextSize, &NumerOfBytesRead, &ol); 
 
    if (!result)
    {
      wcout &lt;&lt; "Can't read message" &lt;&lt; endl;
      delete sMsgBuffer;
      break;
    }
 
    MailslotCallback(sMsgBuffer);
 
    delete sMsgBuffer;
 
    result = GetMailslotInfo(hMailslot, 0, &NextSize, &MessageCount, NULL);
    if (!result)
    {
      wcout &lt;&lt; "Can't get mailslot info" &lt;&lt; endl;
      break;
    }
  }
 
Sleep(2000);

}
}

return 0;

Transmitter code:
 
void MailslotSend(wchar_t* sMailslotName, void* pData, size_t ndataSize)
{
    HANDLE hFile; 
 
<pre>
hFile = CreateFile(sMailslotName,
    GENERIC_WRITE,
    FILE_SHARE_READ,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);
 
if (hFile == INVALID_HANDLE_VALUE) 
{
    wcout &lt;&lt; "Can't begin sending data - error " &lt;&lt; GetLastError() &lt;&lt; endl;
    return;
}
 
DWORD NumberOfBytesWritten;
OVERLAPPED ol;
 
auto fResult = WriteFile(hFile, pData, ndataSize, &NumberOfBytesWritten, &ol);
if (!fResult)
    wcout &lt;&lt; "Data send failed - error " &lt;&lt; GetLastError() &lt;&lt; endl;
else
    wcout &lt;&lt; "Data send ok" &lt;&lt; endl;
 
CloseHandle(hFile);

}

int _tmain(int argc, _TCHAR* argv[])
{
wchar_t msg[64] = {0};
 
wcout &lt;&lt; "Enter message: ";
wcin &gt;&gt; msg;
 
while ( lstrcmp(L"", msg) != 0 )
{
    // sending

    MailslotSend(L"\\\\.\\mailslot\\MySendRecv", msg, 64*sizeof(wchar_t) );
 
    wcout &lt;&lt; "Enter message: ";
    ZeroMemory(msg, 64*sizeof(wchar_t) );
    wcin &gt;&gt; msg;
}
 
return 0;

}

Posted 22-May-13 1:57am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are calling WriteFile() with a pointer to an overlapped structure. This is not necessary, because the file has not been opened for overlapped I/O.

The error is sourced by passing an uninitialized overlapped structure. The invalid handle error does not apply to file handle but to the event handle member of the overlapped structure when WriteFile() tries to reset the event.
  Permalink  
Comments
YuriMaks at 22-May-13 8:25am
   
Thank you, it works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100