Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

I want to share var between 2 processes How to copy memory do it?


  • Please log in to reply
3 replies to this topic
billy036
  • Members
  • 45 posts
  • Last active: Feb 20 2014 02:40 AM
  • Joined: 06 Nov 2011
I think CreateFileMapping Function may be do it,so I change C++ code from MSDN
Process 1:
Null :=
BUF_SIZE := 256
szName := "Global\MyFileMappingObject"
szMsg := "Message from first process"
INVALID_HANDLE_VALUE := 0xFFFFFFFF
PAGE_READWRITE := 0x4
FILE_MAP_ALL_ACCESS := 0xF001F
szMsgLen := StrLen(szMsg) * ( A_Isunicode ? 2 : 1 )
hMapFile := DllCall("CreateFileMapping"
, "Ptr", INVALID_HANDLE_VALUE
, "Ptr", 0
, "Ptr", PAGE_READWRITE
, "Ptr", 0
, "Ptr", BUF_SIZE
, "Str", szName)
if ( hMapFile == Null ) or ( hMapFile == 0 )
{
MsgBox, CreateFileMapping Failed
ExitApp
}
pBuf := DllCall("MapViewOfFile"
, "Ptr", hMapFile
, "Ptr", FILE_MAP_ALL_ACCESS
, "Ptr", 0
, "Ptr", 0
, "Ptr", BUF_SIZE)
if ( pBuf == Null ) or ( pBuf == 0 )
{
MsgBox, MapViewOfFile Failed
ExitApp
}
;I call RtlMoveMemory replace CopyMemory because call CopyMemory failed
DllCall("RtlMoveMemory", "Ptr", &pBuf, "Ptr", &szMsg, "Ptr", szMsgLen)
szBuf := StrGet(&pBuf,szMsgLen)
MsgBox, % szBuf
/*
DllCall("UnmapViewOfFile", "Ptr", pBuf)
DllCall("CloseHandle", "Ptr", hMapFile)
*/
Process 2:
Null :=
BUF_SIZE := 256
szName := "Global\MyFileMappingObject"
FILE_MAP_ALL_ACCESS := 0xF001F
hMapFile := DllCall("OpenFileMapping"
, "Ptr", FILE_MAP_ALL_ACCESS
, "Ptr", 0
, "Str", szName)
if ( hMapFile == Null ) or ( hMapFile == 0 )
{
MsgBox, OpenFileMapping Failed
ExitApp
}
pBuf := DllCall("MapViewOfFile"
, "Ptr", hMapFile
, "Ptr", FILE_MAP_ALL_ACCESS
, "Ptr", 0
, "Ptr", 0
, "Ptr", BUF_SIZE)
if ( pBuf == Null ) or ( pBuf == 0 )
{
MsgBox, MapViewOfFile Failed
ExitApp
}
szBuf := StrGet(&pBuf)
MsgBox, % szBuf
DllCall("UnmapViewOfFile", "Ptr", pBuf)
DllCall("CloseHandle", "Ptr", hMapFile)

I run process 1 at first,and run process 2

Why the process 2 msgbox is numbers ?

I hope share process 1 var to process 2 ,if process 2 change the share var vaule,Process 1 can be know it.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

First Process:

