forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_RemoteBuf.ahk
156 lines (124 loc) · 5.36 KB
/
_RemoteBuf.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
; Title: Remote Buffer
; *Read and write process memory*
;
/*-------------------------------------------------------------------------------
Function: Open
Open remote buffer
Parameters:
H - Reference to variable to receive remote buffer handle
hwnd - HWND of the window that belongs to the process
size - Size of the buffer
Returns:
Error message on failure
*/
RemoteBuf_Open(ByRef H, hwnd, size) {
static MEM_COMMIT:=0x1000, PAGE_READWRITE:=4
WinGet, pid, PID, ahk_id %hwnd%
hProc := DllCall( "OpenProcess", "uint", 0x38, "int", 0, "uint", pid) ;0x38 = PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE
IfEqual, hProc,0, return A_ThisFunc "> Unable to open process (" A_LastError ")"
bufAdr := DllCall( "VirtualAllocEx", "PTR", hProc, "PTR", 0, "PTR", size, "uint", MEM_COMMIT, "uint", PAGE_READWRITE)
IfEqual, bufAdr,0, return A_ThisFunc "> Unable to allocate memory (" A_LastError ")"
; Buffer handle structure:
; @0: hProc
; @4: size
; @8: bufAdr
VarSetCapacity(H, A_PtrSize*3, 0 )
NumPut( hProc, H, 0,"PTR")
NumPut( size, H, A_PtrSize,"PTR")
NumPut( bufAdr, H, A_PtrSize*2,"PTR")
}
/*----------------------------------------------------
Function: Close
Close the remote buffer
Parameters:
H - Remote buffer handle
*/
RemoteBuf_Close(ByRef H) {
static MEM_RELEASE := 0x8000
handle := NumGet(H, 0,"PTR")
IfEqual, handle, 0, return A_ThisFunc "> Invalid remote buffer handle"
adr := NumGet(H, A_PtrSize*2,"PTR")
r := DllCall( "VirtualFreeEx", "PTR", handle, "PTR", adr, "PTR", 0, "uint", MEM_RELEASE)
ifEqual, r, 0, return A_ThisFunc "> Unable to free memory (" A_LastError ")"
DllCall( "CloseHandle", "PTR", handle )
VarSetCapacity(H, 0 )
}
/*----------------------------------------------------
Function: Read
Read from the remote buffer into local buffer
Parameters:
H - Remote buffer handle
pLocal - Reference to the local buffer
pSize - Size of the local buffer
pOffset - Optional reading offset, by default 0
Returns:
TRUE on success or FALSE on failure. ErrorMessage on bad remote buffer handle
*/
RemoteBuf_Read(ByRef H, ByRef pLocal, pSize, pOffset := 0){
handle := NumGet( H, 0,"PTR"), size:= NumGet( H, A_PtrSize,"PTR"), adr := NumGet( H, A_PtrSize*2,"PTR")
IfEqual, handle, 0, return A_ThisFunc "> Invalid remote buffer handle"
IfGreaterOrEqual, offset, %size%, return A_ThisFunc "> Offset is bigger then size"
VarSetCapacity( pLocal, pSize )
return DllCall( "ReadProcessMemory", "PTR", handle, "PTR", adr + pOffset, "PTR", &pLocal, "PTR", size, "PTR", 0 ), VarSetCapacity(pLocal, -1)
}
/*----------------------------------------------------
Function: Write
Write local buffer into remote buffer
Parameters:
H - Remote buffer handle
pLocal - Reference to the local buffer
pSize - Size of the local buffer
pOffset - Optional writting offset, by default 0
Returns:
TRUE on success or FALSE on failure. ErrorMessage on bad remote buffer handle
*/
RemoteBuf_Write(Byref H, byref pLocal, pSize, pOffset:=0) {
handle:= NumGet( H, 0,"PTR"), size := NumGet( H, A_PtrSize,"PTR"), adr := NumGet( H, A_PtrSize*2,"PTR")
IfEqual, handle, 0, return A_ThisFunc "> Invalid remote buffer handle"
IfGreaterOrEqual, offset, %size%, return A_ThisFunc "> Offset is bigger then size"
return DllCall( "WriteProcessMemory", "PTR", handle,"PTR", adr + pOffset,"PTR", &pLocal,"PTR", pSize, "PTR", 0 )
}
/*----------------------------------------------------
Function: Get
Get address or size of the remote buffer
Parameters:
H - Remote buffer handle
pQ - Query parameter: set to "adr" to get address (default), to "size" to get the size or to "handle" to get Windows API handle of the remote buffer.
Returns:
Address or size of the remote buffer
*/
RemoteBuf_Get(ByRef H, pQ:="adr") {
return pQ = "adr" ? NumGet(H, A_PtrSize*2,"PTR") : pQ = "size" ? NumGet(H, A_PtrSize,"PTR") : NumGet(H,"PTR")
}
/*---------------------------------------------------------------------------------------
Group: Example
(start code)
;get the handle of the Explorer window
WinGet, hw, ID, ahk_class ExploreWClass
;open two buffers
RemoteBuf_Open( hBuf1, hw, 128 )
RemoteBuf_Open( hBuf2, hw, 16 )
;write something
str := "1234"
RemoteBuf_Write( hBuf1, str, strlen(str) )
str := "_5678"
RemoteBuf_Write( hBuf1, str, strlen(str), 4)
str := "_testing"
RemoteBuf_Write( hBuf2, str, strlen(str))
;read
RemoteBuf_Read( hBuf1, str, 10 )
out = %str%
RemoteBuf_Read( hBuf2, str, 10 )
out = %out%%str%
MsgBox %out%
;close
RemoteBuf_Close( hBuf1 )
RemoteBuf_Close( hBuf2 )
(end code)
*/
/*-------------------------------------------------------------------------------------------------------------------
Group: About
o Ver 2.0 by majkinetor. See http://www.autohotkey.com/forum/topic12251.html
o Code updates by infogulch
o Licenced under Creative Commons Attribution-Noncommercial <http://creativecommons.org/licenses/by-nc/3.0/>.
*/