- 浏览: 400866 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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.
发表评论
-
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2576I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10953In this post, .net platform h ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
C - macros use technique - do while(0);
2012-12-19 07:41 765it is not common when you see a ... -
tools - gpupdate to update the group policy settings
2012-11-20 17:23 491you can update your local group ... -
Exceed to remote connect to Linux box
2012-11-13 17:58 364in this post, I will introduce ... -
Cmd - Remove directories on Windows
2012-10-31 11:18 816on windows, there is no rm -rf ... -
windows - create symbolic/hard links
2012-09-19 14:30 740If you are working on the windo ... -
windows - Maximum Path Length Limitation
2012-09-11 11:13 1232Maximum Path Length Limitat ... -
DPI and Device-Independent Pixels
2012-08-03 17:57 1428This is a repost of the dev cen ... -
Windows Print Metric/Unit Systems in System.Printing and System.Drawing.Priting
2012-08-03 15:59 1688There are two worlds in the win ... -
Windows Desktop Document and Printing, and the Printing System
2012-08-03 15:26 1268This blog is mainly a entry/int ...
相关推荐
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提供了丰富的网络API集,这些API不仅包括前面提到的Winsock、MailSlots等,还包括一些用于高级网络功能的API,如WFP(Windows Filtering Platform)等。 #### TechNet TechNet是微软提供的一项服务,旨在...
【Mailslots即时聊天】是一种基于Java技术开发的即时通信软件,它提供了类似QQ和MSN的聊天体验。这个软件的核心特点是实现实时的消息传递、用户组管理和更多其他互动功能。通过分析给定的文件名,我们可以推测这个...
在Windows操作系统中,进程间通信(IPC, Inter-Process Communication)是实现不同进程之间数据交换的关键技术。本文将详细介绍Windows平台下的多种进程间通信方式,并分析每种方式的特点、应用场景及其实现机制。 ...
为了方便应用程序间的数据共享和交换,Microsoft Win32 API 提供了大量的机制来支持这些活动,即进程间通信 (Inter-Process Communication, IPC)。 #### 二、进程间通信的方法 在Windows中实现进程间通信的主要...
此外,可能还会涵盖Windows特定的网络服务,如命名管道和MailSlots,这些都是Windows系统特有的通信机制。 文件名为001.PDF至005.PDF,暗示了这些PDF文件可能是按照章节顺序编排的,从基础到进阶,逐步展开Windows...
Windows还提供了更多的通信机制,如消息队列、共享内存和Mailslots。 **套接字和网络** 虽然套接字在两个系统中都是网络通信的标准接口,但具体的API和行为略有差异,例如,socket()、bind()、connect()和listen()...
在Windows操作系统中,进程间通信(Inter-Process Communication, IPC)是多个独立运行的进程之间交换数据、协调工作的重要机制。本篇文章将深入探讨Windows进程间通信的基本概念、常用技术以及"kaipian"源码可能...
此外,Windows操作系统提供了许多特有的网络功能,如命名管道、MailSlots和Windows消息队列,这些机制可以在同一台机器或网络内的不同进程间进行通信。书中会解释这些机制的工作原理以及如何在实际项目中应用。 ...
在Windows操作系统中,开发应用程序时有时需要实现进程间通信(IPC,Inter-Process Communication),以实现不同进程间的协作。邮槽、管道和套接字(socket)是Windows IPC中的三种重要机制,它们各自有其特点和适用...
10. **高级网络服务**:除了基本的套接字操作,Windows还提供了高级网络服务,如命名管道、Mailslots和NetBIOS,这些服务在特定场景下能提供更高效的通信方式。 以上知识点是Windows网络编程的基础,实际开发中还...
重定向器是邮槽(Mailslots)和命名管道(Named Pipes)的基础,这两种通信机制允许Windows机器之间的广播和单向数据通信,以及建立双向通道,支持Windows的安全通信。 #### 三、Winsock编程:现代网络编程的标准 ...
进程间通信(IPC, Inter-Process Communication)是计算机科学中一个重要的概念,特别是在多任务和分布式系统中。在VC++环境下,实现进程间通信的方法多种多样,每种方法都有其适用的场景和优缺点。本文将详细介绍...
进程间通信(IPC,Inter-Process Communication)是操作系统中一种重要的技术,允许不同的进程之间共享数据和信息。在Windows操作系统中,提供了多种实现IPC的方法,其中包括邮槽(Mailslot)。邮槽是一种简单且高效...
本教程聚焦于MFC中的一个重要概念——进程间通信(Inter-Process Communication,IPC)。进程间通信允许不同的进程之间交换数据,共享资源,协同工作,是Windows编程中的关键技能。Lesson 17的MFC教程将深入探讨这一...
6. **邮件槽(Mailslots)**:广播式通信,可在不同主机间交换数据,但Windows 9x仅支持客户端。 7. **Windows套接字(Windows Sockets,Winsock)**:遵循标准的网络通信协议,支持跨平台通信,功能类似于消息管道...
邮槽 (Mailslots) 邮槽类似于消息管道,但更适用于小型消息的传递。在 Windows 系统中,邮槽提供了一种简单的机制来在进程间发送和接收消息。它支持一定大小的消息(通常是几千字节),并且提供了基本的安全性和...
进程间通信(IPC, Inter-Process Communication)是在计算机科学中一个重要的概念,尤其在多任务操作系统环境下,多个进程之间需要交换数据或者同步操作时显得尤为重要。VC++作为一款广泛使用的软件开发工具,提供了...