Null := 0
BUF_SIZE := 256
szName := "Global\MyFileMappingObject"
szMsg := "Message from first process"
INVALID_HANDLE_VALUE := -1
PAGE_READWRITE := 0x4
FILE_MAP_ALL_ACCESS := 0xF001F
szMsgLen := StrLen(szMsg) * ( A_Isunicode ? 2 : 1 )
hMapFile := DllCall("CreateFileMapping"
, "Ptr", INVALID_HANDLE_VALUE
, "Ptr", 0
, "Int", PAGE_READWRITE
, "Int", 0
, "Int", BUF_SIZE
, "Str", szName)
if ( hMapFile == Null ) or ( hMapFile == 0 )
{
MsgBox, CreateFileMapping Failed
ExitApp
}
pBuf := DllCall("MapViewOfFile"
, "Ptr", hMapFile
, "Int", FILE_MAP_ALL_ACCESS
, "Int", 0
, "Int", 0
, "Ptr", BUF_SIZE)
if ( pBuf == Null ) or ( pBuf == 0 )
{
MsgBox, MapViewOfFile Failed
ExitApp
}
;I call RtlMoveMemory replace CopyMemory because call CopyMemory failed
DllCall("RtlMoveMemory", "Ptr", pBuf, "Ptr", &szMsg, "Ptr", szMsgLen)
szBuf := StrGet(pBuf,szMsgLen)
MsgBox, % szBuf
/*
DllCall("UnmapViewOfFile", "Ptr", pBuf)
DllCall("CloseHandle", "Ptr", hMapFile)
*/
Second process
Null := 0
BUF_SIZE := 256
szName := "Global\MyFileMappingObject"
FILE_MAP_ALL_ACCESS := 0xF001F
hMapFile := DllCall("OpenFileMapping"
, "Ptr", FILE_MAP_ALL_ACCESS
, "Int", 0
, "Str", szName)
if ( hMapFile == Null ) or ( hMapFile == 0 )
{
MsgBox, OpenFileMapping Failed
ExitApp
}
pBuf := DllCall("MapViewOfFile"
, "Ptr", hMapFile
, "Int", FILE_MAP_ALL_ACCESS
, "Int", 0
, "Int", 0
, "Ptr", BUF_SIZE)
if ( pBuf == Null ) or ( pBuf == 0 )
{
MsgBox, MapViewOfFile Failed
ExitApp
}
szBuf := StrGet(pBuf)
MsgBox, % szBuf
DllCall("UnmapViewOfFile", "Ptr", pBuf)
DllCall("CloseHandle", "Ptr", hMapFile)



billy036
  • Members
  • 45 posts
  • Last active: Feb 20 2014 02:40 AM
  • Joined: 06 Nov 2011
thank very much! Posted Image

ruespe
  • Members
  • 567 posts
  • Last active: Dec 01 2014 07:59 PM
  • Joined: 17 Jun 2008
You also can look at this example out of the help file:

; Example: Send a string of any length from one script to another. This is a working example.
; To use it, save and run both of the following scripts then press Win+Space to show an
; InputBox that will prompt you to type in a string.


; Save the following script as "Receiver.ahk" then launch it:
#SingleInstance
OnMessage(0x4a, "Receive_WM_COPYDATA") ; 0x4a is WM_COPYDATA
return

Receive_WM_COPYDATA(wParam, lParam)
{
StringAddress := NumGet(lParam + 8) ; lParam+8 is the address of CopyDataStruct's lpData member.
CopyOfData := StrGet(StringAddress) ; Copy the string out of the structure.
; Show it with ToolTip vs. MsgBox so we can return in a timely fashion:
ToolTip %A_ScriptName%`nReceived the following string:`n%CopyOfData%
return true ; Returning 1 (true) is the traditional way to acknowledge this message.
}

; Save the following script as "Sender.ahk" then launch it. After that, press the Win+Space hotkey.
TargetScriptTitle = Receiver.ahk ahk_class AutoHotkey

#space:: ; Win+Space hotkey. Press it to show an InputBox for entry of a message string.
InputBox, StringToSend, Send text via WM_COPYDATA, Enter some text to Send:
if ErrorLevel ; User pressed the Cancel button.
return
result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
if result = FAIL
MsgBox SendMessage failed. Does the following WinTitle exist?:`n%TargetScriptTitle%
else if result = 0
MsgBox Message sent but the target window responded with 0, which may mean it ignored it.
return

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle) ; ByRef saves a little memory in this case.
; This function sends the specified string to the specified window and returns the reply.
; The reply is 1 if the target window processed the message, or 0 if it ignored it.

{
VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0) ; Set up the structure's memory area.
; First set the structure's cbData member to the size of the string, including its zero terminator:
SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
NumPut(SizeInBytes, CopyDataStruct, A_PtrSize) ; OS requires that this be done.
NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize) ; Set lpData to point to the string itself.
Prev_DetectHiddenWindows := A_DetectHiddenWindows
Prev_TitleMatchMode := A_TitleMatchMode
DetectHiddenWindows On
SetTitleMatchMode 2
SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle% ; 0x4a is WM_COPYDATA. Must use Send not Post.
DetectHiddenWindows %Prev_DetectHiddenWindows% ; Restore original setting for the caller.
SetTitleMatchMode %Prev_TitleMatchMode% ; Same.
return ErrorLevel ; Return SendMessage's reply back to our caller.
}