Windows - communication with Mailslots

1 篇文章 0 订阅

Mailslot is some mechanism on Windows that you can compare against the Linux implemenation such as Named Pipe or windows Socket. 

 

If you are looking for an introduction on the Windows Mailslot, you can see "About Mailslots (windows)" from MSDN.

 

Generally with the mail slots , you can write/read through some file handle like way, and you have to first create a mailslots so that participant can read/communicate with each other.

 

It has been said in the MSDN blog that "Mailslots can broadcast messages within a domain." while this parts is not covered in this post. 

 

I will followed the steps as in the MSDN to tell how to create/write/read the Mailslot. 

 

Create Mailslots

See this page for details 

 

 

HANDLE hSlot;
LPTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");



// to use the std namespace


// use the following namespace 
//#include <Windows.h>
//#include <stdio.h>

//
///**
//  - create a named mail slot
//*/

BOOL WINAPI MakeSlot(LPTSTR lpszSlotName)
{
  hSlot = CreateMailslot(lpszSlotName, 
    0,                        // no maxinum mesage size
    MAILSLOT_WAIT_FOREVER,                         // no time-out for operation
    (LPSECURITY_ATTRIBUTES)NULL);                        // no maxinum mesage size
  if (hSlot == INVALID_HANDLE_VALUE) {
    printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE; 
  }

  else printf("MailSlot created succesfully\n");
  return TRUE;
}

 

and you will use the following header files

 

 

#include <Windows.h>
#include <stdio.h>

// you will need  the following header file to use the TCHAR and StringCchPrintf
#include <strsafe.h>
#include <tchar.h>

Write to Mailslots

See this page for details.

 

 

LPTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");

BOOL WriteSlot(HANDLE hSlot, LPTSTR lpszMessage)
{
   BOOL fResult; 
   DWORD cbWritten; 
 
   fResult = WriteFile(hSlot, 
     lpszMessage, 
     (DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR),  
     &cbWritten, 
     (LPOVERLAPPED) NULL); 
 
   if (!fResult) 
   { 
      printf("WriteFile failed with %d.\n", GetLastError()); 
      return FALSE; 
   } 
 
   printf("Slot written to successfully.\n"); 

   return TRUE;
}

int main()
{ 
   HANDLE hFile; 

   hFile = CreateFile(SlotName, 
     GENERIC_WRITE, 
     FILE_SHARE_READ,
     (LPSECURITY_ATTRIBUTES) NULL, 
     OPEN_EXISTING, 
     FILE_ATTRIBUTE_NORMAL, 
     (HANDLE) NULL); 
 
   if (hFile == INVALID_HANDLE_VALUE) 
   { 
      printf("CreateFile failed with %d.\n", GetLastError()); 
      return FALSE; 
   } 
 
   WriteSlot(hFile, TEXT("Message one for mailslot."));
   WriteSlot(hFile, TEXT("Message two for mailslot."));

   Sleep(5000);

   WriteSlot(hFile, TEXT("Message three for mailslot."));
 
   CloseHandle(hFile); 
 
   return TRUE;
}
 

you will need to use the following header files. 

 

#include <Windows.h>
#include <stdio.h>

// you will need  the following header file to use the TCHAR and StringCchPrintf
#include <strsafe.h>
#include <tchar.h>
 

Read from Mailslots.

For reading , you might want to start this page. 

 

 

/**
   - create a named mail slot
*/

BOOL WINAPI MakeSlot(LPTSTR lpszSlotName)
{
  hSlot = CreateMailslot(lpszSlotName, 
    0,                        // no maxinum mesage size
    MAILSLOT_WAIT_FOREVER,                         // no time-out for operation
    (LPSECURITY_ATTRIBUTES)NULL);                        // no maxinum mesage size
  if (hSlot == INVALID_HANDLE_VALUE) {
    printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE; 
  }

  else printf("MailSlot created succesfully\n");
  return TRUE;
}

/** 
 - read from the mailslots
*/
BOOL ReadSlot()
{
  DWORD cbMessage, cMessage, cbRead;

  BOOL fResult;
  LPTSTR lpszBuffer;
  TCHAR achID[80];
  TCHAR cAllMessages;
  HANDLE hEvent;
  OVERLAPPED ov;

  cbMessage = cMessage = cbRead = 0;

  hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
  if (NULL == hEvent) { 
    return FALSE;
  }

  ov.Offset = 0;
  ov.OffsetHigh = 0;
  ov.hEvent = hEvent;

  fResult = GetMailslotInfo(hSlot, 
    (LPDWORD) NULL, // no maxinum message size, 
    &cbMessage, // size of the next message
    &cMessage, // number of message
    (LPDWORD) NULL); // no read time OUT

  if (!fResult) {
    printf("GetMailslotInfo failed with %d.\n", GetLastError());
    return FALSE;
  }

  if (cbMessage == MAILSLOT_NO_MESSAGE) { 
     printf("Wait for a message...\n");
     return TRUE;
  }

  cAllMessages = cMessage;

  while (cMessage != 0) { 
    // create a message-number string
    StringCchPrintf((LPTSTR) achID, 
      80, 
      TEXT("\nMessage #%d of %d\n"), 
      cAllMessages - cMessage  + 1,
      cAllMessages);

    // allocate memory for the message
    lpszBuffer = (LPTSTR) GlobalAlloc(GPTR,
      lstrlen((LPTSTR) achID) * sizeof (TCHAR) + cbMessage);

    if (NULL == lpszBuffer) { 
      return FALSE;
    }
    lpszBuffer[0] = '\0';

    fResult = ReadFile(hSlot,
      lpszBuffer,
      cbMessage,
      &cbRead,
      &ov);

    if (!fResult) { 
      printf ("ReadFile failed with %d.\n", GetLastError());
      GlobalFree((HGLOBAL) lpszBuffer);
      return FALSE;
    }

    // concatenate the message and the message-number string
    StringCbCat(lpszBuffer,
      lstrlen((LPTSTR) achID)* sizeof(TCHAR) + cbMessage,
      (LPTSTR) achID);

    // display the message _t stands for the terminal
    _tprintf(TEXT("Contents of hthe mailslot:%s\n"), lpszBuffer);

    GlobalFree((HGLOBAL) lpszBuffer);

    fResult = GetMailslotInfo(hSlot,   // the mail slot handle
      (LPDWORD) NULL,                  // no maximum message size
      &cbMessage,                      // size of next message
      &cMessage,                       // number of messages
      (LPDWORD) NULL);                 // no read -timeout

    if (!fResult) { 
      printf("GetMailslotInfo failed (%d)\n", GetLastError());
      return FALSE;
    }
  }

  CloseHandle(hEvent);

  return TRUE;

}


void main()
{
   MakeSlot(SlotName);

   while(TRUE)
   {
      ReadSlot();
      Sleep(3000);
   }
}
 

 

Extended reading.

As it might be stated that you can broadcast messsage to mutiple clients, you might be tempted into the falsify impression that you can create multiple client on the same computer that listen to a mailslots to facilitate inter-process communication. 

 

Well, that is not true, by broadcasting, it means that broadcast to domains, and computer are members to domains, so there is no telling a clinet from a computer from another client on the same computer. 

 

You can find more detailed discussion on the broadcasting of the Mailslots in this post. - using mailslots

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值