-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAutoItObject.au3
More file actions
2054 lines (1937 loc) · 142 KB
/
AutoItObject.au3
File metadata and controls
2054 lines (1937 loc) · 142 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
; #INDEX# =======================================================================================================================
; Title .........: AutoItObject v1.2.8.4
; AutoIt Version : 3.3
; Language ......: English (language independent)
; Description ...: Brings Objects to AutoIt.
; Author(s) .....: monoceres, trancexx, Kip, Prog@ndy
; Copyright .....: Copyright (C) The AutoItObject-Team. All rights reserved.
; License .......: Artistic License 2.0, see Artistic.txt
;
; This file is part of AutoItObject.
;
; AutoItObject is free software; you can redistribute it and/or modify
; it under the terms of the Artistic License as published by Larry Wall,
; either version 2.0, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
; See the Artistic License for more details.
;
; You should have received a copy of the Artistic License with this Kit,
; in the file named "Artistic.txt". If not, you can get a copy from
; <http://www.perlfoundation.org/artistic_license_2_0> OR
; <http://www.opensource.org/licenses/artistic-license-2.0.php>
;
; ------------------------ AutoItObject CREDITS: ------------------------
; Copyright (C) by:
; The AutoItObject-Team:
; Andreas Karlsson (monoceres)
; Dragana R. (trancexx)
; Dave Bakker (Kip)
; Andreas Bosch (progandy, Prog@ndy)
;
; ===============================================================================================================================
#include-once
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
; #CURRENT# =====================================================================================================================
;_AutoItObject_AddDestructor
;_AutoItObject_AddEnum
;_AutoItObject_AddMethod
;_AutoItObject_AddProperty
;_AutoItObject_Class
;_AutoItObject_CLSIDFromString
;_AutoItObject_CoCreateInstance
;_AutoItObject_Create
;_AutoItObject_DllOpen
;_AutoItObject_DllStructCreate
;_AutoItObject_IDispatchToPtr
;_AutoItObject_IUnknownAddRef
;_AutoItObject_IUnknownRelease
;_AutoItObject_ObjCreate
;_AutoItObject_ObjCreateEx
;_AutoItObject_ObjectFromDtag
;_AutoItObject_PtrToIDispatch
;_AutoItObject_RegisterObject
;_AutoItObject_RemoveMember
;_AutoItObject_Shutdown
;_AutoItObject_Startup
;_AutoItObject_UnregisterObject
;_AutoItObject_VariantClear
;_AutoItObject_VariantCopy
;_AutoItObject_VariantFree
;_AutoItObject_VariantInit
;_AutoItObject_VariantRead
;_AutoItObject_VariantSet
;_AutoItObject_WrapperAddMethod
;_AutoItObject_WrapperCreate
; ===============================================================================================================================
; #INTERNAL_NO_DOC# =============================================================================================================
;__Au3Obj_OleUninitialize
;__Au3Obj_IUnknown_AddRef
;__Au3Obj_IUnknown_Release
;__Au3Obj_GetMethods
;__Au3Obj_SafeArrayCreate
;__Au3Obj_SafeArrayDestroy
;__Au3Obj_SafeArrayAccessData
;__Au3Obj_SafeArrayUnaccessData
;__Au3Obj_SafeArrayGetUBound
;__Au3Obj_SafeArrayGetLBound
;__Au3Obj_SafeArrayGetDim
;__Au3Obj_CreateSafeArrayVariant
;__Au3Obj_ReadSafeArrayVariant
;__Au3Obj_CoTaskMemAlloc
;__Au3Obj_CoTaskMemFree
;__Au3Obj_CoTaskMemRealloc
;__Au3Obj_GlobalAlloc
;__Au3Obj_GlobalFree
;__Au3Obj_SysAllocString
;__Au3Obj_SysCopyString
;__Au3Obj_SysReAllocString
;__Au3Obj_SysFreeString
;__Au3Obj_SysStringLen
;__Au3Obj_SysReadString
;__Au3Obj_PtrStringLen
;__Au3Obj_PtrStringRead
;__Au3Obj_FunctionProxy
;__Au3Obj_EnumFunctionProxy
;__Au3Obj_ObjStructGetElements
;__Au3Obj_ObjStructMethod
;__Au3Obj_ObjStructDestructor
;__Au3Obj_ObjStructPointer
;__Au3Obj_PointerCall
;__Au3Obj_Mem_DllOpen
;__Au3Obj_Mem_FixReloc
;__Au3Obj_Mem_FixImports
;__Au3Obj_Mem_LoadLibraryEx
;__Au3Obj_Mem_FreeLibrary
;__Au3Obj_Mem_GetAddress
;__Au3Obj_Mem_VirtualProtect
;__Au3Obj_Mem_Base64Decode
;__Au3Obj_Mem_BinDll
;__Au3Obj_Mem_BinDll_X64
; ===============================================================================================================================
; #DATATYPES# =====================================================================================================================
; none - no value (only valid for return type, equivalent to void in C)
; byte - an unsigned 8 bit integer
; boolean - an unsigned 8 bit integer
; short - a 16 bit integer
; word, ushort - an unsigned 16 bit integer
; int, long - a 32 bit integer
; bool - a 32 bit integer
; dword, ulong, uint - an unsigned 32 bit integer
; hresult - an unsigned 32 bit integer
; int64 - a 64 bit integer
; uint64 - an unsigned 64 bit integer
; ptr - a general pointer (void *)
; hwnd - a window handle (pointer wide)
; handle - an handle (pointer wide)
; float - a single precision floating point number
; double - a double precision floating point number
; int_ptr, long_ptr, lresult, lparam - an integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt
; uint_ptr, ulong_ptr, dword_ptr, wparam - an unsigned integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt
; str - an ANSI string (a minimum of 65536 chars is allocated)
; wstr - a UNICODE wide character string (a minimum of 65536 chars is allocated)
; bstr - a composite data type that consists of a length prefix, a data string and a terminator
; variant - a tagged union that can be used to represent any other data type
; idispatch, object - a composite data type that represents object with IDispatch interface
; ===============================================================================================================================
;--------------------------------------------------------------------------------------------------------------------------------------
#Region Variable definitions
Global Const $gh_AU3Obj_kernel32dll = DllOpen("kernel32.dll")
Global Const $gh_AU3Obj_oleautdll = DllOpen("oleaut32.dll")
Global Const $gh_AU3Obj_ole32dll = DllOpen("ole32.dll")
Global Const $__Au3Obj_X64 = @AutoItX64
Global Const $__Au3Obj_VT_EMPTY = 0
Global Const $__Au3Obj_VT_NULL = 1
Global Const $__Au3Obj_VT_I2 = 2
Global Const $__Au3Obj_VT_I4 = 3
Global Const $__Au3Obj_VT_R4 = 4
Global Const $__Au3Obj_VT_R8 = 5
Global Const $__Au3Obj_VT_CY = 6
Global Const $__Au3Obj_VT_DATE = 7
Global Const $__Au3Obj_VT_BSTR = 8
Global Const $__Au3Obj_VT_DISPATCH = 9
Global Const $__Au3Obj_VT_ERROR = 10
Global Const $__Au3Obj_VT_BOOL = 11
Global Const $__Au3Obj_VT_VARIANT = 12
Global Const $__Au3Obj_VT_UNKNOWN = 13
Global Const $__Au3Obj_VT_DECIMAL = 14
Global Const $__Au3Obj_VT_I1 = 16
Global Const $__Au3Obj_VT_UI1 = 17
Global Const $__Au3Obj_VT_UI2 = 18
Global Const $__Au3Obj_VT_UI4 = 19
Global Const $__Au3Obj_VT_I8 = 20
Global Const $__Au3Obj_VT_UI8 = 21
Global Const $__Au3Obj_VT_INT = 22
Global Const $__Au3Obj_VT_UINT = 23
Global Const $__Au3Obj_VT_VOID = 24
Global Const $__Au3Obj_VT_HRESULT = 25
Global Const $__Au3Obj_VT_PTR = 26
Global Const $__Au3Obj_VT_SAFEARRAY = 27
Global Const $__Au3Obj_VT_CARRAY = 28
Global Const $__Au3Obj_VT_USERDEFINED = 29
Global Const $__Au3Obj_VT_LPSTR = 30
Global Const $__Au3Obj_VT_LPWSTR = 31
Global Const $__Au3Obj_VT_RECORD = 36
Global Const $__Au3Obj_VT_INT_PTR = 37
Global Const $__Au3Obj_VT_UINT_PTR = 38
Global Const $__Au3Obj_VT_FILETIME = 64
Global Const $__Au3Obj_VT_BLOB = 65
Global Const $__Au3Obj_VT_STREAM = 66
Global Const $__Au3Obj_VT_STORAGE = 67
Global Const $__Au3Obj_VT_STREAMED_OBJECT = 68
Global Const $__Au3Obj_VT_STORED_OBJECT = 69
Global Const $__Au3Obj_VT_BLOB_OBJECT = 70
Global Const $__Au3Obj_VT_CF = 71
Global Const $__Au3Obj_VT_CLSID = 72
Global Const $__Au3Obj_VT_VERSIONED_STREAM = 73
Global Const $__Au3Obj_VT_BSTR_BLOB = 0xfff
Global Const $__Au3Obj_VT_VECTOR = 0x1000
Global Const $__Au3Obj_VT_ARRAY = 0x2000
Global Const $__Au3Obj_VT_BYREF = 0x4000
Global Const $__Au3Obj_VT_RESERVED = 0x8000
Global Const $__Au3Obj_VT_ILLEGAL = 0xffff
Global Const $__Au3Obj_VT_ILLEGALMASKED = 0xfff
Global Const $__Au3Obj_VT_TYPEMASK = 0xfff
Global Const $__Au3Obj_tagVARIANT = "word vt;word r1;word r2;word r3;ptr data; ptr"
Global Const $__Au3Obj_VARIANT_SIZE = DllStructGetSize(DllStructCreate($__Au3Obj_tagVARIANT, 1))
Global Const $__Au3Obj_PTR_SIZE = DllStructGetSize(DllStructCreate('ptr', 1))
Global Const $__Au3Obj_tagSAFEARRAYBOUND = "ulong cElements; long lLbound;"
Global $ghAutoItObjectDLL = -1, $giAutoItObjectDLLRef = 0
;===============================================================================
#interface "IUnknown"
Global Const $sIID_IUnknown = "{00000000-0000-0000-C000-000000000046}"
; Definition
Global $dtagIUnknown = "QueryInterface hresult(ptr;ptr*);" & _
"AddRef dword();" & _
"Release dword();"
; List
Global $ltagIUnknown = "QueryInterface;" & _
"AddRef;" & _
"Release;"
;===============================================================================
;===============================================================================
#interface "IDispatch"
Global Const $sIID_IDispatch = "{00020400-0000-0000-C000-000000000046}"
; Definition
Global $dtagIDispatch = $dtagIUnknown & _
"GetTypeInfoCount hresult(dword*);" & _
"GetTypeInfo hresult(dword;dword;ptr*);" & _
"GetIDsOfNames hresult(ptr;ptr;dword;dword;ptr);" & _
"Invoke hresult(dword;ptr;dword;word;ptr;ptr;ptr;ptr);"
; List
Global $ltagIDispatch = $ltagIUnknown & _
"GetTypeInfoCount;" & _
"GetTypeInfo;" & _
"GetIDsOfNames;" & _
"Invoke;"
;===============================================================================
#EndRegion Variable definitions
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region Misc
DllCall($gh_AU3Obj_ole32dll, 'long', 'OleInitialize', 'ptr', 0)
OnAutoItExitRegister("__Au3Obj_OleUninitialize")
Func __Au3Obj_OleUninitialize()
; Author: Prog@ndy
DllCall($gh_AU3Obj_ole32dll, 'long', 'OleUninitialize')
_AutoItObject_Shutdown(True)
EndFunc ;==>__Au3Obj_OleUninitialize
Func __Au3Obj_IUnknown_AddRef($vObj)
Local $sType = "ptr"
If IsObj($vObj) Then $sType = "idispatch"
Local $tVARIANT = DllStructCreate($__Au3Obj_tagVARIANT)
; Actual call
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "long", "DispCallFunc", _
$sType, $vObj, _
"dword", $__Au3Obj_PTR_SIZE, _ ; offset (4 for x86, 8 for x64)
"dword", 4, _ ; CC_STDCALL
"dword", $__Au3Obj_VT_UINT, _
"dword", 0, _ ; number of function parameters
"ptr", 0, _ ; parameters related
"ptr", 0, _ ; parameters related
"ptr", DllStructGetPtr($tVARIANT))
If @error Or $aCall[0] Then Return SetError(1, 0, 0)
; Collect returned
Return DllStructGetData(DllStructCreate("dword", DllStructGetPtr($tVARIANT, "data")), 1)
EndFunc ;==>__Au3Obj_IUnknown_AddRef
Func __Au3Obj_IUnknown_Release($vObj)
Local $sType = "ptr"
If IsObj($vObj) Then $sType = "idispatch"
Local $tVARIANT = DllStructCreate($__Au3Obj_tagVARIANT)
; Actual call
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "long", "DispCallFunc", _
$sType, $vObj, _
"dword", 2 * $__Au3Obj_PTR_SIZE, _ ; offset (8 for x86, 16 for x64)
"dword", 4, _ ; CC_STDCALL
"dword", $__Au3Obj_VT_UINT, _
"dword", 0, _ ; number of function parameters
"ptr", 0, _ ; parameters related
"ptr", 0, _ ; parameters related
"ptr", DllStructGetPtr($tVARIANT))
If @error Or $aCall[0] Then Return SetError(1, 0, 0)
; Collect returned
Return DllStructGetData(DllStructCreate("dword", DllStructGetPtr($tVARIANT, "data")), 1)
EndFunc ;==>__Au3Obj_IUnknown_Release
Func __Au3Obj_GetMethods($tagInterface)
Local $sMethods = StringReplace(StringRegExpReplace($tagInterface, "\h*(\w+)\h*(\w+\*?)\h*(\((.*?)\))\h*(;|;*\z)", "$1\|$2;$4" & @LF), ";" & @LF, @LF)
If $sMethods = $tagInterface Then $sMethods = StringReplace(StringRegExpReplace($tagInterface, "\h*(\w+)\h*(;|;*\z)", "$1\|" & @LF), ";" & @LF, @LF)
Return StringTrimRight($sMethods, 1)
EndFunc ;==>__Au3Obj_GetMethods
Func __Au3Obj_ObjStructGetElements($sTag, ByRef $sAlign)
Local $sAlignment = StringRegExpReplace($sTag, "\h*(align\h+\d+)\h*;.*", "$1")
If $sAlignment <> $sTag Then
$sAlign = $sAlignment
$sTag = StringRegExpReplace($sTag, "\h*(align\h+\d+)\h*;", "")
EndIf
; Return StringRegExp($sTag, "\h*\w+\h*(\w+)\h*", 3) ; DO NOT REMOVE THIS LINE
Return StringTrimRight(StringRegExpReplace($sTag, "\h*\w+\h*(\w+)\h*(\[\d+\])*\h*(;|;*\z)\h*", "$1;"), 1)
EndFunc ;==>__Au3Obj_ObjStructGetElements
#EndRegion Misc
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region SafeArray
Func __Au3Obj_SafeArrayCreate($vType, $cDims, $rgsabound)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "ptr", "SafeArrayCreate", "dword", $vType, "uint", $cDims, 'ptr', $rgsabound)
If @error Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_SafeArrayCreate
Func __Au3Obj_SafeArrayDestroy($pSafeArray)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "int", "SafeArrayDestroy", "ptr", $pSafeArray)
If @error Then Return SetError(1, 0, 1)
Return $aCall[0]
EndFunc ;==>__Au3Obj_SafeArrayDestroy
Func __Au3Obj_SafeArrayAccessData($pSafeArray, ByRef $pArrayData)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "int", "SafeArrayAccessData", "ptr", $pSafeArray, 'ptr*', 0)
If @error Then Return SetError(1, 0, 1)
$pArrayData = $aCall[2]
Return $aCall[0]
EndFunc ;==>__Au3Obj_SafeArrayAccessData
Func __Au3Obj_SafeArrayUnaccessData($pSafeArray)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "int", "SafeArrayUnaccessData", "ptr", $pSafeArray)
If @error Then Return SetError(1, 0, 1)
Return $aCall[0]
EndFunc ;==>__Au3Obj_SafeArrayUnaccessData
Func __Au3Obj_SafeArrayGetUBound($pSafeArray, $iDim, ByRef $iBound)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "int", "SafeArrayGetUBound", "ptr", $pSafeArray, 'uint', $iDim, 'long*', 0)
If @error Then Return SetError(1, 0, 1)
$iBound = $aCall[3]
Return $aCall[0]
EndFunc ;==>__Au3Obj_SafeArrayGetUBound
Func __Au3Obj_SafeArrayGetLBound($pSafeArray, $iDim, ByRef $iBound)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "int", "SafeArrayGetLBound", "ptr", $pSafeArray, 'uint', $iDim, 'long*', 0)
If @error Then Return SetError(1, 0, 1)
$iBound = $aCall[3]
Return $aCall[0]
EndFunc ;==>__Au3Obj_SafeArrayGetLBound
Func __Au3Obj_SafeArrayGetDim($pSafeArray)
Local $aResult = DllCall($gh_AU3Obj_oleautdll, "uint", "SafeArrayGetDim", "ptr", $pSafeArray)
If @error Then Return SetError(1, 0, 0)
Return $aResult[0]
EndFunc ;==>__Au3Obj_SafeArrayGetDim
Func __Au3Obj_CreateSafeArrayVariant(ByRef Const $aArray)
; Author: Prog@ndy
Local $iDim = UBound($aArray, 0), $pData, $pSafeArray, $bound, $subBound, $tBound
Switch $iDim
Case 1
$bound = UBound($aArray) - 1
$tBound = DllStructCreate($__Au3Obj_tagSAFEARRAYBOUND)
DllStructSetData($tBound, 1, $bound + 1)
$pSafeArray = __Au3Obj_SafeArrayCreate($__Au3Obj_VT_VARIANT, 1, DllStructGetPtr($tBound))
If 0 = __Au3Obj_SafeArrayAccessData($pSafeArray, $pData) Then
For $i = 0 To $bound
_AutoItObject_VariantInit($pData + $i * $__Au3Obj_VARIANT_SIZE)
_AutoItObject_VariantSet($pData + $i * $__Au3Obj_VARIANT_SIZE, $aArray[$i])
Next
__Au3Obj_SafeArrayUnaccessData($pSafeArray)
EndIf
Return $pSafeArray
Case 2
$bound = UBound($aArray, 1) - 1
$subBound = UBound($aArray, 2) - 1
$tBound = DllStructCreate($__Au3Obj_tagSAFEARRAYBOUND & $__Au3Obj_tagSAFEARRAYBOUND)
DllStructSetData($tBound, 3, $bound + 1)
DllStructSetData($tBound, 1, $subBound + 1)
$pSafeArray = __Au3Obj_SafeArrayCreate($__Au3Obj_VT_VARIANT, 2, DllStructGetPtr($tBound))
If 0 = __Au3Obj_SafeArrayAccessData($pSafeArray, $pData) Then
For $i = 0 To $bound
For $j = 0 To $subBound
_AutoItObject_VariantInit($pData + ($j + $i * ($subBound + 1)) * $__Au3Obj_VARIANT_SIZE)
_AutoItObject_VariantSet($pData + ($j + $i * ($subBound + 1)) * $__Au3Obj_VARIANT_SIZE, $aArray[$i][$j])
Next
Next
__Au3Obj_SafeArrayUnaccessData($pSafeArray)
EndIf
Return $pSafeArray
Case Else
Return 0
EndSwitch
EndFunc ;==>__Au3Obj_CreateSafeArrayVariant
Func __Au3Obj_ReadSafeArrayVariant($pSafeArray)
; Author: Prog@ndy
Local $iDim = __Au3Obj_SafeArrayGetDim($pSafeArray), $pData, $lbound, $bound, $subBound
Switch $iDim
Case 1
__Au3Obj_SafeArrayGetLBound($pSafeArray, 1, $lbound)
__Au3Obj_SafeArrayGetUBound($pSafeArray, 1, $bound)
$bound -= $lbound
Local $array[$bound + 1]
If 0 = __Au3Obj_SafeArrayAccessData($pSafeArray, $pData) Then
For $i = 0 To $bound
$array[$i] = _AutoItObject_VariantRead($pData + $i * $__Au3Obj_VARIANT_SIZE)
Next
__Au3Obj_SafeArrayUnaccessData($pSafeArray)
EndIf
Return $array
Case 2
__Au3Obj_SafeArrayGetLBound($pSafeArray, 2, $lbound)
__Au3Obj_SafeArrayGetUBound($pSafeArray, 2, $bound)
$bound -= $lbound
__Au3Obj_SafeArrayGetLBound($pSafeArray, 1, $lbound)
__Au3Obj_SafeArrayGetUBound($pSafeArray, 1, $subBound)
$subBound -= $lbound
Local $array[$bound + 1][$subBound + 1]
If 0 = __Au3Obj_SafeArrayAccessData($pSafeArray, $pData) Then
For $i = 0 To $bound
For $j = 0 To $subBound
$array[$i][$j] = _AutoItObject_VariantRead($pData + ($j + $i * ($subBound + 1)) * $__Au3Obj_VARIANT_SIZE)
Next
Next
__Au3Obj_SafeArrayUnaccessData($pSafeArray)
EndIf
Return $array
Case Else
Return 0
EndSwitch
EndFunc ;==>__Au3Obj_ReadSafeArrayVariant
#EndRegion SafeArray
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region Memory
Func __Au3Obj_CoTaskMemAlloc($iSize)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_ole32dll, "ptr", "CoTaskMemAlloc", "uint_ptr", $iSize)
If @error Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_CoTaskMemAlloc
Func __Au3Obj_CoTaskMemFree($pCoMem)
; Author: Prog@ndy
DllCall($gh_AU3Obj_ole32dll, "none", "CoTaskMemFree", "ptr", $pCoMem)
If @error Then Return SetError(1, 0, 0)
EndFunc ;==>__Au3Obj_CoTaskMemFree
Func __Au3Obj_CoTaskMemRealloc($pCoMem, $iSize)
; Author: Prog@ndy
Local $aCall = DllCall($gh_AU3Obj_ole32dll, "ptr", "CoTaskMemRealloc", 'ptr', $pCoMem, "uint_ptr", $iSize)
If @error Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_CoTaskMemRealloc
Func __Au3Obj_GlobalAlloc($iSize, $iFlag)
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "ptr", "GlobalAlloc", "dword", $iFlag, "dword_ptr", $iSize)
If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_GlobalAlloc
Func __Au3Obj_GlobalFree($pPointer)
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "ptr", "GlobalFree", "ptr", $pPointer)
If @error Or $aCall[0] Then Return SetError(1, 0, 0)
Return 1
EndFunc ;==>__Au3Obj_GlobalFree
#EndRegion Memory
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region SysString
Func __Au3Obj_SysAllocString($str)
; Author: monoceres
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "ptr", "SysAllocString", "wstr", $str)
If @error Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_SysAllocString
Func __Au3Obj_SysCopyString($pBSTR)
; Author: Prog@ndy
If Not $pBSTR Then Return SetError(2, 0, 0)
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "ptr", "SysAllocStringLen", "ptr", $pBSTR, "uint", __Au3Obj_SysStringLen($pBSTR))
If @error Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_SysCopyString
Func __Au3Obj_SysReAllocString(ByRef $pBSTR, $str)
; Author: Prog@ndy
If Not $pBSTR Then Return SetError(2, 0, 0)
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "int", "SysReAllocString", 'ptr*', $pBSTR, "wstr", $str)
If @error Then Return SetError(1, 0, 0)
$pBSTR = $aCall[1]
Return $aCall[0]
EndFunc ;==>__Au3Obj_SysReAllocString
Func __Au3Obj_SysFreeString($pBSTR)
; Author: Prog@ndy
If Not $pBSTR Then Return SetError(2, 0, 0)
DllCall($gh_AU3Obj_oleautdll, "none", "SysFreeString", "ptr", $pBSTR)
If @error Then Return SetError(1, 0, 0)
EndFunc ;==>__Au3Obj_SysFreeString
Func __Au3Obj_SysStringLen($pBSTR)
; Author: Prog@ndy
If Not $pBSTR Then Return SetError(2, 0, 0)
Local $aCall = DllCall($gh_AU3Obj_oleautdll, "uint", "SysStringLen", "ptr", $pBSTR)
If @error Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_SysStringLen
Func __Au3Obj_SysReadString($pBSTR, $iLen = -1)
; Author: Prog@ndy
If Not $pBSTR Then Return SetError(2, 0, '')
If $iLen < 1 Then $iLen = __Au3Obj_SysStringLen($pBSTR)
If $iLen < 1 Then Return SetError(1, 0, '')
Return DllStructGetData(DllStructCreate("wchar[" & $iLen & "]", $pBSTR), 1)
EndFunc ;==>__Au3Obj_SysReadString
Func __Au3Obj_PtrStringLen($pStr)
; Author: Prog@ndy
Local $aResult = DllCall($gh_AU3Obj_kernel32dll, 'int', 'lstrlenW', 'ptr', $pStr)
If @error Then Return SetError(1, 0, 0)
Return $aResult[0]
EndFunc ;==>__Au3Obj_PtrStringLen
Func __Au3Obj_PtrStringRead($pStr, $iLen = -1)
; Author: Prog@ndy
If $iLen < 1 Then $iLen = __Au3Obj_PtrStringLen($pStr)
If $iLen < 1 Then Return SetError(1, 0, '')
Return DllStructGetData(DllStructCreate("wchar[" & $iLen & "]", $pStr), 1)
EndFunc ;==>__Au3Obj_PtrStringRead
#EndRegion SysString
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region Proxy Functions
Func __Au3Obj_FunctionProxy($FuncName, $oSelf) ; allows binary code to call autoit functions
Local $arg = $oSelf.__params__ ; fetch params
If IsArray($arg) Then
Local $ret = Call($FuncName, $arg) ; Call
If @error = 0xDEAD And @extended = 0xBEEF Then Return 0
$oSelf.__error__ = @error ; set error
$oSelf.__result__ = $ret ; set result
Return 1
EndIf
; return error when params-array could not be created
EndFunc ;==>__Au3Obj_FunctionProxy
Func __Au3Obj_EnumFunctionProxy($iAction, $FuncName, $oSelf, $pVarCurrent, $pVarResult)
Local $Current, $ret
Switch $iAction
Case 0 ; Next
$Current = $oSelf.__bridge__(Number($pVarCurrent))
$ret = Execute($FuncName & "($oSelf, $Current)")
If @error Then Return False
$oSelf.__bridge__(Number($pVarCurrent)) = $Current
$oSelf.__bridge__(Number($pVarResult)) = $ret
Return 1
Case 1 ;Skip
Return False
Case 2 ; Reset
$Current = $oSelf.__bridge__(Number($pVarCurrent))
$ret = Execute($FuncName & "($oSelf, $Current)")
If @error Or Not $ret Then Return False
$oSelf.__bridge__(Number($pVarCurrent)) = $Current
Return True
EndSwitch
EndFunc ;==>__Au3Obj_EnumFunctionProxy
#EndRegion Proxy Functions
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region Call Pointer
Func __Au3Obj_PointerCall($sRetType, $pAddress, $sType1 = "", $vParam1 = 0, $sType2 = "", $vParam2 = 0, $sType3 = "", $vParam3 = 0, $sType4 = "", $vParam4 = 0, $sType5 = "", $vParam5 = 0, $sType6 = "", $vParam6 = 0, $sType7 = "", $vParam7 = 0, $sType8 = "", $vParam8 = 0, $sType9 = "", $vParam9 = 0, $sType10 = "", $vParam10 = 0, $sType11 = "", $vParam11 = 0, $sType12 = "", $vParam12 = 0, $sType13 = "", $vParam13 = 0, $sType14 = "", $vParam14 = 0, $sType15 = "", $vParam15 = 0, $sType16 = "", $vParam16 = 0, $sType17 = "", $vParam17 = 0, $sType18 = "", $vParam18 = 0, $sType19 = "", $vParam19 = 0, $sType20 = "", $vParam20 = 0)
; Author: Ward, Prog@ndy, trancexx
Local Static $pHook, $hPseudo, $tPtr, $sFuncName = "MemoryCallEntry"
If $pAddress Then
If Not $pHook Then
Local $sDll = "AutoItObject.dll"
If $__Au3Obj_X64 Then $sDll = "AutoItObject_X64.dll"
$hPseudo = DllOpen($sDll)
If $hPseudo = -1 Then
$sDll = "kernel32.dll"
$sFuncName = "GlobalFix"
$hPseudo = DllOpen($sDll)
EndIf
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "ptr", "GetModuleHandleW", "wstr", $sDll)
If @error Or Not $aCall[0] Then Return SetError(7, @error, 0) ; Couldn't get dll handle
Local $hModuleHandle = $aCall[0]
$aCall = DllCall($gh_AU3Obj_kernel32dll, "ptr", "GetProcAddress", "ptr", $hModuleHandle, "str", $sFuncName)
If @error Then Return SetError(8, @error, 0) ; Wanted function not found
$pHook = $aCall[0]
$aCall = DllCall($gh_AU3Obj_kernel32dll, "bool", "VirtualProtect", "ptr", $pHook, "dword", 7 + 5 * $__Au3Obj_X64, "dword", 64, "dword*", 0)
If @error Or Not $aCall[0] Then Return SetError(9, @error, 0) ; Unable to set MEM_EXECUTE_READWRITE
If $__Au3Obj_X64 Then
DllStructSetData(DllStructCreate("word", $pHook), 1, 0xB848)
DllStructSetData(DllStructCreate("word", $pHook + 10), 1, 0xE0FF)
Else
DllStructSetData(DllStructCreate("byte", $pHook), 1, 0xB8)
DllStructSetData(DllStructCreate("word", $pHook + 5), 1, 0xE0FF)
EndIf
$tPtr = DllStructCreate("ptr", $pHook + 1 + $__Au3Obj_X64)
EndIf
DllStructSetData($tPtr, 1, $pAddress)
Local $aRet
Switch @NumParams
Case 2
$aRet = DllCall($hPseudo, $sRetType, $sFuncName)
Case 4
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1)
Case 6
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2)
Case 8
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3)
Case 10
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4)
Case 12
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5)
Case 14
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6)
Case 16
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7)
Case 18
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8)
Case 20
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9)
Case 22
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10)
Case 24
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11)
Case 26
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12)
Case 28
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13)
Case 30
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14)
Case 32
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14, $sType15, $vParam15)
Case 34
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14, $sType15, $vParam15, $sType16, $vParam16)
Case 36
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14, $sType15, $vParam15, $sType16, $vParam16, $sType17, $vParam17)
Case 38
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14, $sType15, $vParam15, $sType16, $vParam16, $sType17, $vParam17, $sType18, $vParam18)
Case 40
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14, $sType15, $vParam15, $sType16, $vParam16, $sType17, $vParam17, $sType18, $vParam18, $sType19, $vParam19)
Case 42
$aRet = DllCall($hPseudo, $sRetType, $sFuncName, $sType1, $vParam1, $sType2, $vParam2, $sType3, $vParam3, $sType4, $vParam4, $sType5, $vParam5, $sType6, $vParam6, $sType7, $vParam7, $sType8, $vParam8, $sType9, $vParam9, $sType10, $vParam10, $sType11, $vParam11, $sType12, $vParam12, $sType13, $vParam13, $sType14, $vParam14, $sType15, $vParam15, $sType16, $vParam16, $sType17, $vParam17, $sType18, $vParam18, $sType19, $vParam19, $sType20, $vParam20)
Case Else
If Mod(@NumParams, 2) Then Return SetError(4, 0, 0) ; Bad number of parameters
Return SetError(5, 0, 0) ; Max number of parameters exceeded
EndSwitch
Return SetError(@error, @extended, $aRet) ; All went well. Error description and return values like with DllCall()
EndIf
Return SetError(6, 0, 0) ; Null address specified
EndFunc ;==>__Au3Obj_PointerCall
#EndRegion Call Pointer
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
#Region Embedded DLL
Func __Au3Obj_Mem_DllOpen($bBinaryImage = 0, $sSubrogor = "cmd.exe")
If Not $bBinaryImage Then
If $__Au3Obj_X64 Then
$bBinaryImage = __Au3Obj_Mem_BinDll_X64()
Else
$bBinaryImage = __Au3Obj_Mem_BinDll()
EndIf
EndIf
; Make structure out of binary data that was passed
Local $tBinary = DllStructCreate("byte[" & BinaryLen($bBinaryImage) & "]")
DllStructSetData($tBinary, 1, $bBinaryImage) ; fill the structure
; Get pointer to it
Local $pPointer = DllStructGetPtr($tBinary)
; Start processing passed binary data. 'Reading' PE format follows.
Local $tIMAGE_DOS_HEADER = DllStructCreate("char Magic[2];" & _
"word BytesOnLastPage;" & _
"word Pages;" & _
"word Relocations;" & _
"word SizeofHeader;" & _
"word MinimumExtra;" & _
"word MaximumExtra;" & _
"word SS;" & _
"word SP;" & _
"word Checksum;" & _
"word IP;" & _
"word CS;" & _
"word Relocation;" & _
"word Overlay;" & _
"char Reserved[8];" & _
"word OEMIdentifier;" & _
"word OEMInformation;" & _
"char Reserved2[20];" & _
"dword AddressOfNewExeHeader", _
$pPointer)
; Move pointer
$pPointer += DllStructGetData($tIMAGE_DOS_HEADER, "AddressOfNewExeHeader") ; move to PE file header
$pPointer += 4 ; size of skipped $tIMAGE_NT_SIGNATURE structure
; In place of IMAGE_FILE_HEADER structure
Local $tIMAGE_FILE_HEADER = DllStructCreate("word Machine;" & _
"word NumberOfSections;" & _
"dword TimeDateStamp;" & _
"dword PointerToSymbolTable;" & _
"dword NumberOfSymbols;" & _
"word SizeOfOptionalHeader;" & _
"word Characteristics", _
$pPointer)
; Get number of sections
Local $iNumberOfSections = DllStructGetData($tIMAGE_FILE_HEADER, "NumberOfSections")
; Move pointer
$pPointer += 20 ; size of $tIMAGE_FILE_HEADER structure
; Determine the type
Local $tMagic = DllStructCreate("word Magic;", $pPointer)
Local $iMagic = DllStructGetData($tMagic, 1)
Local $tIMAGE_OPTIONAL_HEADER
If $iMagic = 267 Then ; x86 version
If $__Au3Obj_X64 Then Return SetError(1, 0, -1) ; incompatible versions
$tIMAGE_OPTIONAL_HEADER = DllStructCreate("word Magic;" & _
"byte MajorLinkerVersion;" & _
"byte MinorLinkerVersion;" & _
"dword SizeOfCode;" & _
"dword SizeOfInitializedData;" & _
"dword SizeOfUninitializedData;" & _
"dword AddressOfEntryPoint;" & _
"dword BaseOfCode;" & _
"dword BaseOfData;" & _
"dword ImageBase;" & _
"dword SectionAlignment;" & _
"dword FileAlignment;" & _
"word MajorOperatingSystemVersion;" & _
"word MinorOperatingSystemVersion;" & _
"word MajorImageVersion;" & _
"word MinorImageVersion;" & _
"word MajorSubsystemVersion;" & _
"word MinorSubsystemVersion;" & _
"dword Win32VersionValue;" & _
"dword SizeOfImage;" & _
"dword SizeOfHeaders;" & _
"dword CheckSum;" & _
"word Subsystem;" & _
"word DllCharacteristics;" & _
"dword SizeOfStackReserve;" & _
"dword SizeOfStackCommit;" & _
"dword SizeOfHeapReserve;" & _
"dword SizeOfHeapCommit;" & _
"dword LoaderFlags;" & _
"dword NumberOfRvaAndSizes", _
$pPointer)
; Move pointer
$pPointer += 96 ; size of $tIMAGE_OPTIONAL_HEADER
ElseIf $iMagic = 523 Then ; x64 version
If Not $__Au3Obj_X64 Then Return SetError(1, 0, -1) ; incompatible versions
$tIMAGE_OPTIONAL_HEADER = DllStructCreate("word Magic;" & _
"byte MajorLinkerVersion;" & _
"byte MinorLinkerVersion;" & _
"dword SizeOfCode;" & _
"dword SizeOfInitializedData;" & _
"dword SizeOfUninitializedData;" & _
"dword AddressOfEntryPoint;" & _
"dword BaseOfCode;" & _
"uint64 ImageBase;" & _
"dword SectionAlignment;" & _
"dword FileAlignment;" & _
"word MajorOperatingSystemVersion;" & _
"word MinorOperatingSystemVersion;" & _
"word MajorImageVersion;" & _
"word MinorImageVersion;" & _
"word MajorSubsystemVersion;" & _
"word MinorSubsystemVersion;" & _
"dword Win32VersionValue;" & _
"dword SizeOfImage;" & _
"dword SizeOfHeaders;" & _
"dword CheckSum;" & _
"word Subsystem;" & _
"word DllCharacteristics;" & _
"uint64 SizeOfStackReserve;" & _
"uint64 SizeOfStackCommit;" & _
"uint64 SizeOfHeapReserve;" & _
"uint64 SizeOfHeapCommit;" & _
"dword LoaderFlags;" & _
"dword NumberOfRvaAndSizes", _
$pPointer)
; Move pointer
$pPointer += 112 ; size of $tIMAGE_OPTIONAL_HEADER
Else
Return SetError(1, 0, -1) ; incompatible versions
EndIf
; Extract data
Local $iEntryPoint = DllStructGetData($tIMAGE_OPTIONAL_HEADER, "AddressOfEntryPoint") ; if loaded binary image would start executing at this address
Local $pOptionalHeaderImageBase = DllStructGetData($tIMAGE_OPTIONAL_HEADER, "ImageBase") ; address of the first byte of the image when it's loaded in memory
$pPointer += 8 ; skipping IMAGE_DIRECTORY_ENTRY_EXPORT
; Import Directory
Local $tIMAGE_DIRECTORY_ENTRY_IMPORT = DllStructCreate("dword VirtualAddress; dword Size", $pPointer)
; Collect data
Local $pAddressImport = DllStructGetData($tIMAGE_DIRECTORY_ENTRY_IMPORT, "VirtualAddress")
;~ Local $iSizeImport = DllStructGetData($tIMAGE_DIRECTORY_ENTRY_IMPORT, "Size")
$pPointer += 8 ; size of $tIMAGE_DIRECTORY_ENTRY_IMPORT
$pPointer += 24 ; skipping IMAGE_DIRECTORY_ENTRY_RESOURCE, IMAGE_DIRECTORY_ENTRY_EXCEPTION, IMAGE_DIRECTORY_ENTRY_SECURITY
; Base Relocation Directory
Local $tIMAGE_DIRECTORY_ENTRY_BASERELOC = DllStructCreate("dword VirtualAddress; dword Size", $pPointer)
; Collect data
Local $pAddressNewBaseReloc = DllStructGetData($tIMAGE_DIRECTORY_ENTRY_BASERELOC, "VirtualAddress")
Local $iSizeBaseReloc = DllStructGetData($tIMAGE_DIRECTORY_ENTRY_BASERELOC, "Size")
$pPointer += 8 ; size of IMAGE_DIRECTORY_ENTRY_BASERELOC
$pPointer += 40 ; skipping IMAGE_DIRECTORY_ENTRY_DEBUG, IMAGE_DIRECTORY_ENTRY_COPYRIGHT, IMAGE_DIRECTORY_ENTRY_GLOBALPTR, IMAGE_DIRECTORY_ENTRY_TLS, IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
$pPointer += 40 ; five more generally unused data directories
; Load the victim
Local $pBaseAddress = __Au3Obj_Mem_LoadLibraryEx($sSubrogor, 1) ; "lighter" loading, DONT_RESOLVE_DLL_REFERENCES
If @error Then Return SetError(2, 0, -1) ; Couldn't load subrogor
Local $pHeadersNew = DllStructGetPtr($tIMAGE_DOS_HEADER) ; starting address of binary image headers
Local $iOptionalHeaderSizeOfHeaders = DllStructGetData($tIMAGE_OPTIONAL_HEADER, "SizeOfHeaders") ; the size of the MS-DOS stub, the PE header, and the section headers
; Set proper memory protection for writting headers (PAGE_READWRITE)
If Not __Au3Obj_Mem_VirtualProtect($pBaseAddress, $iOptionalHeaderSizeOfHeaders, 4) Then Return SetError(3, 0, -1) ; Couldn't set proper protection for headers
; Write NEW headers
DllStructSetData(DllStructCreate("byte[" & $iOptionalHeaderSizeOfHeaders & "]", $pBaseAddress), 1, DllStructGetData(DllStructCreate("byte[" & $iOptionalHeaderSizeOfHeaders & "]", $pHeadersNew), 1))
; Dealing with sections. Will write them.
Local $tIMAGE_SECTION_HEADER
Local $iSizeOfRawData, $pPointerToRawData
Local $iVirtualSize, $iVirtualAddress
Local $pRelocRaw
For $i = 1 To $iNumberOfSections
$tIMAGE_SECTION_HEADER = DllStructCreate("char Name[8];" & _
"dword UnionOfVirtualSizeAndPhysicalAddress;" & _
"dword VirtualAddress;" & _
"dword SizeOfRawData;" & _
"dword PointerToRawData;" & _
"dword PointerToRelocations;" & _
"dword PointerToLinenumbers;" & _
"word NumberOfRelocations;" & _
"word NumberOfLinenumbers;" & _
"dword Characteristics", _
$pPointer)
; Collect data
$iSizeOfRawData = DllStructGetData($tIMAGE_SECTION_HEADER, "SizeOfRawData")
$pPointerToRawData = $pHeadersNew + DllStructGetData($tIMAGE_SECTION_HEADER, "PointerToRawData")
$iVirtualAddress = DllStructGetData($tIMAGE_SECTION_HEADER, "VirtualAddress")
$iVirtualSize = DllStructGetData($tIMAGE_SECTION_HEADER, "UnionOfVirtualSizeAndPhysicalAddress")
If $iVirtualSize And $iVirtualSize < $iSizeOfRawData Then $iSizeOfRawData = $iVirtualSize
; Set MEM_EXECUTE_READWRITE for sections (PAGE_EXECUTE_READWRITE for all for simplicity)
If Not __Au3Obj_Mem_VirtualProtect($pBaseAddress + $iVirtualAddress, $iVirtualSize, 64) Then
$pPointer += 40 ; size of $tIMAGE_SECTION_HEADER structure
ContinueLoop
EndIf
; Clean the space
DllStructSetData(DllStructCreate("byte[" & $iVirtualSize & "]", $pBaseAddress + $iVirtualAddress), 1, DllStructGetData(DllStructCreate("byte[" & $iVirtualSize & "]"), 1))
; If there is data to write, write it
If $iSizeOfRawData Then DllStructSetData(DllStructCreate("byte[" & $iSizeOfRawData & "]", $pBaseAddress + $iVirtualAddress), 1, DllStructGetData(DllStructCreate("byte[" & $iSizeOfRawData & "]", $pPointerToRawData), 1))
; Relocations
If $iVirtualAddress <= $pAddressNewBaseReloc And $iVirtualAddress + $iSizeOfRawData > $pAddressNewBaseReloc Then $pRelocRaw = $pPointerToRawData + ($pAddressNewBaseReloc - $iVirtualAddress)
; Imports
If $iVirtualAddress <= $pAddressImport And $iVirtualAddress + $iSizeOfRawData > $pAddressImport Then __Au3Obj_Mem_FixImports($pPointerToRawData + ($pAddressImport - $iVirtualAddress), $pBaseAddress) ; fix imports in place
; Move pointer
$pPointer += 40 ; size of $tIMAGE_SECTION_HEADER structure
Next
; Fix relocations
If $pAddressNewBaseReloc And $iSizeBaseReloc Then __Au3Obj_Mem_FixReloc($pRelocRaw, $iSizeBaseReloc, $pBaseAddress, $pOptionalHeaderImageBase, $iMagic = 523)
; Entry point address
Local $pEntryFunc = $pBaseAddress + $iEntryPoint
; DllMain simulation
__Au3Obj_PointerCall("bool", $pEntryFunc, "ptr", $pBaseAddress, "dword", 1, "ptr", 0) ; DLL_PROCESS_ATTACH
; Get pseudo-handle
Local $hPseudo = DllOpen($sSubrogor)
__Au3Obj_Mem_FreeLibrary($pBaseAddress) ; decrement reference count
Return $hPseudo
EndFunc ;==>__Au3Obj_Mem_DllOpen
Func __Au3Obj_Mem_FixReloc($pData, $iSize, $pAddressNew, $pAddressOld, $fImageX64)
Local $iDelta = $pAddressNew - $pAddressOld ; dislocation value
Local $tIMAGE_BASE_RELOCATION, $iRelativeMove
Local $iVirtualAddress, $iSizeofBlock, $iNumberOfEntries
Local $tEnries, $iData, $tAddress
Local $iFlag = 3 + 7 * $fImageX64 ; IMAGE_REL_BASED_HIGHLOW = 3 or IMAGE_REL_BASED_DIR64 = 10
While $iRelativeMove < $iSize ; for all data available
$tIMAGE_BASE_RELOCATION = DllStructCreate("dword VirtualAddress; dword SizeOfBlock", $pData + $iRelativeMove)
$iVirtualAddress = DllStructGetData($tIMAGE_BASE_RELOCATION, "VirtualAddress")
$iSizeofBlock = DllStructGetData($tIMAGE_BASE_RELOCATION, "SizeOfBlock")
$iNumberOfEntries = ($iSizeofBlock - 8) / 2
$tEnries = DllStructCreate("word[" & $iNumberOfEntries & "]", DllStructGetPtr($tIMAGE_BASE_RELOCATION) + 8)
; Go through all entries
For $i = 1 To $iNumberOfEntries
$iData = DllStructGetData($tEnries, 1, $i)
If BitShift($iData, 12) = $iFlag Then ; check type
$tAddress = DllStructCreate("ptr", $pAddressNew + $iVirtualAddress + BitAND($iData, 0xFFF)) ; the rest of $iData is offset
DllStructSetData($tAddress, 1, DllStructGetData($tAddress, 1) + $iDelta) ; this is what's this all about
EndIf
Next
$iRelativeMove += $iSizeofBlock
WEnd
Return 1 ; all OK!
EndFunc ;==>__Au3Obj_Mem_FixReloc
Func __Au3Obj_Mem_FixImports($pImportDirectory, $hInstance)
Local $hModule, $tFuncName, $sFuncName, $pFuncAddress
Local $tIMAGE_IMPORT_MODULE_DIRECTORY, $tModuleName
Local $tBufferOffset2, $iBufferOffset2
Local $iInitialOffset, $iInitialOffset2, $iOffset
While 1
$tIMAGE_IMPORT_MODULE_DIRECTORY = DllStructCreate("dword RVAOriginalFirstThunk;" & _
"dword TimeDateStamp;" & _
"dword ForwarderChain;" & _
"dword RVAModuleName;" & _
"dword RVAFirstThunk", _
$pImportDirectory)
If Not DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAFirstThunk") Then ExitLoop ; the end
$tModuleName = DllStructCreate("char Name[64]", $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAModuleName"))
$hModule = __Au3Obj_Mem_LoadLibraryEx(DllStructGetData($tModuleName, "Name")) ; load the module, full load
$iInitialOffset = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAFirstThunk")
$iInitialOffset2 = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAOriginalFirstThunk")
If $iInitialOffset2 = $hInstance Then $iInitialOffset2 = $iInitialOffset
$iOffset = 0 ; back to 0
While 1
$tBufferOffset2 = DllStructCreate("ptr", $iInitialOffset2 + $iOffset)
$iBufferOffset2 = DllStructGetData($tBufferOffset2, 1) ; value at that address
If Not $iBufferOffset2 Then ExitLoop ; zero value is the end
If BitShift(BinaryMid($iBufferOffset2, $__Au3Obj_PTR_SIZE, 1), 7) Then ; MSB is set for imports by ordinal, otherwise not
$pFuncAddress = __Au3Obj_Mem_GetAddress($hModule, BitAND($iBufferOffset2, 0xFFFFFF)) ; the rest is ordinal value
Else
$tFuncName = DllStructCreate("word Ordinal; char Name[64]", $hInstance + $iBufferOffset2)
$sFuncName = DllStructGetData($tFuncName, "Name")
$pFuncAddress = __Au3Obj_Mem_GetAddress($hModule, $sFuncName)
EndIf
DllStructSetData(DllStructCreate("ptr", $iInitialOffset + $iOffset), 1, $pFuncAddress) ; and this is what's this all about
$iOffset += $__Au3Obj_PTR_SIZE ; size of $tBufferOffset2
WEnd
$pImportDirectory += 20 ; size of $tIMAGE_IMPORT_MODULE_DIRECTORY
WEnd
Return 1 ; all OK!
EndFunc ;==>__Au3Obj_Mem_FixImports
Func __Au3Obj_Mem_Base64Decode($sData) ; Ward
Local $bOpcode
If $__Au3Obj_X64 Then
$bOpcode = Binary("0x4156415541544D89CC555756534C89C34883EC20410FB64104418800418B3183FE010F84AB00000073434863D24D89C54889CE488D3C114839FE0F84A50100000FB62E4883C601E8B501000083ED2B4080FD5077E2480FBEED0FB6042884C00FBED078D3C1E20241885500EB7383FE020F841C01000031C083FE03740F4883C4205B5E5F5D415C415D415EC34863D24D89C54889CE488D3C114839FE0F84CA0000000FB62E4883C601E85301000083ED2B4080FD5077E2480FBEED0FB6042884C078D683E03F410845004983C501E964FFFFFF4863D24D89C54889CE488D3C114839FE0F84E00000000FB62E4883C601E80C01000083ED2B4080FD5077E2480FBEED0FB6042884C00FBED078D389D04D8D7501C1E20483E03041885501C1F804410845004839FE747B0FB62E4883C601E8CC00000083ED2B4080FD5077E6480FBEED0FB6042884C00FBED078D789D0C1E2064D8D6E0183E03C41885601C1F8024108064839FE0F8536FFFFFF41C7042403000000410FB6450041884424044489E84883C42029D85B5E5F5D415C415D415EC34863D24889CE4D89C6488D3C114839FE758541C7042402000000410FB60641884424044489F04883C42029D85B5E5F5D415C415D415EC341C7042401000000410FB6450041884424044489E829D8E998FEFFFF41C7042400000000410FB6450041884424044489E829D8E97CFEFFFFE8500000003EFFFFFF3F3435363738393A3B3C3DFFFFFFFEFFFFFF000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F3031323358C3")
Else
$bOpcode = Binary("0x5557565383EC1C8B6C243C8B5424388B5C24308B7424340FB6450488028B550083FA010F84A1000000733F8B5424388D34338954240C39F30F848B0100000FB63B83C301E8890100008D57D580FA5077E50FBED20FB6041084C00FBED078D78B44240CC1E2028810EB6B83FA020F841201000031C083FA03740A83C41C5B5E5F5DC210008B4C24388D3433894C240C39F30F84CD0000000FB63B83C301E8300100008D57D580FA5077E50FBED20FB6041084C078DA8B54240C83E03F080283C2018954240CE96CFFFFFF8B4424388D34338944240C39F30F84D00000000FB63B83C301E8EA0000008D57D580FA5077E50FBED20FB6141084D20FBEC278D78B4C240C89C283E230C1FA04C1E004081189CF83C70188410139F374750FB60383C3018844240CE8A80000000FB654240C83EA2B80FA5077E00FBED20FB6141084D20FBEC278D289C283E23CC1FA02C1E006081739F38D57018954240C8847010F8533FFFFFFC74500030000008B4C240C0FB60188450489C82B44243883C41C5B5E5F5DC210008D34338B7C243839F3758BC74500020000000FB60788450489F82B44243883C41C5B5E5F5DC210008B54240CC74500010000000FB60288450489D02B442438E9B1FEFFFFC7450000000000EB99E8500000003EFFFFFF3F3435363738393A3B3C3DFFFFFFFEFFFFFF000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F3031323358C3")
EndIf
Local $tCodeBuffer = DllStructCreate("byte[" & BinaryLen($bOpcode) & "]")
DllStructSetData($tCodeBuffer, 1, $bOpcode)
__Au3Obj_Mem_VirtualProtect(DllStructGetPtr($tCodeBuffer), DllStructGetSize($tCodeBuffer), 64)
If @error Then Return SetError(1, 0, "")
Local $iLen = StringLen($sData)
Local $tOut = DllStructCreate("byte[" & $iLen & "]")
Local $tState = DllStructCreate("byte[16]")
Local $Call = __Au3Obj_PointerCall("int", DllStructGetPtr($tCodeBuffer), "str", $sData, "dword", $iLen, "ptr", DllStructGetPtr($tOut), "ptr", DllStructGetPtr($tState))
If @error Then Return SetError(2, 0, "")
Return BinaryMid(DllStructGetData($tOut, 1), 1, $Call[0])
EndFunc ;==>__Au3Obj_Mem_Base64Decode
Func __Au3Obj_Mem_LoadLibraryEx($sModule, $iFlag = 0)
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "handle", "LoadLibraryExW", "wstr", $sModule, "handle", 0, "dword", $iFlag)
If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_Mem_LoadLibraryEx
Func __Au3Obj_Mem_FreeLibrary($hModule)
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "bool", "FreeLibrary", "handle", $hModule)
If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
Return 1
EndFunc ;==>__Au3Obj_Mem_FreeLibrary
Func __Au3Obj_Mem_GetAddress($hModule, $vFuncName)
Local $sType = "str"
If IsNumber($vFuncName) Then $sType = "int" ; if ordinal value passed
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "ptr", "GetProcAddress", "handle", $hModule, $sType, $vFuncName)
If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
Return $aCall[0]
EndFunc ;==>__Au3Obj_Mem_GetAddress
Func __Au3Obj_Mem_VirtualProtect($pAddress, $iSize, $iProtection)
Local $aCall = DllCall($gh_AU3Obj_kernel32dll, "bool", "VirtualProtect", "ptr", $pAddress, "dword_ptr", $iSize, "dword", $iProtection, "dword*", 0)
If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
Return 1