`

Windows - communication with Mailslots

阅读更多

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

 

 

 

分享到:
评论

相关推荐

    window编程从入门到精通全部经典书籍

    Up to date information on network programming, including NetBIOS, mailslots and pipes, and of course the ever important windows sockets, complete with winsock2 and raw sockets. Also contains specific ...

    深入研究Windows内部原理系列之十二:网络协议的构成和实现

    Windows提供了丰富的网络API集,这些API不仅包括前面提到的Winsock、MailSlots等,还包括一些用于高级网络功能的API,如WFP(Windows Filtering Platform)等。 #### TechNet TechNet是微软提供的一项服务,旨在...

    Mailslots即时聊天

    【Mailslots即时聊天】是一种基于Java技术开发的即时通信软件,它提供了类似QQ和MSN的聊天体验。这个软件的核心特点是实现实时的消息传递、用户组管理和更多其他互动功能。通过分析给定的文件名,我们可以推测这个...

    windows 进程间通讯大全

    在Windows操作系统中,进程间通信(IPC, Inter-Process Communication)是实现不同进程之间数据交换的关键技术。本文将详细介绍Windows平台下的多种进程间通信方式,并分析每种方式的特点、应用场景及其实现机制。 ...

    windows下进程间通信总结

    为了方便应用程序间的数据共享和交换,Microsoft Win32 API 提供了大量的机制来支持这些活动,即进程间通信 (Inter-Process Communication, IPC)。 #### 二、进程间通信的方法 在Windows中实现进程间通信的主要...

    WINDOWS网络编程技术1-5pdf part1

    此外,可能还会涵盖Windows特定的网络服务,如命名管道和MailSlots,这些都是Windows系统特有的通信机制。 文件名为001.PDF至005.PDF,暗示了这些PDF文件可能是按照章节顺序编排的,从基础到进阶,逐步展开Windows...

    VC编程-UNIX与Windows移植.doc

    Windows还提供了更多的通信机制,如消息队列、共享内存和Mailslots。 **套接字和网络** 虽然套接字在两个系统中都是网络通信的标准接口,但具体的API和行为略有差异,例如,socket()、bind()、connect()和listen()...

    Windows进程间通信开篇

    在Windows操作系统中,进程间通信(Inter-Process Communication, IPC)是多个独立运行的进程之间交换数据、协调工作的重要机制。本篇文章将深入探讨Windows进程间通信的基本概念、常用技术以及"kaipian"源码可能...

    windows 网络编程(中文版)

    此外,Windows操作系统提供了许多特有的网络功能,如命名管道、MailSlots和Windows消息队列,这些机制可以在同一台机器或网络内的不同进程间进行通信。书中会解释这些机制的工作原理以及如何在实际项目中应用。 ...

    windows应用程序捆绑内核编程实例第四章

    在Windows操作系统中,开发应用程序时有时需要实现进程间通信(IPC,Inter-Process Communication),以实现不同进程间的协作。邮槽、管道和套接字(socket)是Windows IPC中的三种重要机制,它们各自有其特点和适用...

    Windows 网络编程

    10. **高级网络服务**:除了基本的套接字操作,Windows还提供了高级网络服务,如命名管道、Mailslots和NetBIOS,这些服务在特定场景下能提供更高效的通信方式。 以上知识点是Windows网络编程的基础,实际开发中还...

    windows网络编程技术

    重定向器是邮槽(Mailslots)和命名管道(Named Pipes)的基础,这两种通信机制允许Windows机器之间的广播和单向数据通信,以及建立双向通道,支持Windows的安全通信。 #### 三、Winsock编程:现代网络编程的标准 ...

    VC进程间通信方法-- 文档

    进程间通信(IPC, Inter-Process Communication)是计算机科学中一个重要的概念,特别是在多任务和分布式系统中。在VC++环境下,实现进程间通信的方法多种多样,每种方法都有其适用的场景和优缺点。本文将详细介绍...

    进程间通信-邮槽

    进程间通信(IPC,Inter-Process Communication)是操作系统中一种重要的技术,允许不同的进程之间共享数据和信息。在Windows操作系统中,提供了多种实现IPC的方法,其中包括邮槽(Mailslot)。邮槽是一种简单且高效...

    MFC教程lesson 17-进程间通信.rar

    本教程聚焦于MFC中的一个重要概念——进程间通信(Inter-Process Communication,IPC)。进程间通信允许不同的进程之间交换数据,共享资源,协同工作,是Windows编程中的关键技能。Lesson 17的MFC教程将深入探讨这一...

Global site tag (gtag.js) - Google Analytics