-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxCom.bas
1559 lines (795 loc) · 36.7 KB
/
xCom.bas
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
Attribute VB_Name = "xCom"
Option Explicit
#If Win64 Then
Const cSizeOfVariant& = 2 + 2 * 3 + 8 * 2 '24
Const cSizeOfPointer& = 8
Const cNullPointer^ = 0
#Else
Const cSizeOfVariant& = 2 + 2 * 3 + 4 * 2 '16
Const cSizeOfPointer& = 4
Const cNullPointer& = 0
#End If
' win API ---------
Private Declare PtrSafe Function DispCallFunc Lib "oleaut32" ( _
ByVal pvInstance_ As LongPtr, ByVal oVft_ As LongPtr, ByVal cc_ As Long, _
ByVal vtReturn_ As Integer, _
ByVal cActuals_ As Long, valueTypeTop_ As Integer, argPtrTop_ As LongPtr, _
pvargResult_ As Variant _
) As Long
Const cStdCall& = 4
Private Declare PtrSafe Function VariantCopy Lib "oleaut32" (ByRef dst_ As Any, ByRef src_ As Any) As Long
Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef dst_ As Any, ByRef src_ As Any, ByVal size_&)
Private Declare PtrSafe Function CoTaskMemAlloc Lib "Ole32" (ByVal byte_&) As LongPtr
Private Declare PtrSafe Sub CoTaskMemFree Lib "Ole32" (ByVal pMem_ As LongPtr)
'--------------------
Private Const cE_NOINTERFACE& = &H80004002
'構造体定義 ============================================================
' GUID 格納用構造体(中身はいじらないので16バイトあればいい)
Private Type CoGuid
id0 As Long
id1 As Integer
id2 As Integer
id3(8 - 1) As Byte
End Type
' // IID_NULL is null Interface ID, used when no other Interface ID is known.
'
' IID_NULL = NewGUID("{00000000-0000-0000-0000-000000000000}")
'
' // IID_IUnknown is for IUnknown interfaces.
'
' IID_IUnknown = NewGUID("{00000000-0000-0000-C000-000000000046}")
'
' // IID_IDispatch is for IDispatch interfaces.
'
' IID_IDispatch = NewGUID("{00020400-0000-0000-C000-000000000046}")
'
' // IID_IEnumVariant is for IEnumVariant interfaces
'
' IID_IEnumVariant = NewGUID("{00020404-0000-0000-C000-000000000046}")
' TypeInfo オブジェクトの関数情報構造体
Private Type CoFuncDesc
memid As Long
lprgscode As LongPtr
lprgelemdescParam As LongPtr
funckind As Long
invkind As Long
callconv As Long
cParams As Integer
cParamsOpt As Integer
oVft As Integer
cScodes As Integer
elemdescFunc As LongPtr
wFuncFlags As Integer
End Type
' TypeInfo オブジェクトの属性情報構造体(サイズ短縮版)
Private Type CoTypeAttrLite
Guid As CoGuid
lcid As Long
dwReserved As Long
memidConstructor As Long
memidDestructor As Long
lpstrSchema As LongPtr
cbSizeInstance As Long
typekind As Long
cFuncs As Integer
cVars As Integer
cImplTypes As Integer
cbSizeVft As Integer
cbAlignment As Integer
wTypeFlags As Integer
wMajorVerNum As Integer
wMinorVerNum As Integer
' tdescAlias As TYPEDESC '14/26 72/88 ; (3*Ptr)+ 2 ? https://msdn.microsoft.com/en-us/library/aa911717.aspx
' idldescType As IDLDESC '6 78/114 ; ? https://msdn.microsoft.com/en-us/library/aa909796.aspx
End Type
' Invoke 引数情報
Private Type DispParamsStruct
pArgs_ As LongPtr
pArgNames_ As LongPtr
argLength_ As Long
argNameLength_ As Long
End Type
' Variant 内部レイアウト
Private Type VariantStruct
varType As Integer
reserve0 As Integer
reserve1 As Integer
reserve2 As Integer
pEntity0 As LongPtr
pEntity1 As LongPtr
End Type
'自前 EnumVariant 構造体
Public Type EnumVariantStruct
PVtable As LongPtr
OperatorFunc As IFunc
RefCount As Long
End Type
' SafeArray 内部レイアウト
Private Type SafeArrayStruct
cDims As Integer
fFeatures As Integer
cbElements As Long '1要素のバイトサイズ
cLocks As Long 'ロック数。こちらでカウントアップしてしまうと、VBA側から操作できなくなる。
pvData As LongPtr 'PVOID
'SafeArrayBound rgsabound[ cDims ];
End Type
Private Type SafeArrayBound
Elements As Long '要素数
BaseIndex As Long ' LBound()
End Type
' 共用変数 ==========================================================
' CallDispFunc() 用の配列。
' モジュールレベルで宣言しないで済む方法を模索したい。
' ・ Static と同等の効果(一度行えば以降すむ)
' ・ Redim できる
' ・関数を通さないで済む(できれば)
' という条件を満たす方法はなさそうかなぁ…。CoTaskMem() とか?
Public CallableParamTypes() As Integer
Public CallableParamPtrs() As LongPtr
Public CallableParamArgs() As VariantStruct
'ユーティリティ関数 =======================================================
'GUIDを文字列に変換する。
Public Function GuidToString(guid_ As CoGuid) As String
Dim ts_$()
ReDim ts_(5 - 1)
ts_(0) = Right("00000000" & Hex(guid_.id0), 8)
ts_(1) = Right("0000" & Hex(guid_.id1), 4)
ts_(2) = Right("0000" & Hex(guid_.id2), 4)
ts_(3) = "0000"
ts_(4) = "000000000000"
Dim i&, istr_&
For i = 1 To 4 - 1 Step 2
Mid(ts_(3), i, 2) = Right("00" & Hex(guid_.id3(istr_)), 2)
istr_ = istr_ + 1
Next
For i = 1 To 12 - 1 Step 2
Mid(ts_(4), i, 2) = Right("00" & Hex(guid_.id3(istr_)), 2)
istr_ = istr_ + 1
Next
GuidToString = Join(ts_, "-")
End Function
Private Function isIUnknown_(guid_ As CoGuid) As Boolean
If Not (guid_.id0 = 0 And guid_.id1 = 0 And guid_.id2 = 0) Then Exit Function
Dim bs_(8 - 1) As Byte: bs_(0) = &HC0: bs_(7) = &H46
isIUnknown_ = CStr(guid_.id3) = CStr(bs_)
End Function
Private Function isIEnumVariant_(guid_ As CoGuid) As Boolean
If Not (guid_.id0 = &H20404 And guid_.id1 = 0 And guid_.id2 = 0) Then Exit Function
Dim bs_(8 - 1) As Byte: bs_(0) = &HC0: bs_(7) = &H46
isIEnumVariant_ = CStr(guid_.id3) = CStr(bs_)
End Function
' variant 値を受け取り、それへの参照 variant 値を生成して返す。
'参照元が破棄された場合、アクセスすると落ちるので注意。
Public Function ref(value_) As Variant
Dim v_ As VariantStruct
MoveMemory v_, value_, LenB(v_)
If (v_.varType And &H4000) = 0 Then
v_.varType = v_.varType Or &H4000
v_.pEntity0 = VarPtr(value_) + (VarPtr(v_.pEntity0) - VarPtr(v_))
End If
MoveMemory ref, v_, LenB(v_)
End Function
'受け取った variant 値を Empty にして、コピーした variant を戻り値として返す。
' ※戻り値を別の variant に代入した場合は、配列のコピーは行われない(らしい、検証したがいまいちわからんかった)。
Public Function Move(value_) As Variant
MoveMemory Move, value_, cSizeOfVariant
MoveMemory value_, Empty, cSizeOfVariant '元を空にしないと開放が2重に行われて落ちる。
End Function
'調整中
Public Sub CallFunc2(pObj_ As LongPtr, pFunc_ As LongPtr, ByRef args_, Optional ByRef out_result_, Optional isVbaClass_ As Boolean)
Dim argLength_&: argLength_ = UBound(args_) + 1
Dim argResLength_&: argResLength_ = argLength_ + (Not IsMissing(out_result_)) * isVbaClass_
'引数用配列の初期化
Static types_%()
Static pArgs_() As LongPtr
Select Case True
Case (Not types_) = -1 '未初期化
Dim minLen_&: minLen_ = IIf(argResLength_ > 7, argResLength_, 7) '最低 10 は確保される。
initArgArray_ minLen_, types_, pArgs_
Case argResLength_ > UBound(types_) + 1
initArgArray_ argResLength_, types_, pArgs_
End Select
'引数のセット
Dim i&
For i = 0 To argLength_ - 1
pArgs_(i) = VarPtr(args_(i))
Next
'関数をコール
If Not isVbaClass_ Then
Dim resultType_ As VbVarType
If Not IsMissing(out_result_) Then resultType_ = vbVariant
DispCallFunc pObj_, pFunc_, cStdCall, resultType_, argLength_, types_(0), pArgs_(0), out_result_
Else
If argResLength_ > argLength_ Then
Dim result_
Dim resultArg_ As VariantStruct
resultArg_.varType = vbVariant Or &H4000
resultArg_.pEntity0 = VarPtr(result_)
types_(argLength_) = vbVariant Or &H4000
pArgs_(argLength_) = VarPtr(resultArg_)
End If
Dim res_
DispCallFunc pObj_, pFunc_, cStdCall, vbLong, argResLength_, types_(0), pArgs_(0), res_
End If
End Sub
Public Sub CallFunc( _
_
pObj_ As LongPtr, pFunc_ As LongPtr, pArgTop_ As LongPtr, argLength_&, _
_
Optional ByRef out_result_, Optional isVbaClass_ As Boolean _
_
)
Dim argResLength_&: argResLength_ = argLength_ + (Not IsMissing(out_result_)) * isVbaClass_
'引数用配列の初期化
Static types_%()
Static pArgs_() As LongPtr
Select Case True
Case (Not types_) = -1 '未初期化
Dim minLen_&: minLen_ = IIf(argResLength_ > 7, argResLength_, 7) '最低 10 は確保される。
initArgArray_ minLen_, types_, pArgs_
Case argResLength_ > UBound(types_) + 1
initArgArray_ argResLength_, types_, pArgs_
End Select
'引数のセット
Dim i&
For i = 0 To argLength_ - 1
pArgs_(i) = pArgTop_ + i * cSizeOfVariant
Next
'関数をコール
If Not isVbaClass_ Then
Dim resultType_ As VbVarType
If Not IsMissing(out_result_) Then resultType_ = vbVariant
DispCallFunc pObj_, pFunc_, cStdCall, resultType_, argLength_, types_(0), pArgs_(0), out_result_
Else
If argResLength_ > argLength_ Then
Dim result_
Dim resultArg_ As VariantStruct
resultArg_.varType = vbVariant Or &H4000
resultArg_.pEntity0 = VarPtr(result_)
types_(argLength_) = vbVariant Or &H4000
pArgs_(argLength_) = VarPtr(resultArg_)
End If
Dim res_
DispCallFunc pObj_, pFunc_, cStdCall, vbLong, argResLength_, types_(0), pArgs_(0), res_
End If
End Sub
'引数用配列を初期化する。
Private Sub initArgArray_(length_&, ByRef types_%(), ByRef pArgs_() As LongPtr)
Dim capacity_&: capacity_ = length_ + length_ \ 2 - 1 '必要数の 1.5 倍を確保。
ReDim types_(capacity_)
ReDim pArgs_(capacity_)
Dim i&
For i = 0 To capacity_
types_(i) = vbVariant Or &H4000
Next
End Sub
'------------------
' dispId からメソッドを呼び出す。
Public Function InvokeMethod(object_ As Object, dispId_&, ParamArray args_()) As Variant
Call_Invoke object_, dispId_, VarPtr(args_(0)), UBound(args_) + 1, out_result_:=InvokeMethod
End Function
'メソッド名前から序数を取得する
Public Function GetOrderByName(object_ As Object, methodName_$) As Long
Dim typeInfo_ As IUnknown
Set typeInfo_ = Call_GetTypeInfo(object_)
Dim dispId_&: dispId_ = Call_GetIDsOfNames(object_, methodName_)
Dim i&
For i = 0 To call_GetTypeAttr_(typeInfo_).cFuncs - 1
' funcDesc 構造体を取得して dispId が一致するメソッドを探し、vtable 上の順番を取得する。
Dim desc_ As CoFuncDesc
desc_ = call_GetFuncDesc_(typeInfo_, i)
If desc_.memid = dispId_ Then
GetOrderByName = desc_.oVft
Exit Function
End If
Next
GetOrderByName = -1
End Function
'オブジェクトメンバメソッド名リストの取得
Public Function MakeVtableList(object_ As Object) As Collection
Dim typeInfo_ As IUnknown
Set typeInfo_ = Call_GetTypeInfo(object_)
Dim c_ As New Collection
Dim i&
For i = 0 To call_GetTypeAttr_(typeInfo_).cFuncs - 1
' funcDesc 構造体を取得して、memid と vtable 上の順番を取得する。
Dim desc_ As CoFuncDesc
desc_ = call_GetFuncDesc_(typeInfo_, i)
' dispId_ からメソッドの名前を取得する。
Dim thisName_$
thisName_ = call_GetNames(typeInfo_, desc_.memid)
c_.Add desc_.oVft, thisName_
Next
Set MakeVtableList = c_
End Function
'メソッドアドレスを取得する。
Public Function GetAddress(object_ As Object, methodName_$) As LongPtr
Dim typeInfo_ As IUnknown
Set typeInfo_ = Call_GetTypeInfo(object_)
Dim dispId_&
dispId_ = Call_GetIDsOfNames(object_, methodName_)
GetAddress = Call_AddressOfMember(typeInfo_, dispId_)
End Function
' v-table からメソッドアドレスを取得する。
Public Function GetAddressFromVtable(object_ As Object, methodName_$) As LongPtr
' Dim ti_: Set ti_ = call_GetTypeInfo_(object_)
Dim order_&: order_ = xCom.GetOrderByName(object_, methodName_)
Dim pVTable_ As LongPtr
MoveMemory ByVal VarPtr(pVTable_), ByVal ObjPtr(object_), LenB(pVTable_)
MoveMemory ByVal VarPtr(GetAddressFromVtable), ByVal pVTable_ + order_, LenB(GetAddressFromVtable)
' Dim a_(64) As LongPtr
' MoveMemory ByVal VarPtr(a_(0)), ByVal pVTable_, 32 * 4
End Function
'インターフェースメソッドコール ===============================================================================
' TypeInfo 取得 ----------------------------
Public Function Call_GetTypeInfo(object_ As Object) As IUnknown
'パラメータ設定
Static types_%()
Static ptrs_() As LongPtr
setParamsForGetTypeInfo_ types_, ptrs_, _
get_typeInfo_:=Call_GetTypeInfo
'取得
Dim res_
Const cGetTypeInfo As LongPtr = 4 * cSizeOfPointer
DispCallFunc ObjPtr(object_), cGetTypeInfo, cStdCall, vbLong, UBound(types_) + 1, types_(0), ptrs_(0), res_
'if res_ = then err.Raise 0,,""
End Function
' - - - - - - - -
Private Sub setParamsForGetTypeInfo_( _
_
ByRef ref_types_%(), ByRef ref_ptrs_() As LongPtr, _
_
ByRef get_typeInfo_ As IUnknown _
_
)
If (Not Not ref_types_) = 0 Then
ReDim ref_types_(3 - 1)
ReDim ref_ptrs_(3 - 1)
'引数
Static zero_: zero_ = 0& ' 0 では Integer になるのでダメ
Static locale_: locale_ = &H800& ' LOCALE_SYSTEM_DEFAULT
Static ppTi_: ppTi_ = VarPtr(get_typeInfo_) ' IUnknown 型のもつポインタ値を書き換えて取得する。
ref_types_(0) = vbLong
ref_ptrs_(0) = VarPtr(zero_)
ref_types_(1) = vbLong
ref_ptrs_(1) = VarPtr(locale_)
ref_types_(2) = varType(ppTi_)
ref_ptrs_(2) = VarPtr(ppTi_)
End If
ppTi_ = VarPtr(get_typeInfo_)
End Sub
' ----------------------------------------------------------------
' dispid 取得 ----------------------------
Public Function Call_GetIDsOfNames(object_ As Object, fname_$) As Long
'パラメータ設定
Static types_%()
Static ptrs_() As LongPtr
setParamsForGetDispId_ types_, ptrs_, _
give_fname_:=fname_, _
get_dispId_:=Call_GetIDsOfNames
'取得
Dim res_
Const cGetIDsOfNames As LongPtr = 5 * cSizeOfPointer
DispCallFunc ObjPtr(object_), cGetIDsOfNames, cStdCall, vbLong, UBound(types_) + 1, types_(0), ptrs_(0), res_
'if res_ = then err.Raise 0,,""
End Function
' - - - - - - - -
Private Sub setParamsForGetDispId_( _
_
ByRef ref_types_%(), ByRef ref_ptrs_() As LongPtr, _
_
give_fname_$, _
_
ByRef get_dispId_& _
_
)
If (Not Not ref_types_) = 0 Then
ReDim ref_types_(5 - 1)
ReDim ref_ptrs_(5 - 1)
Static iid_ As CoGuid 'GUIDはゼロ
Static pName_ As LongPtr '関数名
'引数
Static pIid_: pIid_ = VarPtr(iid_)
Static ppName_: ppName_ = VarPtr(pName_)
Static count_: count_ = 1& ' 1 では Integer となりバグる、このへんシビア
Static locale_: locale_ = &H800& ' LOCALE_SYSTEM_DEFAULT
Static pId_: pId_ = VarPtr(get_dispId_)
ref_types_(0) = varType(pIid_)
ref_ptrs_(0) = VarPtr(pIid_)
ref_types_(1) = varType(ppName_)
ref_ptrs_(1) = VarPtr(ppName_)
ref_types_(2) = vbLong
ref_ptrs_(2) = VarPtr(count_)
ref_types_(3) = vbLong
ref_ptrs_(3) = VarPtr(locale_)
ref_types_(4) = varType(pId_)
ref_ptrs_(4) = VarPtr(pId_)
End If
pName_ = StrPtr(give_fname_)
pId_ = VarPtr(get_dispId_)
End Sub
' ----------------------------------------------------------------
'メソッド情報を取得、FuncDesc 構造体を返す ----------------------------
Private Function call_GetFuncDesc_(typeInfo_ As IUnknown, funcIndex_&) As CoFuncDesc
'パラメータ設定
Static types_%()
Static ptrs_() As LongPtr
Dim pDesc_ As LongPtr '結果 FuncDesc の先頭
setParamsForGetFuncDesc_ types_, ptrs_, _
give_funcIndex_:=funcIndex_, _
get_pDesc_:=pDesc_
' FuncDesc 構造体の取得
Dim resA_
Const cGetFuncDesc As LongPtr = 5 * cSizeOfPointer
DispCallFunc ObjPtr(typeInfo_), cGetFuncDesc, cStdCall, vbLong, UBound(types_) + 1, types_(0), ptrs_(0), resA_
'if res_ = then err.Raise 0,,""
'構造体をコピーする
MoveMemory ByVal VarPtr(call_GetFuncDesc_), ByVal pDesc_, LenB(call_GetFuncDesc_)
' FuncDesc 構造体の開放
Dim resB_
Dim pDescRelease_: pDescRelease_ = pDesc_
Const cReleaseFuncDesc As LongPtr = 20 * cSizeOfPointer
DispCallFunc ObjPtr(typeInfo_), cReleaseFuncDesc, cStdCall, vbEmpty, 1, varType(pDescRelease_), VarPtr(pDescRelease_), resB_
'if res_ = then err.Raise 0,,""
End Function
' - - - - - - - -
Private Sub setParamsForGetFuncDesc_( _
_
ByRef ref_types_%(), ByRef ref_ptrs_() As LongPtr, _
_
give_funcIndex_&, _
_
get_pDesc_ As LongPtr _
_
)
If (Not Not ref_types_) = 0 Then
ReDim ref_types_(2 - 1)
ReDim ref_ptrs_(2 - 1)
'引数
Static fIndex_: fIndex_ = give_funcIndex_
Static ppDesc_: ppDesc_ = VarPtr(get_pDesc_)
ref_types_(0) = vbLong
ref_ptrs_(0) = VarPtr(fIndex_)
ref_types_(1) = varType(ppDesc_)
ref_ptrs_(1) = VarPtr(ppDesc_)
End If
ppDesc_ = VarPtr(get_pDesc_)
fIndex_ = give_funcIndex_
End Sub
' ----------------------------------------------------------------
'名前の取得 ----------------------------
Public Function call_GetNames(typeInfo_ As IUnknown, dispId_&) As String
'パラメータ設定
Static types_%()
Static ptrs_() As LongPtr
Dim pName_ As LongPtr '受け取る名前文字列の先頭(なんで開放しなくていいんだろう?)
setParamsForGetName_ types_, ptrs_, _
give_dispId_:=dispId_, _
get_pName_:=pName_
'取得
Dim res_
Const cGetNames As LongPtr = 7 * cSizeOfPointer
DispCallFunc ObjPtr(typeInfo_), cGetNames, cStdCall, vbLong, UBound(types_) + 1, types_(0), ptrs_(0), res_ ' get names
'if res_ = then err.Raise 0,,""
'結果を格納する。
MoveMemory ByVal VarPtr(call_GetNames), ByVal VarPtr(pName_), LenB(pName_) ' pName_ は bstr
End Function
' - - - - - - - -
Private Sub setParamsForGetName_( _
_
ByRef ref_types_%(), ByRef ref_ptrs_() As LongPtr, _
_
give_dispId_&, _
_
get_pName_ As LongPtr _
_
)
If (Not Not ref_types_) = 0 Then
ReDim ref_types_(4 - 1)
ReDim ref_ptrs_(4 - 1)
'参照先
Static pRtnLength_& '取得数が返ってくるが、使用しない。
'引数
Static dispId_: dispId_ = give_dispId_
Static maxLength_: maxLength_ = 1& ' 1 では Integer となりバグる、このへんシビア
Static ppName_: ppName_ = VarPtr(get_pName_)
Static ppLength_: ppLength_ = VarPtr(pRtnLength_)
ref_types_(0) = vbLong
ref_ptrs_(0) = VarPtr(dispId_)
ref_types_(1) = varType(ppName_)
ref_ptrs_(1) = VarPtr(ppName_)
ref_types_(2) = vbLong
ref_ptrs_(2) = VarPtr(maxLength_)
ref_types_(3) = varType(ppLength_)
ref_ptrs_(3) = VarPtr(ppLength_)
End If
dispId_ = give_dispId_
ppName_ = VarPtr(get_pName_)
End Sub
' ----------------------------------------------------------------