Shared memory variables demo

Recommended Posts

There are already a few examples of sharing variables via memory on this forum but you may like this one.

There are 2 scripts:

1. Example_Sharing_Memory_Var.au3 is the calling application (I call it the mother application)

2. Sharing_Memory_Client.au3 is the client application

The mother Application is creating the structures for the different variables to be shared across applications (could be more than 2 of course)

Once the structures are defined and assigned, the child application is launched bythe mother with command line parameters that contains the PID of the mother and the addresses of the pointers to the shared variables.

In this example the dialogue is bi-directional:

- The mother application changes background colour as requested by the child application.

- The Child application GUI will move position on request of the mother.

- Both GUIs will force shutdown of the other application by pressing the Stop button (I quit, you quit…)

- The mother application will send any text that is entered in the input field (press enter to confirm)

- The mother application can send two commands to the child application, ‘color’ and ‘quit’. Color will force a color change for both GUIs, Quit will be suicidal for the mother application.

Pre-requisite:

- Don’t change the name of the child application unless you change the name in the calling application

The script uses Nomad memory UDF, I included it in the script, so you don't have to search for it.

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.0.0
Author: GreenCan
Script Function: Sharing_Memory_Client.au3
Child application started by Example_Sharing_Memory_Var.au3
Pre-requisite:
1. Do NOT change the name of the script as it is started by Example_Sharing_Memory_Var.au3
This is the child application that is started by the mother application
The child receives the PID and the pointers so it can access the variables
Credits: Thanks to following contributors
- This source includes Memory UDF library by Nomad
#ce----------------------------------------------------------------------------; #include "memory2.au3" #include <GUIConstantsEx.au3>If$CmdLine[0]<>4ThenExit; require PID, pointer1 and pointer2; set GUI$GuiWidth=300$GuiHeight=300$Gui=GUICreate("Child Application",$GuiWidth,$GuiHeight,@DesktopWidth-$GuiWidth-100,@DesktopHeight-$GuiHeight-100)Global$PID=$CmdLine[1]Global$Pointer1=$CmdLine[2]Global$Pointer2=$CmdLine[3]Global$Pointer3=$CmdLine[4]; $Pointer1 = initial value of color passed by the mother application; the child application (this script) will also use $Pointer1 to communicate that it's stops (by setting $Pointer1 to -1 )$Color= Read_Memory($PID,$Pointer1); $Pointer2 = initial value of corner position passed by the mother application$Position= Read_Memory($PID,$Pointer2); initial value of passed char String$Char=""GuiCtrlCreateLabel("Child application",5,5,$GuiWidth-10,20)GUICtrlSetColor(-1,0xFFFFFF)GUICtrlSetFont(-1,10)Local$LabelColor=GuiCtrlCreateLabel("The Application will stop when the mother application Quits",5,30,$GuiWidth-10,50)GUICtrlSetColor($LabelColor,0xFFFFFF)GUICtrlSetFont(-1,10)Local$GUIEdit=GUICtrlCreateEdit("",5,$GuiHeight-235,$GuiWidth-10,195)Local$Stop=GUICtrlCreateButton("Stop",($GuiWidth/2)-35,$GuiHeight-30,70,22)GUISetState(); show the pointersGUICtrlSetData($GUIEdit,"Pointer address 1: "&$Pointer1&@CRLF&" address 2: "&$Pointer2&@CRLF&" address 3: "&$Pointer3&@CRLF,"append")
Color()While1$msg=GUIGetMsg()If$msg=$StopThen
Write_Memory($PID,$Pointer1,-1); communicate the exit value to the mother applicationExitEndIf; if the mother application is closed, the the value will be set to -1 and the child will close too$exitValue= Read_Memory($PID,$Pointer1,"int")If$exitValue=0Then
Quit()Else; the mother application requests the child application to change corner if the value of $Pointer2 changes$newPosition= Read_Memory($PID,$Pointer2)If$Position<>$newPositionThenGUICtrlSetData($GUIEdit,"Request from mother to change corner to: "&$newPosition&@CRLF,"append")$Position=$newPositionIf$Position=1Then; Right Lower cornerWinMove($Gui,"",@DesktopWidth-$GuiWidth-100,@DesktopHeight-$GuiHeight-100)ElseIf$Position=2Then; Left Lower cornerWinMove($Gui,"",100,@DesktopHeight-$GuiHeight-100)ElseIf$Position=3Then; Right Upper cornerWinMove($Gui,"",@DesktopWidth-$GuiWidth-100,100)EndIfEndIf; the mother application sents a character string$Char= Read_Memory($PID,$Pointer3,"char var4[128]")If$Char<>""Then
Write_Memory($PID,$Pointer3,"","char var4[128]"); empty value in memoryIfStringUpper(StringStripWS($Char,3))="QUIT"ThenGUICtrlSetData($GUIEdit,"Received command"&": "&$Char&@CRLF,"append")
Quit()ElseIfStringUpper(StringStripWS($Char,3))="COLOR"ThenGUICtrlSetData($GUIEdit,"Received command"&": "&$Char&@CRLF,"append")
Color()ElseGUICtrlSetData($GUIEdit,"Received"&": "&$Char&@CRLF,"append");TrayTip("text", $Char, 5)EndIfEndIf; change background color and sent to the mother application $randomizer=Random(1,600,1)If$randomizer=1Then Color()EndIfWEnd
#FUNCTION# ==============================================================Func Color()$Color=Random(1,30000000,1); new background colorGUISetBkColor($Color,$Gui)
Write_Memory($PID,$Pointer1,$Color,"int"); set the new value to be returned to the mother applicationGUICtrlSetData($GUIEdit,"Sent Color Change: 0x"&Hex($Color)&@CRLF,"append")EndFunc;==>Color
#FUNCTION# ==============================================================Func Quit()GUICtrlSetData($GUIEdit,"Mother application requests Shutdown"&@CRLF&@CRLF&"Shutdown within 3 seconds","append")Beep(4000,150)sleep(3000); wait 3 secs before closing; You ask me to quit but you Quit too...
Write_Memory($PID,$Pointer1,-1); communicate the exit value to the mother applicationExitEndFunc;==>Quit
#FUNCTION# ==============================================================Func Write_Memory($_PID,$_Pointer,$_Value,$_VarType="int"); This function will replace a value in memory, allocated to the calling application Local$DllHandle=_MemoryOpen($_PID); Open the memory allocated by the PID from the calling applicationLocal$Data=_MemoryWrite($_Pointer,$DllHandle,$_Value,$_VarType); Write the new value at the Pointer address $error=@Error; just check if any error writing the memoryIf$error>1Thenbeep(1000,1000); just ring the bell if unable to write_MemoryClose($DllHandle); Close the HandleEndFunc;==>Write_Memory
#FUNCTION# ==============================================================Func Read_Memory($_PID,$_Pointer,$_VarType="int"); This function will read a value in memory, allocated by the calling application ToolbarAG.exeLocal$DllHandle=_MemoryOpen($_PID); Open the memory allocated by the PID from the calling application Local$Data=_MemoryRead($_Pointer,$DllHandle,$_VarType); read value passed by the mother application$error=@Error; just check if any error reading the memory_MemoryClose($DllHandle); Close the HandleIf$error>1ThenReturn""ElseReturn$DataEndIfEndFunc;==>Read_Memory
#FUNCTION# ==============================================================#include-once#region _Memory;=================================================================================================; AutoIt Version: 3.1.127 (beta); Language: English; Platform: All Windows; Author: Nomad; Requirements: These functions will only work with beta.;=================================================================================================; Credits: wOuter - These functions are based on his original _Mem() functions. But they are; easier to comprehend and more reliable. These functions are in no way a direct copy; of his functions. His functions only provided a foundation from which these evolved.;=================================================================================================;; Functions:;;=================================================================================================; Function: _MemoryOpen($iv_Pid[, $iv_DesiredAccess[, $iv_InheritHandle]]); Description: Opens a process and enables all possible access rights to the process. The; Process ID of the process is used to specify which process to open. You must; call this function before calling _MemoryClose(), _MemoryRead(), or _MemoryWrite().; Parameter(s): $iv_Pid - The Process ID of the program you want to open.; $iv_DesiredAccess - (optional) Set to 0x1F0FFF by default, which enables all; possible access rights to the process specified by the; Process ID.; $if_InheritHandle - (optional) If this value is TRUE, all processes created by; this process will inherit the access handle. Set to TRUE; (1) by default. Set to 0 if you want it to be FALSE.; Requirement(s): A valid process ID.; Return Value(s): On Success - Returns an array containing the Dll handle and an open handle to; the specified process.; On Failure - Returns 0; @Error - 0 = No error.; 1 = Invalid $iv_Pid.; 2 = Failed to open Kernel32.dll.; 3 = Failed to open the specified process.; Author(s): Nomad; Note(s):;=================================================================================================Func_MemoryOpen($iv_Pid,$iv_DesiredAccess=0x1F0FFF,$if_InheritHandle=1)IfNotProcessExists($iv_Pid)ThenSetError(1)Return0EndIfLocal$ah_Handle[2]=[DllOpen('kernel32.dll')]If@ErrorThenSetError(2)Return0EndIfLocal$av_OpenProcess=DllCall($ah_Handle[0],'int','OpenProcess','int',$iv_DesiredAccess,'int',$if_InheritHandle,'int',$iv_Pid)If@ErrorThenDllClose($ah_Handle[0])SetError(3)Return0EndIf$ah_Handle[1]=$av_OpenProcess[0]Return$ah_HandleEndFunc;=================================================================================================; Function: _MemoryRead($iv_Address, $ah_Handle[, $sv_Type]); Description: Reads the value located in the memory address specified.; Parameter(s): $iv_Address - The memory address you want to read from. It must be in hex; format (0x00000000).; $ah_Handle - An array containing the Dll handle and the handle of the open; process as returned by _MemoryOpen().; $sv_Type - (optional) The "Type" of value you intend to read. This is set to; 'dword'(32bit(4byte) signed integer) by default. See the help file; for DllStructCreate for all types.; An example: If you want to read a word that is 15 characters in; length, you would use 'char[16]'.; Requirement(s): The $ah_Handle returned from _MemoryOpen.; Return Value(s): On Success - Returns the value located at the specified address.; On Failure - Returns 0; @Error - 0 = No error.; 1 = Invalid $ah_Handle.; 2 = $sv_Type was not a string.; 3 = $sv_Type is an unknown data type.; 4 = Failed to allocate the memory needed for the DllStructure.; 5 = Error allocating memory for $sv_Type.; 6 = Failed to read from the specified process.; Author(s): Nomad; Note(s): Values returned are in Decimal format, unless specified as a 'char' type, then; they are returned in ASCII format. Also note that size ('char[size]') for all; 'char' types should be 1 greater than the actual size.;=================================================================================================Func_MemoryRead($iv_Address,$ah_Handle,$sv_Type='dword')IfNotIsArray($ah_Handle)ThenSetError(1)Return0EndIfLocal$v_Buffer=DllStructCreate($sv_Type)If@ErrorThenSetError(@Error+1)Return0EndIfDllCall($ah_Handle[0],'int','ReadProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')IfNot@ErrorThenLocal$v_Value=DllStructGetData($v_Buffer,1)Return$v_ValueElseSetError(6)Return0EndIfEndFunc;=================================================================================================; Function: _MemoryWrite($iv_Address, $ah_Handle, $v_Data[, $sv_Type]); Description: Writes data to the specified memory address.; Parameter(s): $iv_Address - The memory address you want to write to. It must be in hex; format (0x00000000).; $ah_Handle - An array containing the Dll handle and the handle of the open; process as returned by _MemoryOpen().; $v_Data - The data to be written.; $sv_Type - (optional) The "Type" of value you intend to write. This is set to; 'dword'(32bit(4byte) signed integer) by default. See the help file; for DllStructCreate for all types.; An example: If you want to write a word that is 15 characters in; length, you would use 'char[16]'.; Requirement(s): The $ah_Handle returned from _MemoryOpen.; Return Value(s): On Success - Returns 1; On Failure - Returns 0; @Error - 0 = No error.; 1 = Invalid $ah_Handle.; 2 = $sv_Type was not a string.; 3 = $sv_Type is an unknown data type.; 4 = Failed to allocate the memory needed for the DllStructure.; 5 = Error allocating memory for $sv_Type.; 6 = $v_Data is not in the proper format to be used with the "Type"; selected for $sv_Type, or it is out of range.; 7 = Failed to write to the specified process.; Author(s): Nomad; Note(s): Values sent must be in Decimal format, unless specified as a 'char' type, then; they must be in ASCII format. Also note that size ('char[size]') for all; 'char' types should be 1 greater than the actual size.;=================================================================================================Func_MemoryWrite($iv_Address,$ah_Handle,$v_Data,$sv_Type='dword')IfNotIsArray($ah_Handle)ThenSetError(1)Return0EndIfLocal$v_Buffer=DllStructCreate($sv_Type)If@ErrorThenSetError(@Error+1)Return0ElseDllStructSetData($v_Buffer,1,$v_Data)If@ErrorThenSetError(6)Return0EndIfEndIfDllCall($ah_Handle[0],'int','WriteProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')IfNot@ErrorThenReturn1ElseSetError(7)Return0EndIfEndFunc;=================================================================================================; Function: _MemoryClose($ah_Handle); Description: Closes the process handle opened by using _MemoryOpen().; Parameter(s): $ah_Handle - An array containing the Dll handle and the handle of the open; process as returned by _MemoryOpen().; Requirement(s): The $ah_Handle returned from _MemoryOpen.; Return Value(s): On Success - Returns 1; On Failure - Returns 0; @Error - 0 = No error.; 1 = Invalid $ah_Handle.; 2 = Unable to close the process handle.; Author(s): Nomad; Note(s):;=================================================================================================Func_MemoryClose($ah_Handle)IfNotIsArray($ah_Handle)ThenSetError(1)Return0EndIfDllCall($ah_Handle[0],'int','CloseHandle','int',$ah_Handle[1])IfNot@ErrorThenDllClose($ah_Handle[0])Return1ElseDllClose($ah_Handle[0])SetError(2)Return0EndIfEndFunc;=================================================================================================; Function: _MemoryPointerRead ($iv_Address, $ah_Handle, $av_Offset[, $sv_Type]); Description: Reads a chain of pointers and returns an array containing the destination; address and the data at the address.; Parameter(s): $iv_Address - The static memory address you want to start at. It must be in; hex format (0x00000000).; $ah_Handle - An array containing the Dll handle and the handle of the open; process as returned by _MemoryOpen().; $av_Offset - An array of offsets for the pointers. Each pointer must have an; offset. If there is no offset for a pointer, enter 0 for that; array dimension.; $sv_Type - (optional) The "Type" of data you intend to read at the destination; address. This is set to 'dword'(32bit(4byte) signed integer) by; default. See the help file for DllStructCreate for all types.; Requirement(s): The $ah_Handle returned from _MemoryOpen.; Return Value(s): On Success - Returns an array containing the destination address and the value; located at the address.; On Failure - Returns 0; @Error - 0 = No error.; 1 = $av_Offset is not an array.; 2 = Invalid $ah_Handle.; 3 = $sv_Type is not a string.; 4 = $sv_Type is an unknown data type.; 5 = Failed to allocate the memory needed for the DllStructure.; 6 = Error allocating memory for $sv_Type.; 7 = Failed to read from the specified process.; Author(s): Nomad; Note(s): Values returned are in Decimal format, unless a 'char' type is selected.; Set $av_Offset like this:; $av_Offset[0] = NULL (not used); $av_Offset[1] = Offset for pointer 1 (all offsets must be in Decimal); $av_Offset[2] = Offset for pointer 2; etc...; (The number of array dimensions determines the number of pointers);=================================================================================================Func_MemoryPointerRead ($iv_Address,$ah_Handle,$av_Offset,$sv_Type='dword')IfIsArray($av_Offset)ThenIfIsArray($ah_Handle)ThenLocal$iv_PointerCount=UBound($av_Offset)-1ElseSetError(2)Return0EndIfElseSetError(1)Return0EndIfLocal$iv_Data[2],$iLocal$v_Buffer=DllStructCreate('dword')For$i=0to$iv_PointerCountIf$i=$iv_PointerCountThen$v_Buffer=DllStructCreate($sv_Type)If@ErrorThenSetError(@Error+2)Return0EndIf$iv_Address='0x'&hex($iv_Data[1]+$av_Offset[$i])DllCall($ah_Handle[0],'int','ReadProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')If@ErrorThenSetError(7)Return0EndIf$iv_Data[1]=DllStructGetData($v_Buffer,1)ElseIf$i=0ThenDllCall($ah_Handle[0],'int','ReadProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')If@ErrorThenSetError(7)Return0EndIf$iv_Data[1]=DllStructGetData($v_Buffer,1)Else$iv_Address='0x'&hex($iv_Data[1]+$av_Offset[$i])DllCall($ah_Handle[0],'int','ReadProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')If@ErrorThenSetError(7)Return0EndIf$iv_Data[1]=DllStructGetData($v_Buffer,1)EndIfNext$iv_Data[0]=$iv_AddressReturn$iv_DataEndFunc;=================================================================================================; Function: _MemoryPointerWrite ($iv_Address, $ah_Handle, $av_Offset, $v_Data[, $sv_Type]); Description: Reads a chain of pointers and writes the data to the destination address.; Parameter(s): $iv_Address - The static memory address you want to start at. It must be in; hex format (0x00000000).; $ah_Handle - An array containing the Dll handle and the handle of the open; process as returned by _MemoryOpen().; $av_Offset - An array of offsets for the pointers. Each pointer must have an; offset. If there is no offset for a pointer, enter 0 for that; array dimension.; $v_Data - The data to be written.; $sv_Type - (optional) The "Type" of data you intend to write at the destination; address. This is set to 'dword'(32bit(4byte) signed integer) by; default. See the help file for DllStructCreate for all types.; Requirement(s): The $ah_Handle returned from _MemoryOpen.; Return Value(s): On Success - Returns the destination address.; On Failure - Returns 0.; @Error - 0 = No error.; 1 = $av_Offset is not an array.; 2 = Invalid $ah_Handle.; 3 = Failed to read from the specified process.; 4 = $sv_Type is not a string.; 5 = $sv_Type is an unknown data type.; 6 = Failed to allocate the memory needed for the DllStructure.; 7 = Error allocating memory for $sv_Type.; 8 = $v_Data is not in the proper format to be used with the; "Type" selected for $sv_Type, or it is out of range.; 9 = Failed to write to the specified process.; Author(s): Nomad; Note(s): Data written is in Decimal format, unless a 'char' type is selected.; Set $av_Offset like this:; $av_Offset[0] = NULL (not used, doesn't matter what's entered); $av_Offset[1] = Offset for pointer 1 (all offsets must be in Decimal); $av_Offset[2] = Offset for pointer 2; etc...; (The number of array dimensions determines the number of pointers);=================================================================================================Func_MemoryPointerWrite ($iv_Address,$ah_Handle,$av_Offset,$v_Data,$sv_Type='dword')IfIsArray($av_Offset)ThenIfIsArray($ah_Handle)ThenLocal$iv_PointerCount=UBound($av_Offset)-1ElseSetError(2)Return0EndIfElseSetError(1)Return0EndIfLocal$iv_StructData,$iLocal$v_Buffer=DllStructCreate('dword')For$i=0to$iv_PointerCountIf$i=$iv_PointerCountThen$v_Buffer=DllStructCreate($sv_Type)If@ErrorThenSetError(@Error+3)Return0EndIfDllStructSetData($v_Buffer,1,$v_Data)If@ErrorThenSetError(8)Return0EndIf$iv_Address='0x'&hex($iv_StructData+$av_Offset[$i])DllCall($ah_Handle[0],'int','WriteProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')If@ErrorThenSetError(9)Return0ElseReturn$iv_AddressEndIfElseIf$i=0ThenDllCall($ah_Handle[0],'int','ReadProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')If@ErrorThenSetError(3)Return0EndIf$iv_StructData=DllStructGetData($v_Buffer,1)Else$iv_Address='0x'&hex($iv_StructData+$av_Offset[$i])DllCall($ah_Handle[0],'int','ReadProcessMemory','int',$ah_Handle[1],'int',$iv_Address,'ptr',DllStructGetPtr($v_Buffer),'int',DllStructGetSize($v_Buffer),'int','')If@ErrorThenSetError(3)Return0EndIf$iv_StructData=DllStructGetData($v_Buffer,1)EndIfNextEndFunc#endregion

Share this post

Link to post

Share on other sites

I modified the example, so it runs as well compiled as not compiled now. I removed the if @compiled statement in the client. I also changed the path to production as I guess not everyone has the beta version (I don't )

Share this post

Link to post

Share on other sites

After a cursory glance, the only thing I would change is when exiting the mother process, it should wait for the child process to end after writing the exit memory value, before exiting itself. The child can't read the variable if the mother process has ended.