Windows inter-process communication

  Microsoft Win32 application programming interface (Application Programming Interface, API) provides a lot of support between the application and data sharing mechanisms for the exchange of the exercise of these activities called interprocess communication mechanisms (InterProcess Communication, IPC), refers to the process of communication between different processes data sharing and data exchange . Way inter-process communication are:

1, file mapping

  File Mapping (Memory-Mapped Files) can process the contents of the file as a memory address range as the process to be treated. Thus, the process does not use the file I / O operations, the pointer operation can be simply read and modify the contents of the file.
  Win32 API allows multiple processes to access the same file mapping object, each process receives a pointer to memory in its own address space. By using these pointers, different processes can read or modify the contents of the file, enables shared data in the file.
Application There are three ways to enable multiple processes to share a file mapping object:
  (1) Inheritance: The first process established file mapping object, its children inherit a handle to the object.
  (2) Name the file mapping: the first process can assign a name to the object when creating the file mapping object (the file name may differ). The second process can open the file mapping object by name. In addition, the first process can also put their names passed to the second process through some other IPC mechanisms (named pipes, mail slots, etc.).
  (3) handle copy: The first process established file-mapping object, and then pass through other IPC mechanisms (named pipes, mail slots, etc.) to the second object handle process. The second process duplicates the handle to get access to the file-mapping object.
  File mapping is a very effective method of sharing data between multiple processes, there is better security. However, file mapping can only be used between processes on the local machine can not be used in the network , and the developer also must control the synchronization between processes .

Step 1.1 file mapping

Have created the file mapping:
  1) Open the file CreateFile
  2) to create a file mapping object the CreateFileMapping
  3) create a map view, the map file to memory MapViewOfFile  

Create a file mapping file does not realize the process of data sharing communication:

Process 1:

  1) Create a file mapping object CreateFileMapping

  2) Create a map view, the map file to memory MapViewOfFile

Process 2:

  1) Open the file mapping object OpenFileMapping

  2) Create a map view, the map file to memory MapViewOfFile

1.2 clean-up steps

  1) Unmap view the UnmapViewOfFile
  2) Close CloseHandle map object
  . 3) Close file CloseHandle

2, shared memory

  Win32 API shared memory (Shared Memory) is actually a special case of file mapping. Process when creating a file mapping object with 0xFFFFFFFF instead of the file handle (HANDLE), it means that the corresponding file mapping object is accessed from the operating system page file memory, other processes to open the file mapping object can access the memory block. Since the memory is shared with the file mapping implementation, so it has a good safety, but also can run between processes on the same computer . IPC communication is the fastest means of communication. Synchronization issues need to be considered.

2.1 shared memory step

  1) memory mapping API function calls CreateFileMapping create a shared memory identified by name in the server process;

  2) After you create the file-mapping object, the server process calls MapViewOfFile function mapped to a process's address space;

  3) The client processes to access the shared memory object, you need to call OpenFileMapping memory function by object name, in order to obtain a shared memory object handles;

  4) If the client process to get a handle successfully shared memory object, then call the function to map MapViewOfFile object view. The user can view the object used for data read and write operations, to achieve the purpose of data communication;

  5) When the end user processes use shared memory, call the function to cancel UnmapViewOfFile view in its address space

3, anonymous pipes

  Pipe (Pipe) is a communication channel having two ends: one end of the process can handle and the other end has a handle-process communication. Conduit may be unidirectional - end is read-only, write only the other endpoint; may be bidirectional - both end points of the pipe readable and writable.
  Anonymous pipe (Anonymous Pipe) is  between the parent and child processes, with or without the name of the one-way pipeline to transfer data between two sub-processes of the same parent process . Usually created by a parent process piping, and then inherited by the child process to the channel of communication endpoint handle to read or write endpoint handle, and then communicate. The parent process can also create two or more anonymous pipe inherit read and write handles child process. These sub-processes can communicate directly using a pipe, not required by the parent process.
  Anonymous pipe is an effective way to achieve sub-process standard I / O redirection on a single machine, it can not use the Internet, can not be used between two unrelated processes .

To create a pipeline of 3.1 anonymous

  1) Create a security attribute descriptors, inheritable handle
  2) to create two pipes CreatePipe.
  3) redirect the output, write and read sub-process child process to redirect
  4) create a child process CreateProcess
  5) read \ write data to the child process WriteFile

  Related structures involved are: STARTUPINFO, PROCESS_INFORMATION and SECURITY_ATTRIBUTES.

