-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_VBA_AHK_IPC.ahk
233 lines (180 loc) · 4.61 KB
/
class_VBA_AHK_IPC.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
; **************************************************************************************
; Interprocess communication (IPC) library
;
; Notes:
; On creation, this class creates a memory mapped file (with what ever name you wish)
; and you can turn on the function to starts listening to system messages. Once
; received, the class will read the message that should be present on the memory mapped
; file and run a function with the passed function name and arguements. It responds
; with a "read" mesasge and a return value, if one is present.
; **************************************************************************************
; *********************
; Functions and classes
; *********************
; Handle messages from VBA sent via System messages
VBA_message_read(wParam, lParam, msg)
{
global V2AMessages
if (wParam = 11 and lParam = 11)
{
V2AMessages.grabNewMessage(wParam, lParam, msg)
}
return
}
; Memory mapped file class
Class MemoryMappedFile_IPC
{
; Class variables
ptr := A_PtrSize ? "ptr" : "Uint" ; determine if 32 or 64 system when using Windows DLL API
BUFFER_SIZE:= 1000000
INVALID_HANDLE_VALUE := -1
PAGE_READWRITE := 0x4
FILE_MAP_WRITE := 0x2
FILE_MAP_ALL_ACCESS := 0xF001F
V2AMessage := ""
V2AMessageSplit := ""
V2AMessages := ""
SEPARATOR := "|"
messageNumber := 5000
startMsg := "Started"
readMsg := "Read"
closedMsg := "Closed"
; Initialise class
__New(MMFNameTemp = "VBA_2_QS_IPC", listening := True)
{
this.MMFName := MMFNameTemp
this.createMapping()
this.send(this.startMsg)
if listening
OnMessage(this.messageNumber, "VBA_message_read")
return this
}
; Functions
createMapping()
{
hMMF := DllCall("CreateFileMapping", this.ptr, this.INVALID_HANDLE_VALUE, this.ptr, 0, "int", this.PAGE_READWRITE, "int", 0, "int", this.BUFFER_SIZE, "Str", this.MMFName)
if (hMMF == 0)
{
MsgBox, Error 1
return 0
}
pMemFile := DllCall("MapViewOfFile", this.ptr, hMMF, "int", this.FILE_MAP_ALL_ACCESS, this.ptr, 0, this.ptr, 0, this.ptr, 0)
if (pMemFile == 0)
{
MsgBox, Error 2
return 0
}
this.hMMF := hMMF
this.pMemFile := pMemFile
return
}
send(Func, Argument = "")
{
Func := StrReplace(Func, "<", "")
Func := StrReplace(Func, ">", "")
Func := StrReplace(Func, "|", "")
Argument := StrReplace(Argument, "<", "less than")
Argument := StrReplace(Argument, ">", "more than")
Argument := StrReplace(Argument, "|", "pipe")
if (Argument == "")
{
bufferSend := "<" . Func . ">"
}
else
{
bufferSend := "<" . Func . this.SEPARATOR . Argument . ">"
}
if (StrLen(bufferSend)*(A_Isunicode ? 2 : 1) < this.BUFFER_SIZE)
{
strput(bufferSend, this.pMemFile, "UTF-8")
}
else
{
MsgBox, % "String is larger than allocated memory space"
}
return
}
read()
{
bufferReceived:= strget(this.pMemFile, "UTF-8")
if(InStr(bufferReceived, "<") = 1)
{
EndMarkerPosition := InStr(bufferReceived, ">")
if (EndMarkerPosition <> 0)
{
return SubStr(bufferReceived, 2, EndMarkerPosition - 2)
}
else
{
;MsgBox, End marker is missing!
}
}
else
{
;MsgBox, Beginning marker is missing!
}
return
}
grabNewMessage()
{
global Developing
newMessage := False
Loop, 1200 ; = 1 minutes if using 50 msec sleep
{
V2AMessage := this.Read()
V2AMessageSplit := StrSplit(V2AMessage, this.SEPARATOR)
if V2AMessageSplit[1] != this.readMsg and V2AMessageSplit[1] != this.startMsg
{
newMessage := True
break
}
sleep 50
}
if (newMessage == False)
{
MsgBox, % "No new message was ever received from VBA!"
return False
}
if (V2AMessageSplit[1] = "close")
{
this.send(this.readMsg)
Msgbox, % "Closing down Quick Spiritum..."
ExitApp
}
else if (V2AMessageSplit[1] <> this.readMsg and V2AMessageSplit[1] <> this.startMsg)
{
function := V2AMessageSplit[1]
if (function != "confirmStatus" and Developing)
{
TrayTip, %TrayTipTitle%, % " Spiritum database call for function: " . function
}
returnResult := %function%(V2AMessageSplit[2])
if (StrLen(returnResult) = 0)
{
this.send(this.readMsg)
}
else
{
this.send(this.readMsg, returnResult)
}
}
return True
}
__Delete()
{
this.Send(this.closed.msg)
DllCall("UnmapViewOfFile", "Ptr", this.pMemFile)
DllCall("CloseHandle", "Ptr", this.hMMF)
return
}
}
confirmStatus(nothing)
{
return "Running;" . A_ScriptFullPath
}
; Used for IPC testing purposes
DisplayMessage(message)
{
MsgBox, ,Test Message, % message , 3
return "complete"
}