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);
 
    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:
 
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 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 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2014
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