4, Named Pipes

  命名管道(Named Pipe)是服务器进程和一个或多个客户进程之间通信的单向或双向管道。不同于匿名管道的是命名管道可以在不相关的进程之间和不同计算机之间使用,服务器建立命名管道时给它指定一个名字,任何进程都可以通过该名字打开管道的另一端,根据给定的权限和服务器进程通信。
  命名管道提供了相对简单的编程接口,使通过网络传输数据并不比同一计算机上两进程之间通信更困难,最好不要同时和多个进程通信

4.1 步骤

服务端流程:

  1)创建命名管道:CreateNamedPipe

  2)等待客户端连接:ConnectNamedPipe

  3)读取客户端请求数据:ReadFile

  4)向客户端回复数据:WriteFile

  5)关闭连接:DisconnectNamedPipe

  6)关闭管道:CloseHandle

客户端流程:

  1)打开命名管道:CreateFile

  2)等待服务端响应:WaitNamedPipe

  3)切换管道为读模式:SetNamedPipeHandleState

  4)向服务端发数据:WriteFile

  5)读服务端返回的数据:ReadFile

  6)关闭管道:CloseHandle

5、邮件槽

  邮件槽(Mailslots)提供进程间单向通信能力,任何进程都能建立邮件槽成为邮件槽服务器。其它进程,称为邮件槽客户,可以通过邮件槽的名字给邮件槽服务器进程发送消息。进来的消息一直放在邮件槽中,直到服务器进程读取它为止。一个进程既可以是邮件槽服务器也可以是邮件槽客户,因此可建立多个邮件槽实现进程间的双向通信。
  通过邮件槽可以给本地计算机上的邮件槽、其它计算机上的邮件槽或指定网络区域中所有计算机上有同样名字的邮件槽发送消息。广播通信的消息长度不能超过400字节,非广播消息的长度则受邮件槽服务器指定的最大消息长度的限制。
  邮件槽与命名管道相似,不过它传输数据是通过不可靠的数据报(如TCP/IP协议中的UDP包)完成的,一旦网络发生错误则无法保证消息正确地接收,而命名管道传输数据则是建立在可靠连接基础上的。不过邮件槽有简化的编程接口和给指定网络区域内的所有计算机广播消息的能力,所以邮件槽不失为应用程序发送和接收消息的另一种选择。

5.1 步骤

服务端流程:

  1)创建CreateMailslot

  2)进行读写操作ReadFile

  3)关闭句柄CloseHandle

客户端流程:

  1)同服务端

6、剪贴板

  剪贴板(Clipped Board)实质是Win32 API中一组用来传输数据的函数和消息,为Windows应用程序之间进行数据共享提供了一个中介,Windows已建立的剪切(复制)-粘贴的机制为不同应用程序之间共享不同格式数据提供了一条捷径。当用户在应用程序中执行剪切或复制操作时,应用程序把选取的数据用一种或多种格式放在剪贴板上。然后任何其它应用程序都可以从剪贴板上拾取数据,从给定格式中选择适合自己的格式。
  剪贴板是一个非常松散的交换媒介,可以支持任何数据格式,每一格式由一无符号整数标识,对标准(预定义)剪贴板格式,该值是Win32 API定义的常量;对非标准格式可以使用Register Clipboard Format函数注册为新的剪贴板格式。利用剪贴板进行交换的数据只需在数据格式上一致或都可以转化为某种格式就行。但剪贴板只能在基于Windows的程序中使用,不能在网络上使用

6.1 步骤

1)设置数据

  打开剪贴板OpenClipboard

  分配空间GlobalAlloc

  获取首地址并加锁GlobalLock

  解锁GlobalUnlock

  清空剪贴板EmptyClipboard

  将数据放到剪贴板SetClipboardData

  关闭剪贴板CloseClipboard

2)读取数据

  打开剪贴板OpenClipboard

  判断剪贴板的值IsClipboardFormatAvailable

  获取数据GetClipboardData,格式为CF_TEXT

  获取首地址并加锁GlobalLock

  解锁GlobalUnlock

