Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
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);

    if (!result)
      wcout << "Can't get mailslot info" << endl;
    else if (NextSize == MAILSLOT_NO_MESSAGE)
      ;//wcout << "No message to show" << endl;
    else
      while (MessageCount > 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 << "Can't read message" << endl;
          delete sMsgBuffer;
          break;
        }

        MailslotCallback(sMsgBuffer);

        delete sMsgBuffer;

        result = GetMailslotInfo(hMailslot, 0, &NextSize, &MessageCount, NULL);
        if (!result)
        {
          wcout << "Can't get mailslot info" << endl;
          break;
        }
      }

    Sleep(2000);
  }
}

return 0;


Transmitter code:
C++
void MailslotSend(wchar_t* sMailslotName, void* pData, size_t ndataSize)
{
	HANDLE hFile; 
	
	hFile = CreateFile(sMailslotName,
		GENERIC_WRITE,
		FILE_SHARE_READ,
		0,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	if (hFile == INVALID_HANDLE_VALUE) 
	{
		wcout << "Can't begin sending data - error " << GetLastError() << endl;
		return;
	}

	DWORD NumberOfBytesWritten;
	OVERLAPPED ol;
	
	auto fResult = WriteFile(hFile, pData, ndataSize, &NumberOfBytesWritten, &ol);
    if (!fResult)
		wcout << "Data send failed - error " << GetLastError() << endl;
	else
		wcout << "Data send ok" << endl;

	CloseHandle(hFile);
}

int _tmain(int argc, _TCHAR* argv[])
{
	wchar_t msg[64] = {0};

	wcout << "Enter message: ";
	wcin >> msg;

	while ( lstrcmp(L"", msg) != 0 )
	{
		// sending

		MailslotSend(L"\\\\.\\mailslot\\MySendRecv", msg, 64*sizeof(wchar_t) );

		wcout << "Enter message: ";
		ZeroMemory(msg, 64*sizeof(wchar_t) );
		wcin >> msg;
	}

	return 0;
}
Posted

1 solution

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.
 
Share this answer
 
Comments
YuriMaks 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900