Jump to content
Nine

A simple fast IPC based on Windows Messaging

Recommended Posts

Posted (edited)

After seeing a number of threads talking about how to exchange efficiently messages between processes (Inter Process Communication), I decided to create a framework using Windows Messages WM_COPYDATA.  What is new with this UDF you ask ?  Well it will depends how familiar you are with IPC.  One thing is sure, the simplicity of use and the fabulous speed are amazing.

This is based on a Clients-Server approach.  You can have an unlimited number of clients talking with a single server.  You will have to define the protocol of communication between them, but the code you have to create is incredibly low. The UDF proposes 2 simple message properties of communication.  The first (called data) is based on a number.  You can decide what value 1,2,3, etc. means between your client and server.  Server will react upon the value of the data field.  Second, there is a string field where you can inscribe additional information on request, and where the server will respond to client request (if necessary).

Here are the functions that I have wrapped around this :

Func _WCD_CreateServer 
Func _WCD_CreateClient 
Func _WCD_GetServerHandle 
Func _WCD_IsServerAvailable 
Func _WCD_Send
Func _WCD_WM_COPYDATA_CLIENT_HANDLER
Func _WCD_Client_GetResponse 
Func _WCD_WM_COPYDATA_SERVER_HANDLER
Func _WCD_Server_PeekRequest 
Func _WCD_Server_GetRequest 
Func _WCD_Server_IsRequestAvail 

 

Here an example of the server :

#include <Constants.au3>
#include <GUIConstants.au3>
#include "WCD_IPC.au3"

Opt ("MustDeclareVars", 1)

$_WCD_Verbose = False ; make it True if you want to follow the convos.  False is by default.

Local $hServer = _WCD_CreateServer ()
Local $aReq, $iData

While True
  If _WCD_Server_IsRequestAvail () Then
    $aReq = _WCD_Server_GetRequest ()
    $iData = @extended
    Switch $iData
      Case 1  ; who are you
        _WCD_Send($hServer, $aReq[0], $iData, @ComputerName)
      Case 2
        Switch Number($aReq[1])
          Case 1
            _WCD_Send($hServer, $aReq[0], $iData, @IPAddress1)
          Case 2
            _WCD_Send($hServer, $aReq[0], $iData, @IPAddress2)
          Case 3
            _WCD_Send($hServer, $aReq[0], $iData, @IPAddress3)
          Case 4
            _WCD_Send($hServer, $aReq[0], $iData, @IPAddress4)
          Case Else
            _WCD_Send($hServer, $aReq[0], $iData, "Invalid parameter")
        EndSwitch
    EndSwitch
  EndIf
  Sleep (100)
WEnd

And the client :

#include <Constants.au3>
#include <GUIConstants.au3>
#include "WCD_IPC.au3"

Opt ("MustDeclareVars", 1)

$_WCD_Verbose = True ; as for the server, you can decide to make client verbose or not
Global $hWnd = _WCD_CreateClient ("Test WCD Client")
Global $hWndServer = _WCD_GetServerHandle ()

_WCD_Send($hWnd, $hWndServer, 1) ; simple request - who are you ?
Local $sString = WaitForResponse ()
ConsoleWrite ($sString & @CRLF)
_WCD_Send($hWnd, $hWndServer, 2, "5") ; adding text to a more complex request
$sString = WaitForResponse ()
ConsoleWrite ($sString & @CRLF)

Func WaitForResponse ()
  Local $sResp
  While _WCD_IsServerAvailable ()
    $sResp = _WCD_Client_GetResponse ()
    If $sResp <> "" Then Return $sResp
    Sleep (100)
  WEnd
EndFunc

As always, let me know if you got issues, comments, suggestions.  I will be glad to answer.

Version 2020-06-27

* Allows processes with different levels of privilege to communicate with each other

 

 

WCD_IPC.au3

Edited by Nine

Share this post


Link to post
Share on other sites
Posted (edited)

... I suppose by placing a #RequireAdmin on one of the scripts (?)

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Added to the wiki :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-07-21 - Version 1.5.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...