7、动态数据交换

  动态数据交换(DDE:dynamic data exchange)是使用共享内存在应用程序之间进行数据交换的一种进程间通信形式。应用程序可以使用DDE进行一次性数据传输,也可以当出现新数据时,通过发送更新值在应用程序间动态交换数据。
  DDE和剪贴板一样既支持标准数据格式(如文本、位图等),又可以支持自己定义的数据格式。但它们的数据传输机制却不同,一个明显区别是剪贴板操作几乎总是用作对用户指定操作的一次性应答-如从菜单中选择Paste命令。尽管DDE也可以由用户启动,但它继续发挥作用一般不必用户进一步干预。DDE有三种数据交换方式:
  (1) 冷链:数据交换是一次性数据传输,与剪贴板相同。
  (2) 温链:当数据交换时服务器通知客户,然后客户必须请求新的数据。
  (3) 热链:当数据交换时服务器自动给客户发送数据。
  DDE交换可以发生在单机或网络中不同计算机的应用程序之间。开发者还可以定义定制的DDE数据格式进行应用程序之间特别目的IPC,它们有更紧密耦合的通信要求。大多数基于Windows的应用程序都支持DDE。

8、对象连接与嵌入

  应用程序利用对象连接与嵌入(OLE)技术管理复合文档(由多种数据格式组成的文档),OLE提供使某些应用程序更容易调用其它应用程序进行数据编辑的服务。例如,OLE支持的字处理器可以嵌套电子表格,当用户要编辑电子表格时OLE库可自动启动电子表格编辑器。当用户退出电子表格编辑器时,该表格已在原始字处理器文档中得到更新。在这里电子表格编辑器变成了字处理器的扩展,而如果使用DDE,用户要显式地启动电子表格编辑器。同DDE技术相同,大多数基于Windows的应用程序都支持OLE技术。

 9、动态链接库

  Win32动态连接库(DLL)中的全局数据可以被调用DLL的所有进程共享,这就又给进程间通信开辟了一条新的途径,当然访问时要注意同步问题。虽然可以通过DLL进行进程间数据共享,但从数据安全的角度考虑,我们并不提倡这种方法,使用带有访问权限控制的共享内存的方法更好一些。

10、远程过程调用

 

  Win32 API提供的远程过程调用(RPC)使应用程序可以使用远程调用函数,这使在网络上用RPC进行进程通信就像函数调用那样简单。RPC既可以在单机不同进程间使用也可以在网络中使用
  由于Win32 API提供的RPC服从OSF-DCE(Open Software Foundation Distributed Computing Environment)标准。所以通过Win32 API编写的RPC应用程序能与其它操作系统上支持DEC的RPC应用程序通信。使用RPC开发者可以建立高性能、紧密耦合的分布式应用程序。

11、NetBios函数 

  Win32 API提供NetBios函数用于处理低级网络控制,这主要是为IBM NetBios系统编写与Windows的接口。除非那些有特殊低级网络功能要求的应用程序,其它应用程序最好不要使用NetBios函数来进行进程间通信。

12、Sockets

  Windows Sockets规范是以U.C.Berkeley大学BSD UNIX中流行的Socket接口为范例定义的一套Windows下的网络编程接口。除了Berkeley Socket原有的库函数以外,还扩展了一组针对Windows的函数,使程序员可以充分利用Windows的消息机制进行编程。
  现在通过Sockets实现进程通信的网络应用越来越多,这主要的原因是Sockets的跨平台性要比其它IPC机制好得多,另外WinSock 2.0不仅支持TCP/IP协议,而且还支持其它协议(如IPX)。Sockets的唯一缺点是它支持的是底层通信操作,这使得在单机的进程间进行简单数据传递不太方便,这时使用下面将介绍的WM_COPYDATA消息将更合适些。

12.1 步骤

服务端:

  建立Socket
  声明自身的端口号和地址并绑定到Socket
  使用Listen打开监听,然后不断用Accept去查看是否有连接,如果有,捕获Socket,并通过Recv获取消息的内容
  通信完成后调用CloseSocket关闭这个对应Accept到的Socket,如果不再需要等待任何客户端连接,那么用CloseSocket关闭掉自身的Socket 
客户端:
  建立Socket
  通过端口号和地址确定目标服务器,使用Connect连接到服务器
  send发送消息,等待处理
  通信完成后调用CloseSocket关闭Socket

13、WM_COPYDATA消息

  WM_COPYDATA是一种非常强大却鲜为人知的消息。当一个应用向另一个应用传送数据时,发送方只需使用调用SendMessage函数,参数是目的窗口的句柄、传递数据的起始地址、WM_COPYDATA消息。接收方只需像处理其它消息那样处理WM_COPY DATA消息,这样收发双方就实现了数据共享。
  WM_COPYDATA是一种非常简单的方法,它在底层实际上是通过文件映射来实现的。它的缺点是灵活性不高,并且它只能用于Windows平台的单机环境下。 

根据应用程序是在网络环境下还是在单机环境下工作选择不同的通信方式。

Guess you like

Origin www.cnblogs.com/Sheenagh/p/12222797.html