-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPC.au3
More file actions
1636 lines (1583 loc) · 80.1 KB
/
IPC.au3
File metadata and controls
1636 lines (1583 loc) · 80.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include-once
#include <AutoItConstants.au3>
#include <WinAPIProc.au3>
; #INDEX# =======================================================================================================================
; Title .........: IPC (InterProcessCommunication)
; AutoIt Version : 3.3.18.1
; Language ......: English
; Description ...: UDF for inter process communication between the main and child processes using TCP.
; Strings can be send as well as integer commands.
; Author(s) .....: Kanashius
; Version .......: 1.1.0
; ===============================================================================================================================
; #CURRENT# =====================================================================================================================
; __IPC_StartUp
; __IPC_Shutdown
; __IPC_GetScriptExecutable
; __IPC_StartProcess
; __IPC_SubGetPID
; __IPC_SubCheck
; __IPC_SubConnect
; __IPC_SubSend
; __IPC_SubSendCmd
; __IPC_MainSend
; __IPC_MainSendCmd
; __IPC_ProcessStop
; __IPC_SubProcessing
; __IPC_MainProcessing
; __IPC_Log
; ===============================================================================================================================
; #INTERNAL_USE_ONLY# ===========================================================================================================
; __IPC__SendMsg
; __IPC__ToBinary
; __IPC__BinaryConsumeVar
; __IPC__BinaryConsumeBytes
; __IPC__SubDisconnect
; __IPC__ServerStart
; __IPC__ServerIsRunning
; __IPC__ServerStop
; __IPC__ServerProcessStdOut
; __IPC__ServerProcessLogStd
; __IPC__ServerAccept
; __IPC__AddSocket
; __IPC__ProcessMessages
; __IPC__ProcessMessagesAtSocket
; __IPC__SocketReadBytes
; __IPC__SocketDisconnect
; __IPC__ServerProcessRemove
; __IPC__ProcessIdToHandle
; __IPC__ProcessHandleToId
; ===============================================================================================================================
; #GLOBAL CONSTANTS# ============================================================================================================
Global Const $__IPC_LOG_FATAL = 1, $__IPC_LOG_ERROR = 2, $__IPC_LOG_WARN = 3, $__IPC_LOG_INFO = 4, $__IPC_LOG_DEBUG = 5
Global Const $__IPC_LOG_TRACE = 6
; ===============================================================================================================================
; #INTERNAL_USE_ONLY GLOBAL VARIABLES # =========================================================================================
Global Const $__IPC_CONN_TO_MAIN = 1, $__IPC_CONN_TO_SUB = 2
Global Const $__IPC_MSG_CONNECT = Int(1, 1), $__IPC_MSG_DISCONNECT = Int(2, 1), $__IPC_MSG_ACK = Int(3, 1)
Global Const $__IPC_MSG_DATA = Int(4, 1), $__IPC_MSG_CMD = Int(5, 1), $__IPC_MSG_CMD_DATA = Int(6, 1)
Global Const $__IPC_Port = 40001, $__IPC_MainPullRate = 100, $__IPC_MaxByteRecv = 1024, $__IPC_SubPullRate = 100
Global Const $__IPC_PARAM_CONNECT = "--IPC-CONNECT"
Global Const $__IPC_DataType_Binary = Int(1, 1), $__IPC_DataType_Bool = Int(2, 1), $__IPC_DataType_Func = Int(3, 1)
Global Const $__IPC_DataType_Double = Int(4, 1), $__IPC_DataType_Ptr = Int(5, 1), $__IPC_DataType_Hwnd = Int(6, 1)
Global Const $__IPC_DataType_Int32 = Int(7, 1), $__IPC_DataType_Int64 = Int(8, 1), $__IPC_DataType_Object = Int(9, 1)
Global Const $__IPC_DataType_String = Int(10, 1), $__IPC_DataType_Keyword = Int(11, 1), $__IPC_DataType_DLLStruct = Int(12, 1)
Global Const $__IPC_DataType_Map = Int(13, 1), $__IPC_DataType_Array = Int(14, 1)
Global $__IPC__Data[]
; ===============================================================================================================================
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_StartUp
; Description ...: StartUp of the ICP UDF initializing required variables. Must be called before using other UDF functions.
; Syntax ........: __IPC_StartUp([$iLogLevel = $__IPC_LOG_INFO[, $iMainPullRate = Default[, $iMaxReceiveCount = Default[, $iMainPort = Default]]]])
; Parameters ....: $iLogLevel - [optional] Default: $__IPC_LOG_INFO. All logging equal or lower to the level will be shown.
; $iMainPullRate - [optional] Default: 100 ms. How often the main process looks for new data.
; $iMaxReceiveCount - [optional] Default: Default. Unlimited, however much data can be received.
; $iMainPort - [optional] Default: 40001. The port to start looking for an open port for the TCPServer.
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......:
; Possible log levels: $__IPC_LOG_FATAL, $__IPC_LOG_ERROR, $__IPC_LOG_WARN, $__IPC_LOG_INFO, $__IPC_LOG_DEBUG and $__IPC_LOG_TRACE = 6
;
; The $iMainPullRate defines how often the main process checks TCP/STDOUT/STDERR streams for new data or connections.
; This should not be set very low, because that may cause blocking of the AutoIt-Script (Freezing).
; If a lot of data is sent, consider setting the $iMainPullRate higher to avoid application freezes (see AdlibRegister blocking).
; Setting the $iMainPullRate to 0 will disable the automatic handling of data (AdlibRegister). Then the __IPC_MainProcessing
; function must be called manually.
;
; $iMainPort can be set, but it will not garantee the usage of that port. The Script will start with that number
; and try to listen at that port. If it is already bound, the port will increase, until a free port is found.
; If none is found, __IPC_StartProcess will return an error (when the TCPServer is started).
;
; $iMaxReceiveCount can be set to limit the number of calls to TCPRecv, receiving $__IPC_MaxByteRecv bytes of data.
; This can be useful, if large amount of data are sent in a short time, to avoid the main process from freezing.
; Together with manual handling of data (see $iMainPullRate), this can be used to ensure that the main process does not freeze.
; $iMaxReceiveCount resets for every sub process => every sub process gets pulled $iMaxReceiveCount times.
;
; Errors:
; 1 - Parameter not valid (@extended: 1 - $iLogLevel, 2 - $iMainPullRate, 3 - $iMainPort)
; 2 - __IPC_StartUp was already called, call __IPC_Shutdown first
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_StartUp($iLogLevel = $__IPC_LOG_INFO, $iMainPullRate = Default, $iMaxReceiveCount = Default, $iMainPort = Default)
; handle parameters
If Not UBound(MapKeys($__IPC__Data))=0 Then Return SetError(2, 0, False)
If $iMainPullRate = Default Then $iMainPullRate = $__IPC_MainPullRate
If $iMainPort = Default Then $iMainPort = $__IPC_Port
If Not IsInt($iLogLevel) Or $iLogLevel<0 Or $iLogLevel>$__IPC_LOG_TRACE Then Return SetError(1, 1, False)
If Not IsInt($iMainPullRate) Or $iMainPullRate<0 Then Return SetError(1, 2, False)
If $iMaxReceiveCount <> Default And (Not IsInt($iMaxReceiveCount) Or $iMaxReceiveCount < 0) Then Return SetError(1, 3, False)
If Not IsInt($iMainPort) Or $iMainPort<1024 Or $iMainPort>65535 Then Return SetError(1, 4, False)
If Not MapExists($__IPC__Data, "iLogLevel") Then $__IPC__Data.iLogLevel = $iLogLevel
; init tcp
$__IPC__Data.iStartUp = TCPStartup()
$__IPC__Data.iMaxReceiveCount = $iMaxReceiveCount ; how often tcprecv is called during processing, if more data is available
; init socket map
Local $mConnects[]
$__IPC__Data.mConnects = $mConnects
; init server data map
Local $mServer[], $mProcesses[]
$mServer.mProcesses = $mProcesses ; all started processes are stored here
$mServer.iMainPullRate = $iMainPullRate
$mServer.iMainStartPort = $iMainPort
$mServer.iListen = Default ; the tcplisten handle
$mServer.iPort = Default ; the actually used port
$mServer.iOpenProcesses = 0 ; the number of processes started, but not yet successfully identified at the socket
; ($__IPC_MSG_CONNECT with $hProcess to assign the socket to the process)
$__IPC__Data.mServer = $mServer
; init client data map
Local $mClient[]
$mClient.iPullRate = $__IPC_SubPullRate
$mClient.iSocket = Default ; the subprocess tcp socket
$mClient.sCallback = Default ; the callback of the sub process for message handling
$mClient.sExitCallback = Default ; the callback called when the connection to the main process is closed/lost
$__IPC__Data.mClient = $mClient
__IPC_Log($__IPC_LOG_INFO, "IPC started")
Return True
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_Shutdown
; Description ...: Shutdown of the ICP UDF. Should be called to end the usage of the IPC UDF (or on exit).
; Syntax ........: __IPC_Shutdown()
; Parameters ....:
; Return values .: True on success, False if __IPC_StartUp was never called.
; Author ........: Kanashius
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_Shutdown()
If UBound(MapKeys($__IPC__Data))=0 Then Return False ; startup was not called or shutdown was already called
; disconnect from all processes and send them the message to terminate
Local $arProcesses = MapKeys($__IPC__Data.mServer.mProcesses)
For $i=0 To UBound($arProcesses)-1
__IPC__ServerProcessRemove($arProcesses[$i], True)
Next
; disconnect if it is a subprocess (main process happens nothing)
__IPC__SubDisconnect()
; shutdown tcp if it was started successfully
If MapExists($__IPC__Data, "iStartUp") And $__IPC__Data.iStartUp=1 Then TCPShutdown()
; reset data map
Local $mData[]
$__IPC__Data = $mData
__IPC_Log($__IPC_LOG_INFO, "IPC shutdown")
Return True
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_GetScriptExecutable
; Description ...: Returns the $sExecutable parameter for __IPC_StartProcess, when an .au3 or .exe with the name exists.
; Syntax ........: __IPC_GetScriptExecutable($sScriptPath)
; Parameters ....: $sScriptPath -
; Return values .: The $sExecutable for the __IPC_StartProcess function.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Provides the AutoIt executable to the path of any .au3 file.
; Automatically completes the filename with the .au3 or .exe extension, if such a file exists.
; The .au3 will be preferred.
; e.g.: "IPC-Example-SubSeperated" will automatically start the corresponding .au3 file, if present.
; If not, but a .exe is present, that will be executed.
; If "IPC-Example-SubSeperated.au3" is given, the result will be an AutoIt executable with the script as parameter.
; If the main process is a compiled AutoIt executable, it includes the AutoIt Interpreter.
; So calling it with a Script as parameter will execute that Script.
;
; Errors:
; 1 - Parameter $sScriptPath not valid (@extended: 1 - no possible .au3/.exe file exists,
; 2 - the file is not a .au3 or .exe)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_GetScriptExecutable($sScriptPath)
; handle $sScriptPath, if it is provided without the extension => autocomplete
If Not FileExists($sScriptPath) Then
If FileExists($sScriptPath&".au3") Then
$sScriptPath = $sScriptPath&".au3"
ElseIf FileExists($sScriptPath&".exe") Then
$sScriptPath = $sScriptPath&".exe"
Else
Return SetError(1, 1, False)
EndIf
EndIf
; check the extension and return the desired executable (with arguments)
Local $sExt = StringRight($sScriptPath, 4)
If $sExt = ".au3" Then Return '"'&@AutoItExe&'" /AutoIt3ExecuteScript "'&$sScriptPath&'"'
If $sExt = ".exe" Then Return $sScriptPath
Return SetError(1, 2, False)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_StartProcess
; Description ...: Starts a sub process from the main process.
; Syntax ........: __IPC_StartProcess([$sCallback = Default[, $arguments = Default[, $sDoneCallback = Default[, $sExecutable = Default[, $sWorkingDir = ""[, $show = @SW_HIDE[, $opt_flag = BitOR($STDOUT_CHILD, $STDERR_CHILD)]]]]]]])
; Parameters ....: $sCallback - [optional] Default: no callback. The callback function to call for incoming data from the subprocess.
; $arguments - [optional] Default: no arguments. The arguments to call the subprocess with. Can be a String or a 1D-Array.
; $sDoneCallback - [optional] Default: no callback. This function is called with the $hProcess, when the sub process is closed.
; $sExecutable - [optional] Default: the script itself. The executable to be executed.
; $sWorkingDir - [optional] Default: the script directory. The working dir for the subprocess.
; $show - [optional] Default: @SW_HIDE. See function: "Run".
; $opt_flag - [optional] Default: $STDOUT_CHILD+$STDERR_CHILD. See function: "Run".
; Return values .: The $hSubProcess of the started subprocess. 0 on failure.
; Author ........: Kanashius
; Modified ......:
; Remarks .......:
; $sCallback must be a function with 3 parameters ($hSubProcess, $iCmd, $arData).
; $iCmd is the integer command and $arData is a 1D-Array with all values sent.
; $iCmd or $arData can be Default, if they were not sent.
;
; If $arguments is a 1D-Array, all not string values are converted to string with String().
;
; $sDoneCallback must be a function with 1 parameter ($hProcess). This function will be called, when the sub process does not exist anymore (ProcessExist).
;
; If $sExecutable should be different then the script of the main process, it can be provided here. This can be anything Run() can execute with parameters.
; If it should be another script, __IPC_GetScriptExecutable can be used to get the path to the .au3 or .exe (whichever is present).
;
; To disable console output of the subprocess, $opt_flag can be set to 0 (Or any other $opt_flag, see Run() ).
;
; Errors:
; 1 - Parameter invalid (@extended: 1 - $sCallback, 2 - $arguments, 3 - $sDoneCallback, 4 - $sExecutable, 5 - $sWorkingDir, 6 - $show, 7 - $opt_flag)
; 100 - Calling __IPC__ServerStart failed. @extended contains the @error of__IPC__ServerStart
; 200 - Error creating process handle
; ? - Look at possible @error/@extended from Run()
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_StartProcess($sCallback = Default, $arguments = Default, $sDoneCallback = Default, $sExecutable = Default, $sWorkingDir = "", $show = @SW_HIDE, $opt_flag = BitOR($STDOUT_CHILD, $STDERR_CHILD))
; start the main process server, if it is not running already
If Not __IPC__ServerIsRunning() Then
__IPC__ServerStart()
If @error Then Return SetError(100, @error, 0)
EndIf
; handle all parameters
If $sExecutable=Default Then $sExecutable = __IPC_GetScriptExecutable(@ScriptFullPath)
If $sCallback<>Default And Not IsFunc(Execute($sCallback)) Then Return SetError(1, 1, -1)
If $arguments<>Default And Not IsString($arguments) And Not IsArray($arguments) Then Return SetError(1, 2, 0)
If $sDoneCallback<>Default And Not IsFunc(Execute($sDoneCallback)) Then Return SetError(1, 3, -1)
If $sExecutable<>Default And Not IsString($sExecutable) Then Return SetError(1, 4, 0)
If Not IsString($sWorkingDir) Then Return SetError(1, 5, 0)
If $show<>@SW_SHOW And $show<>@SW_HIDE And $show<>@SW_MINIMIZE And $show<>@SW_MAXIMIZE Then Return SetError(1, 6, 0)
If Not IsInt($opt_flag) Then Return SetError(1, 7, 0)
; create process data
Local $mProcess[]
$mProcess.bStdErr = (BitAND($opt_flag, $STDERR_CHILD)?(True):(False))
$mProcess.bStdOut = ((BitAND($opt_flag, $STDOUT_CHILD) Or BitAND($opt_flag, $STDERR_MERGED))?(True):(False))
$mProcess.iSocket = Default
$mProcess.bWaitForSocket = True
$mProcess.sCallback = $sCallback
$mProcess.sExitCallback = $sDoneCallback
; create process handle
Local $iProcess = MapAppend($__IPC__Data.mServer.mProcesses, $mProcess)
If @error Then Return SetError(200, 0, 0)
Local $hProcess = __IPC__ProcessIdToHandle($iProcess)
; create arguments
Local $sArguments = $__IPC_PARAM_CONNECT&" "&$__IPC__Data.mServer.iPort&" "&$hProcess
If IsString($arguments) Then
$sArguments &= " "&$arguments
ElseIf UBound($arguments)>0 Then
For $i=0 to UBound($arguments)-1 Step 1
$sArguments&=' "'&StringReplace($arguments[$i], '"', '""')&'"'
Next
EndIf
__IPC_Log($__IPC_LOG_INFO, "Start process: "&$sExecutable)
__IPC_Log($__IPC_LOG_DEBUG, @TAB&" with arguments: "&$sArguments)
; start the sub process
Local $sCmd = $sExecutable&" "&$sArguments
Local $iPID = Run($sCmd, $sWorkingDir, $show, $opt_flag)
Local $iError = @error, $iExtended = @extended
; save additional process information
$__IPC__Data["mServer"]["mProcesses"][$iProcess]["iPID"] = $iPID
$__IPC__Data["mServer"]["mProcesses"][$iProcess]["hProcess"] = $hProcess
; add one process to the number of processes started, but not yet successfully connected ($__IPC_MSG_CONNECT with $hProcess)
$__IPC__Data["mServer"]["iOpenProcesses"] += 1
; remove all process data, if the startup failed
If $iError Then
__IPC__ServerProcessRemove($iProcess, True)
__IPC_Log($__IPC_LOG_ERROR, "Failed process start: "&$sCmd)
Return SetError($iError, $iExtended, 0)
EndIf
Return SetExtended($iExtended, $hProcess)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_SubGetPID
; Description ...: Get the PID of a running sub process.
; Syntax ........: __IPC_SubGetPID($hProcess)
; Parameters ....: $hProcess - the sub process handle
; Return values .: The PID. 0 on failure.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $hProcess)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_SubGetPID($hProcess)
Local $iProcess = __IPC__ProcessHandleToId($hProcess)
If @error Then Return SetError(1, 1, 0)
Return $__IPC__Data.mServer.mProcesses[$iProcess].iPID
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_SubCheck
; Description ...: Check if the script is running as sub process and call the corresponding sub process function.
; Syntax ........: __IPC_SubCheck($sFunctionSub[, $sFunctionMain = Default[, $sCallback = Default[, $sExitCallback = Default[, $iLogLevel = $__IPC_LOG_INFO[, $iPullRate = Default]]]]])
; Parameters ....: $sFunctionSub - the function to call for the sub process execution.
; $sFunctionMain - [optional] Default: none. The function to call for the main process execution.
; $sCallback - [optional] Default: no callback. The callback for the sub process messages.
; $sExitCallback - [optional] Default: no callback. The callback for the disconnect/close command from the main process.
; $iLogLevel - [optional] Default: $__IPC_LOG_INFO. All logging equal or lower to the level will be shown.
; $iPullRate - [optional] Default: 100 ms. How often the sub process looks for new data.
; Return values .: The sub process handle on success, 0 otherwise.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: $sFunctionSub must be a function with 1 parameter ($hSubProcess). It is called, when the script is executed as sub process.
;
; $sFunctionMain must be a function without parameters. It is called, when the script is executed as main process.
;
; $sCallback must be a function with 2 parameters ($iCmd, $arData).
; $iCmd contains the integer command and $arData is a 1D-Array with all values sent.
; $iCmd or $arData can be Default, if they were not sent.
;
; $sExitCallback must be a function without parameters. This function will be called, when the main process disconnects from the sub process.
;
; Possible log levels: $__IPC_LOG_FATAL, $__IPC_LOG_ERROR, $__IPC_LOG_WARN, $__IPC_LOG_INFO, $__IPC_LOG_DEBUG and $__IPC_LOG_TRACE = 6
;
; The $iPullRate defines how often the sub process checks TCP streams for new data.
; This should not be set very low, because that may cause blocking of the AutoIt-Script (freezing).
; If a lot of data is sent, consider setting the $iPullRate higher to avoid application freezes (see AdlibRegister blocking).
; Setting the $iPullRate to 0 will disable the automatic handling of data (AdlibRegister). Then the __IPC_SubProcessing
; function must be called manually.
;
; Errors:
; 1 - Parameter invalid (@extended: 1 - $sFunctionSub, 2 - $sFunctionMain, 3 - $sCallback, 4 - $sExitCallback, 5 - $iLogLevel, 6 - $iPullRate, 7 - $iMainPullRate)
; 2 - __IPC_StartUp failed. @extended contains the error from __IPC_StartUp.
; 3 - Connect to main process failed (TCPConnect)
; 4 - Failed to sent the connect command to the main process with tcp
; 5 - Calling $sFunctionSub failed.
; 6 - Calling $sFunctionMain failed.
; 7 - Parameter in commandline invalid (@extended: 1 - iPort, 2 - hProcess)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_SubCheck($sFunctionSub, $sFunctionMain = Default, $sCallback = Default, $sExitCallback = Default, $iLogLevel = $__IPC_LOG_INFO, $iPullRate = Default)
; handle parameters
If Not IsFunc(Execute($sFunctionSub)) Then Return SetError(1, 1, 0)
If $sFunctionMain<>Default And Not IsFunc(Execute($sFunctionMain)) Then Return SetError(1, 2, 0)
; other parameters will be checked/handled in __IPC_SubConnect
; check if the script was started with commandline argument $__IPC_PARAM_CONNECT and the corresponding port and process number
Local $iPort = Default, $hProcess = Default
If UBound($CmdLine)>=4 Then
If $CmdLine[1]=$__IPC_PARAM_CONNECT Then
$iPort = Int($CmdLine[2])
$hProcess = Int($CmdLine[3])
EndIf
Local $arCmdLine[UBound($CmdLine)-3]
$arCmdLine[0] = UBound($arCmdLine)-1
For $i=4 to UBound($CmdLine)-1
$arCmdLine[$i-3] = $CmdLine[$i]
Next
$CmdLine = $arCmdLine
EndIf
; no subprocess detected
If $iPort=Default Or $hProcess=Default Then
If $sFunctionMain<>Default Then
__IPC_StartUp($iLogLevel)
If @error And @error<>2 Then Return SetError(2, @error, False) ; ignore the "startup already called" message, otherwise return the startup error
Call($sFunctionMain)
If @error = 0xDEAD And @extended = 0xBEEF Then Return SetError(6, 0, 0)
EndIf
Return 0
EndIf
Local $hSubProcess = __IPC_SubConnect($iPort, $hProcess, $sCallback, $sExitCallback, $iLogLevel, $iPullRate)
If @error=1 And @extended>2 Then Return SetError(1, @extended, 0)
If @error=1 Then Return SetError(7, @extended, 0)
If @error Then Return SetError(@error, @extended, 0)
Call($sFunctionSub, $hSubProcess)
If @error = 0xDEAD And @extended = 0xBEEF Then Return SetError(5, 0, 0)
Return $hSubProcess
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_SubConnect
; Description ...: Connect to the main process.
; Syntax ........: __IPC_SubConnect($iPort, $hProcess)
; Parameters ....: $iPort - the port the main process is listening at.
; $hProcess - the hSubProcessHandle of this script.
; $sCallback - [optional] Default: no callback. The callback for the sub process messages.
; $sExitCallback - [optional] Default: no callback. The callback for the disconnect/close command from the main process.
; $iLogLevel - [optional] Default: $__IPC_LOG_INFO. All logging equal or lower to the level will be shown.
; $iPullRate - [optional] Default: 100 ms. How often the sub process looks for new data.
; Return values .: The sub process handle on success, 0 otherwise.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: $iPort must be and integer >=1024 and <65535.
; $hProcess must be an integer >=1 and must correspond to the process handle at the main process.
;
; For more information on $sCallback, $sExitCallback, $iLogLevel and $iPullRate see __IPC_SubCheck.
;
; Errors:
; 1 - Parameter invalid (@extended: 1 - $iPort, 2 - $hProcess)
; 2 - Connect to main process failed (TCPConnect)
; 3 - __IPC_StartUp failed. @extended contains the error from __IPC_StartUp.
; 4 - Failed to sent the connect command to the main process with tcp
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_SubConnect($iPort, $hProcess, $sCallback = Default, $sExitCallback = Default, $iLogLevel = $__IPC_LOG_INFO, $iPullRate = Default)
If Not IsInt($iPort) Or $iPort<1024 Or $iPort>65535 Then Return SetError(1, 1, 0)
If Not IsInt($hProcess) Or $hProcess<1 Then Return SetError(1, 2, 0)
If $sCallback<>Default And Not IsFunc(Execute($sCallback)) Then Return SetError(1, 3, 0)
If $sExitCallback<>Default And Not IsFunc(Execute($sExitCallback)) Then Return SetError(1, 4, 0)
If $iPullRate = Default Then $iPullRate = $__IPC_SubPullRate
If Not IsInt($iLogLevel) Or $iLogLevel<0 Or $iLogLevel>$__IPC_LOG_TRACE Then Return SetError(1, 5, False)
If Not IsInt($iPullRate) Or $iPullRate<0 Then Return SetError(1, 6, False)
; handle detected sub process
__IPC_StartUp($iLogLevel)
If @error And @error<>2 Then Return SetError(2, @error, 0) ; ignore the "startup already called" message, otherwise return the startup error
; connect to main process
__IPC_Log($__IPC_LOG_INFO, "Connect: "&$iPort&" >> "&$hProcess)
Local $iSocket = TCPConnect("127.0.0.1", $iPort)
If @error Then
__IPC_Log($__IPC_LOG_ERROR, "Could not connect to main process: "&$iPort&" > "&$hProcess)
Return SetError(3, 0, 0)
EndIf
; connect to the main process
TCPSend($iSocket, Binary($__IPC_MSG_CONNECT)&Binary($hProcess))
IF @error Then Return SetError(4, @error, 0)
; save data for the connection
$__IPC__Data.mClient.iSocket = $iSocket
$__IPC__Data.mClient.sCallback = $sCallback
$__IPC__Data.mClient.sExitCallback = $sExitCallback
$__IPC__Data.mClient.iPullRate = $iPullRate
If $iPullRate>0 And $__IPC__Data.mClient.sCallback<>Default Then AdlibRegister("__IPC_SubProcessing", $__IPC__Data.mClient.iPullRate)
__IPC__AddSocket($iSocket, $__IPC_CONN_TO_MAIN)
Return $hProcess
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_SubSend
; Description ...: Send data to the main process
; Syntax ........: __IPC_SubSend($data1, [$data2 = Default, [$data3 = Default, [$data4 = Default, [$data5 = Default,
; [$data6 = Default, [$data7 = Default, [$data8 = Default, [$data9 = Default, [$data10 = Default]]]]]]]]])
; Parameters ....: $hProcess - the sub process handle
; $data1 - the data
; $data2 - [optional] the data (if provided, it will be send, even if default)
; ... - ...
; $data10 - [optional] the data (if provided, it will be send, even if default)
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid
; 2 - TCP Send failed
; 3 - Not connected to sub process
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_SubSend($data1, $data2 = Default, $data3 = Default, $data4 = Default, $data5 = Default, $data6 = Default, _
$data7 = Default, $data8 = Default, $data9 = Default, $data10 = Default)
If $__IPC__Data.mClient.iSocket=Default Then Return SetError(3, 0, False)
Local $arArgs[@NumParams+3]
$arArgs[0] = "CallArgArray"
$arArgs[1] = $__IPC__Data.mClient.iSocket
$arArgs[2] = Default
For $i=1 To @NumParams
$arArgs[$i+2] = Eval("data"&$i)
Next
Local $bResult = Call("__IPC__SendMsg", $arArgs)
If @error Then Return SetError(@error, @extended-2, $bResult)
Return $bResult
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_SubSendCmd
; Description ...: Send an integer command (with data) to the main process
; Syntax ........: __IPC_SubSendCmd($hProcess, $iCmd, [$data1 = Default, [$data2 = Default, [$data3 = Default, [$data4 = Default,
; [$data5 = Default, [$data6 = Default, [$data7 = Default, [$data8 = Default, [$data9 = Default,
; [$data10 = Default]]]]]]]]]])
; Parameters ....: $iCmd - integer as command
; $data1 - [optional] the data (if provided, it will be send, even if default)
; ... - ...
; $data10 - [optional] the data (if provided, it will be send, even if default)
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (1 - $iCmd)
; 2 - TCP Send failed
; 3 - Not connected to sub process
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_SubSendCmd($iCmd, $data1 = Default, $data2 = Default, $data3 = Default, $data4 = Default, $data5 = Default, $data6 = Default, _
$data7 = Default, $data8 = Default, $data9 = Default, $data10 = Default)
If $__IPC__Data.mClient.iSocket=Default Then Return SetError(3, 0, False)
If Not IsInt($iCmd) Then Return SetError(1, 2, False)
Local $arArgs[@NumParams+2]
$arArgs[0] = "CallArgArray"
$arArgs[1] = $__IPC__Data.mClient.iSocket
$arArgs[2] = $iCmd
For $i=1 To @NumParams-1
$arArgs[$i+2] = Eval("data"&$i)
Next
Local $bResult = Call("__IPC__SendMsg", $arArgs)
If @error Then Return SetError(@error, @extended-1, $bResult)
Return $bResult
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_MainSend
; Description ...: Send data to a sub process
; Syntax ........: __IPC_MainSend($hProcess, $data1, [$data2 = Default, [$data3 = Default, [$data4 = Default, [$data5 = Default,
; [$data6 = Default, [$data7 = Default, [$data8 = Default, [$data9 = Default,
; [$data10 = Default]]]]]]]]])
; Parameters ....: $hProcess - the sub process handle
; $data1 - the data
; $data2 - [optional] the data (if provided, it will be send, even if default)
; ... - ...
; $data10 - [optional] the data (if provided, it will be send, even if default)
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $hProcess)
; 2 - TCP Send failed
; 3 - Not connected to sub process
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_MainSend($hProcess, $data1, $data2 = Default, $data3 = Default, $data4 = Default, $data5 = Default, $data6 = Default, _
$data7 = Default, $data8 = Default, $data9 = Default, $data10 = Default)
Local $iProcess = __IPC__ProcessHandleToId($hProcess)
If @error Then Return SetError(1, 1, False)
Local $iSocket = $__IPC__Data.mServer.mProcesses[$iProcess].iSocket
If $iSocket=Default Then Return SetError(3, 0, False)
Local $arArgs[@NumParams+2]
$arArgs[0] = "CallArgArray"
$arArgs[1] = $iSocket
$arArgs[2] = Default
For $i=1 To @NumParams
$arArgs[$i+2] = Eval("data"&$i)
Next
Local $bResult = Call("__IPC__SendMsg", $arArgs)
If @error=1 Then Return SetError(@error, @extended, $bResult)
If @error Then Return SetError(@error, @extended, $bResult)
Return $bResult
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_MainSendCmd
; Description ...: Send an integer command (with data) to a sub process
; Syntax ........: __IPC_MainSendCmd($hProcess, $iCmd, [$data1 = Default, [$data2 = Default, [$data3 = Default, [$data4 = Default,
; [$data5 = Default, [$data6 = Default, [$data7 = Default, [$data8 = Default,
; [$data9 = Default, [$data10 = Default]]]]]]]]]])
; Parameters ....: $hProcess - the sub process handle
; $iCmd - integer as command
; $data1 - [optional] the data (if provided, it will be send, even if default)
; ... - ...
; $data10 - [optional] the data (if provided, it will be send, even if default)
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $hProcess, 2 - $iCmd)
; 2 - TCP Send failed
; 3 - Not connected to sub process
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_MainSendCmd($hProcess, $iCmd, $data1 = Default, $data2 = Default, $data3 = Default, $data4 = Default, $data5 = Default, _
$data6 = Default, $data7 = Default, $data8 = Default, $data9 = Default, $data10 = Default)
If Not IsInt($iCmd) Then Return SetError(1, 2, False)
Local $iProcess = __IPC__ProcessHandleToId($hProcess)
If @error Then Return SetError(1, 1, False)
Local $iSocket = $__IPC__Data.mServer.mProcesses[$iProcess].iSocket
If $iSocket=Default Then Return SetError(3, 0, False)
Local $arArgs[@NumParams+2]
$arArgs[0] = "CallArgArray"
$arArgs[1] = $iSocket
$arArgs[2] = $iCmd
For $i=2 To @NumParams-1
$arArgs[$i+1] = Eval("data"&($i-1))
Next
Local $bResult = Call("__IPC__SendMsg", $arArgs)
If @error=1 Then Return SetError(@error, @extended, $bResult)
If @error Then Return SetError(@error, @extended, $bResult)
Return $bResult
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_ProcessStop
; Description ...: Disconnect from a sub process. This triggers a call to the sub process $sExitCallback (if provided).
; Syntax ........: __IPC_ProcessStop($hProcess)
; Parameters ....: $hProcess - the sub process handle
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $hProcess)
; 2 - __IPC__SocketDisconnect failed. (@extended: @error from __IPC__SocketDisconnect)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_ProcessStop($hProcess)
Local $iProcess = __IPC__ProcessHandleToId($hProcess)
If @error Then Return SetError(1, 1, False)
__IPC__SocketDisconnect($__IPC__Data.mServer.mProcesses[$iProcess].iSocket)
If @error Then Return SetError(2, @error, False)
Return True
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_SubProcessing
; Description ...: Process messages for the sub process. Only call if the sub process pullrate $iPullRate was set to 0.
; Syntax ........: __IPC_SubProcessing()
; Parameters ....:
; Return values .:
; Author ........: Kanashius
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_SubProcessing()
__IPC__ProcessMessages()
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_MainProcessing
; Description ...: Process messages for the main process. Only call if the main process pullrate $iMainPullRate was set to 0.
; Syntax ........: __IPC_MainProcessing()
; Parameters ....:
; Return values .:
; Author ........: Kanashius
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_MainProcessing()
If $__IPC__Data.mServer.iOpenProcesses>0 Then __IPC__ServerAccept()
__IPC__ProcessMessages()
__IPC__ServerProcessStdOut()
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: __IPC_Log
; Description ...: Log a message to the console with the provided log level.
; Syntax ........: __IPC_Log($iLevel, $sMsg[, $iError=Default[, $iExtended = Default[, $hSubProcess = Default[, $iLine = @ScriptLineNumber]]]])
; Parameters ....: $iLevel - the sub process handle
; $sMsg - the string to print
; $iError - [optional] Default: None. An error if some should be printed.
; $iExtended - [optional] Default: None. Extended data if some should be printed.
; $hSubProcess - [optional] Default: None. The sub process, where the log belongs to.
; $iLine - [optional] Default: @ScriptLineNumber. The Scriptline to print
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $iLevel, 6 - $iLine)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC_Log($iLevel, $sMsg, $iError=Default, $iExtended = Default, $hSubProcess = Default, $iLine = @ScriptLineNumber)
; handle parameters
If Not IsInt($iLevel) Or $iLevel<0 Or $iLevel>$__IPC_LOG_TRACE Then Return SetError(1, 1, False)
If Not IsInt($iLine) Or $iLevel<0 Then Return SetError(1, 6, False)
If $iLevel>$__IPC__Data.iLogLevel Then Return False
; handle $iError and $iExtended
Local $sError = ""
If $iError<>Default And $iExtended<>Default Then
$sError = " [Error: "&$iError&", Extended: "&$iExtended&"]"
ElseIf $iError<>Default Then
$sError = " [Error: "&$iError&"]"
ElseIf $iExtended<>Default Then
$sError = " [Extended: "&$iExtended&"]"
EndIf
; create log prefix depending on log level
Local $sLevel
Switch $iLevel
Case $__IPC_LOG_FATAL
$sLevel = "FATAL"
Case $__IPC_LOG_ERROR
$sLevel = "ERROR"
Case $__IPC_LOG_WARN
$sLevel = "WARN "
Case $__IPC_LOG_INFO
$sLevel = "INFO "
Case $__IPC_LOG_DEBUG
$sLevel = "DEBUG"
Case $__IPC_LOG_TRACE
$sLevel = "TRACE"
EndSwitch
Local $arLines = StringSplit($sMsg, @CRLF, 3)
For $i=0 to UBound($arLines)-1 Step 1
If $hSubProcess<>Default Then
ConsoleWrite(">"&"Sub["&$hSubProcess&"] "&$sLevel&" ["&@YEAR&"/"&@MON&"/"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&"."&@MSEC&"] "&$arLines[$i])
Else
ConsoleWrite(">"&$sLevel&" ["&@YEAR&"/"&@MON&"/"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&"."&@MSEC&"] "&$arLines[$i])
EndIf
If $i=UBound($arLines)-1 Then ConsoleWrite($sError)
ConsoleWrite(@crlf)
Next
Return True
EndFunc
; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __IPC__SendMsg
; Description ...: Load or update the content of a treeview item to fill it with files/folders/drives
; Syntax ........: __IPC__SendMsg($iSocket[, $iCmdOrData[, $data = Default]])
; Parameters ....: $iSocket - the tcp socket
; $iCmd - [optional] the integer command or default for no command
; $data1 - [optional] the data (if provided, it will be send, even if default)
; ... - ...
; $data10 - [optional] the data (if provided, it will be send, even if default)
; Return values .: True on success.
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $iCmdOrData)
; 2 - TCPSend failed
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC__SendMsg($iSocket, $iCmd = Default, $data1 = Default, $data2 = Default, $data3 = Default, $data4 = Default, _
$data5 = Default, $data6 = Default, $data7 = Default, $data8 = Default, $data9 = Default, $data10 = Default)
; check if data is a string
Local $iDataParamCount = @NumParams-2
Local $dData = __IPC__ToBinary($iDataParamCount)
For $i=0 To $iDataParamCount-1
$dData &= __IPC__ToBinary(Eval("data"&$i+1))
If @error Then Return SetError(1, $i+2, False)
Next
Local $iMsg = $__IPC_MSG_DATA
; handle the cmd parameter
If $iCmd<>Default Then
; check cmd parameter if it is an integer
If Not IsInt($iCmd) Then Return SetError(1, 1, False)
$iMsg = $__IPC_MSG_CMD
If $iDataParamCount>0 Then $iMsg = $__IPC_MSG_CMD_DATA
; prepend the command to the data
$dData = __IPC__ToBinary($iCmd) & $dData
EndIf
; check the data length
Local $iLen = BinaryLen($dData)
; send the message binary data length as integer first
TCPSend($iSocket, Binary($iMsg)&Binary(Int($iLen, 2)))
If @error Then Return SetError(2, __IPC__SubDisconnect(True), False)
; send the data in $__IPC_MaxByteRecv sized packages
For $i=1 To $iLen Step $__IPC_MaxByteRecv
Local $dSend = BinaryMid($dData, $i, $__IPC_MaxByteRecv)
TCPSend($iSocket, $dSend)
If @error Then Return SetError(2, __IPC__SubDisconnect(True), False)
Next
Return True
EndFunc
; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __IPC__ToBinary
; Description ...: Convert variable to binary data
; Syntax ........: __IPC__ToBinary($data, $bPrefixed = True)
; Parameters ....: $data - the variable to convert to binary
; Return values .: The binary data on success
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; 1 - Parameter invalid (@extended: 1 - $iCmdOrData)
; 2 - Unknown datatype
; 3 - Could not be converted to binary
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC__ToBinary($data)
Local $bAddSize = False, $iDataType = 0, $dData = Default
Switch VarGetType($data)
Case "Binary"
$iDataType = $__IPC_DataType_Binary
$dData = $data
$bAddSize = True
Case "Bool"
$iDataType = $__IPC_DataType_Bool
$dData = Binary($data)
Case "UserFunction", "Function"
$iDataType = $__IPC_DataType_Func
$dData = StringToBinary(FuncName($data), 4)
$bAddSize = True
Case "Double"
$iDataType = $__IPC_DataType_Double
$dData = Binary($data)
Case "Ptr" ; => also handles
$iDataType = IsHWnd($data)?$__IPC_DataType_Hwnd:$__IPC_DataType_Ptr
$dData = Binary($data)
$bAddSize = True
Case "Int32"
$iDataType = $__IPC_DataType_Int32
$dData = Binary($data)
Case "Int64"
$iDataType = $__IPC_DataType_Int64
$dData = Binary($data)
Case "Object"
#cs
Local $iPid = _WinAPI_GetCurrentProcessID()
__IPC__ToBinary($iPid) ; add to binary
_WinAPI_DuplicateHandle()
Local $sCLSID = ObjName($data, $OBJ_PROGID); $OBJ_CLSID)
Local $sIID = ObjName($data, $OBJ_IID)
ConsoleWrite($sCLSID&@crlf)
ConsoleWrite($sIID&@crlf)
Local $oObj = ObjGet(ObjName($data, $OBJ_MODULE)) ; ObjCreateInterface($sCLSID, $sIID)
$oObj.add(3, "drei")
ConsoleWrite("---------1--------"&@crlf)
For $vKey In $data
ConsoleWrite($vKey&" > "&$data.Item($vKey) & " , ")
Next
ConsoleWrite(@crlf)
ConsoleWrite("---------2--------"&@crlf)
For $vKey In $oObj
ConsoleWrite($vKey&" > "&$oObj.Item($vKey) & " , ")
Next
ConsoleWrite(@crlf)
ConsoleWrite("-----------------"&@crlf)
#ce
Case "String"
$iDataType = $__IPC_DataType_String
$dData = StringToBinary($data, 2)
$bAddSize = True
Case "Keyword"
$iDataType = $__IPC_DataType_Keyword
$dData = StringToBinary($data, 4)
$bAddSize = True
Case "DLLStruct"
#cs
; ??? _WinAPI_DuplicateHandle()
; maybe see objcreate to share memory across processes
Local $iIndex = 1
While True
Local $elem = DllStructGetData($data, $iIndex)
If IsString($elem) Then
; iterate DllStructGetData($data, $iIndex, 1...) and retrieve every index seperately to get the size
EndIf
ConsoleWrite(VarGetType($elem)&" >> "&$elem&@crlf)
$iIndex += 1
If @error Then ExitLoop
WEnd
#ce
Case "Map"
$iDataType = $__IPC_DataType_Map
Local $arKeys = MapKeys($data)
Local $dData = __IPC__ToBinary(UBound($arKeys))
For $i=0 to UBound($arKeys)-1
$dData &= __IPC__ToBinary($arKeys[$i])
$dData &= __IPC__ToBinary($data[$arKeys[$i]])
Next
Case "Array"
$iDataType = $__IPC_DataType_Array
Local $iDim = UBound($data, 0)
$dData = __IPC__ToBinary($iDim)
Switch $iDim
Case 1
$dData &= __IPC__ToBinary(UBound($data))
For $i=0 to UBound($data)-1
$dData &= __IPC__ToBinary($data[$i])
Next
Case 2
$dData &= __IPC__ToBinary(UBound($data))
$dData &= __IPC__ToBinary(UBound($data, 2))
For $i=0 to UBound($data)-1
For $j=0 to UBound($data, 2)-1
$dData &= __IPC__ToBinary($data[$i][$j])
Next
Next
Case 3
$dData &= __IPC__ToBinary(UBound($data))
$dData &= __IPC__ToBinary(UBound($data, 2))
$dData &= __IPC__ToBinary(UBound($data, 3))
For $i=0 to UBound($data)-1
For $j=0 to UBound($data, 2)-1
For $k=0 to UBound($data, 3)-1
$dData &= __IPC__ToBinary($data[$i][$j][$k])
Next
Next
Next
EndSwitch
Case Else
Return SetError(2, 0, 0)
EndSwitch
If $dData=Default Then Return SetError(3, 0, 0)
If $bAddSize Then
Return Binary($iDataType)&__IPC__ToBinary(BinaryLen($dData))&$dData
Else
Return Binary($iDataType)&$dData
EndIf
EndFunc
; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __IPC__BinaryConsumeVar
; Description ...: Read a variable from binary data (and consume it from the binary data)
; Syntax ........: __IPC__BinaryConsumeVar(ByRef $dData)
; Parameters ....: $dData - the binary data
; Return values .: The variable with the correct type on success
; Author ........: Kanashius
; Modified ......:
; Remarks .......: Errors:
; ?
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __IPC__BinaryConsumeVar(ByRef $dData)
; todo error handling
Local $iDataType = Int(__IPC__BinaryConsumeBytes($dData, 4))
Switch $iDataType
Case $__IPC_DataType_Binary
Local $iSize = __IPC__BinaryConsumeVar($dData)
Return __IPC__BinaryConsumeBytes($dData, $iSize)
Case $__IPC_DataType_Bool
Return Int(__IPC__BinaryConsumeBytes($dData, 1))?(True):(False)
Case $__IPC_DataType_Func, $__IPC_DataType_Keyword
Local $iSize = __IPC__BinaryConsumeVar($dData)
Local $dBinaryVal = __IPC__BinaryConsumeBytes($dData, $iSize)
Local $sName = BinaryToString($dBinaryVal, 4)
Return Execute($sName)
Case $__IPC_DataType_Double
Local $dDouble = __IPC__BinaryConsumeBytes($dData, 8)
Local $tDoubleStruct = DllStructCreate("byte[8]")
DllStructSetData($tDoubleStruct, 1, $dDouble)
Return DllStructGetData(DllStructCreate("double", DllStructGetPtr($tDoubleStruct)), 1)
Case $__IPC_DataType_Ptr, $__IPC_DataType_Hwnd
Local $iSize = __IPC__BinaryConsumeVar($dData)
Local $dPtr = Ptr(Int(__IPC__BinaryConsumeBytes($dData, $iSize)))
If $iDataType=$__IPC_DataType_Hwnd Then Return HWnd($dPtr)
Return $dPtr
Case $__IPC_DataType_Int32
Return Int(__IPC__BinaryConsumeBytes($dData, 4), 1)
Case $__IPC_DataType_Int64
Return Int(__IPC__BinaryConsumeBytes($dData, 8), 2)
Case $__IPC_DataType_Object
; todo
;Local $iPid = __IPC__BinaryConsumeVar($dData)
;_WinAPI_GetProcessHandleCount
;_WinAPI_DuplicateHandle(?, ?, ?, 0, True, 2) ; 2 => DUPLICATE_SAME_ACCESS
Case $__IPC_DataType_String