-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
automation.py
986 lines (879 loc) · 32.8 KB
/
automation.py
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
# comtypes.automation module
import array
import datetime
import decimal
import sys
from ctypes import *
from ctypes import _Pointer
from _ctypes import CopyComPointer
from comtypes import (
BSTR, COMError, COMMETHOD, GUID, IID, IUnknown, STDMETHOD, TYPE_CHECKING,
)
from comtypes.hresult import *
import comtypes.patcher
import comtypes
try:
from comtypes import _safearray
except (ImportError, AttributeError):
class _safearray(object):
tagSAFEARRAY = None
from ctypes.wintypes import DWORD, LONG, UINT, VARIANT_BOOL, WCHAR, WORD
if TYPE_CHECKING:
from typing import (
Any, Callable, ClassVar, List, Optional, Tuple, Union as _UnionT,
)
from comtypes import hints
if sys.version_info >= (3, 0):
int_types = (int, )
str_types = (str, )
base_text_type = str
else:
int_types = (int, long)
str_types = (unicode, str)
base_text_type = basestring
LCID = DWORD
DISPID = LONG
SCODE = LONG
VARTYPE = c_ushort
DISPATCH_METHOD = 1
DISPATCH_PROPERTYGET = 2
DISPATCH_PROPERTYPUT = 4
DISPATCH_PROPERTYPUTREF = 8
tagINVOKEKIND = c_int
INVOKE_FUNC = DISPATCH_METHOD
INVOKE_PROPERTYGET = DISPATCH_PROPERTYGET
INVOKE_PROPERTYPUT = DISPATCH_PROPERTYPUT
INVOKE_PROPERTYPUTREF = DISPATCH_PROPERTYPUTREF
INVOKEKIND = tagINVOKEKIND
################################
# helpers
IID_NULL = GUID()
riid_null = byref(IID_NULL)
_byref_type = type(byref(c_int()))
# 30. December 1899, midnight. For VT_DATE.
_com_null_date = datetime.datetime(1899, 12, 30, 0, 0, 0)
################################################################
# VARIANT, in all it's glory.
VARENUM = c_int # enum
VT_EMPTY = 0
VT_NULL = 1
VT_I2 = 2
VT_I4 = 3
VT_R4 = 4
VT_R8 = 5
VT_CY = 6
VT_DATE = 7
VT_BSTR = 8
VT_DISPATCH = 9
VT_ERROR = 10
VT_BOOL = 11
VT_VARIANT = 12
VT_UNKNOWN = 13
VT_DECIMAL = 14
VT_I1 = 16
VT_UI1 = 17
VT_UI2 = 18
VT_UI4 = 19
VT_I8 = 20
VT_UI8 = 21
VT_INT = 22
VT_UINT = 23
VT_VOID = 24
VT_HRESULT = 25
VT_PTR = 26
VT_SAFEARRAY = 27
VT_CARRAY = 28
VT_USERDEFINED = 29
VT_LPSTR = 30
VT_LPWSTR = 31
VT_RECORD = 36
VT_INT_PTR = 37
VT_UINT_PTR = 38
VT_FILETIME = 64
VT_BLOB = 65
VT_STREAM = 66
VT_STORAGE = 67
VT_STREAMED_OBJECT = 68
VT_STORED_OBJECT = 69
VT_BLOB_OBJECT = 70
VT_CF = 71
VT_CLSID = 72
VT_VERSIONED_STREAM = 73
VT_BSTR_BLOB = 4095
VT_VECTOR = 4096
VT_ARRAY = 8192
VT_BYREF = 16384
VT_RESERVED = 32768
VT_ILLEGAL = 65535
VT_ILLEGALMASKED = 4095
VT_TYPEMASK = 4095
class tagCY(Structure):
_fields_ = [("int64", c_longlong)]
CY = tagCY
CURRENCY = CY
class tagDEC(Structure):
_fields_ = [("wReserved", c_ushort),
("scale", c_ubyte),
("sign", c_ubyte),
("Hi32", c_ulong),
("Lo64", c_ulonglong)]
def as_decimal(self):
""" Convert a tagDEC struct to Decimal.
See http://msdn.microsoft.com/en-us/library/cc234586.aspx for the tagDEC
specification.
"""
digits = (self.Hi32 << 64) + self.Lo64
decimal_str = "{0}{1}e-{2}".format(
'-' if self.sign else '',
digits,
self.scale,
)
return decimal.Decimal(decimal_str)
DECIMAL = tagDEC
# The VARIANT structure is a good candidate for implementation in a C
# helper extension. At least the get/set methods.
class tagVARIANT(Structure):
if TYPE_CHECKING:
vt = hints.AnnoField() # type: int
_ = hints.AnnoField() # type: U_VARIANT1.__tagVARIANT.U_VARIANT2
null = hints.AnnoField() # type: ClassVar[VARIANT]
empty = hints.AnnoField() # type: ClassVar[VARIANT]
missing = hints.AnnoField() # type: ClassVar[VARIANT]
class U_VARIANT1(Union):
class __tagVARIANT(Structure):
# The C Header file defn of VARIANT is much more complicated, but
# this is the ctypes version - functional as well.
class U_VARIANT2(Union):
class _tagBRECORD(Structure):
_fields_ = [("pvRecord", c_void_p),
("pRecInfo", POINTER(IUnknown))]
_fields_ = [
("VT_BOOL", VARIANT_BOOL),
("VT_I1", c_byte),
("VT_I2", c_short),
("VT_I4", c_long),
("VT_I8", c_longlong),
("VT_INT", c_int),
("VT_UI1", c_ubyte),
("VT_UI2", c_ushort),
("VT_UI4", c_ulong),
("VT_UI8", c_ulonglong),
("VT_UINT", c_uint),
("VT_R4", c_float),
("VT_R8", c_double),
("VT_CY", c_longlong),
("c_wchar_p", c_wchar_p),
("c_void_p", c_void_p),
("pparray", POINTER(POINTER(_safearray.tagSAFEARRAY))),
("bstrVal", BSTR),
("_tagBRECORD", _tagBRECORD),
]
_anonymous_ = ["_tagBRECORD"]
_fields_ = [("vt", VARTYPE),
("wReserved1", c_ushort),
("wReserved2", c_ushort),
("wReserved3", c_ushort),
("_", U_VARIANT2)
]
_fields_ = [("__VARIANT_NAME_2", __tagVARIANT),
("decVal", DECIMAL)]
_anonymous_ = ["__VARIANT_NAME_2"]
_fields_ = [("__VARIANT_NAME_1", U_VARIANT1)]
_anonymous_ = ["__VARIANT_NAME_1"]
def __init__(self, *args):
if args:
self.value = args[0]
def __del__(self):
if self._b_needsfree_:
# XXX This does not work. _b_needsfree_ is never
# set because the buffer is internal to the object.
_VariantClear(self)
def __repr__(self):
if self.vt & VT_BYREF:
return "VARIANT(vt=0x%x, byref(%r))" % (self.vt, self[0])
elif self is type(self).null:
return "VARIANT.null"
elif self is type(self).empty:
return "VARIANT.empty"
elif self is type(self).missing:
return "VARIANT.missing"
return "VARIANT(vt=0x%x, %r)" % (self.vt, self.value)
@classmethod
def from_param(cls, value):
if isinstance(value, cls):
return value
return cls(value)
def __setitem__(self, index, value):
# This method allows to change the value of a
# (VT_BYREF|VT_xxx) variant in place.
if index != 0:
raise IndexError(index)
if not self.vt & VT_BYREF:
raise TypeError("set_byref requires a VT_BYREF VARIANT instance")
typ = _vartype_to_ctype[self.vt & ~VT_BYREF]
cast(self._.c_void_p, POINTER(typ))[0] = value
# see also c:/sf/pywin32/com/win32com/src/oleargs.cpp 54
def _set_value(self, value):
_VariantClear(self)
if value is None:
self.vt = VT_NULL
elif (hasattr(value, '__len__') and len(value) == 0
and not isinstance(value, base_text_type)):
self.vt = VT_NULL
# since bool is a subclass of int, this check must come before
# the check for int
elif isinstance(value, bool):
self.vt = VT_BOOL
self._.VT_BOOL = value
elif isinstance(value, (int, c_int)):
self.vt = VT_I4
self._.VT_I4 = value
elif isinstance(value, int_types):
u = self._
# try VT_I4 first.
u.VT_I4 = value
if u.VT_I4 == value:
# it did work.
self.vt = VT_I4
return
# try VT_UI4 next.
if value >= 0:
u.VT_UI4 = value
if u.VT_UI4 == value:
# did work.
self.vt = VT_UI4
return
# try VT_I8 next.
if value >= 0:
u.VT_I8 = value
if u.VT_I8 == value:
# did work.
self.vt = VT_I8
return
# try VT_UI8 next.
if value >= 0:
u.VT_UI8 = value
if u.VT_UI8 == value:
# did work.
self.vt = VT_UI8
return
# VT_R8 is last resort.
self.vt = VT_R8
u.VT_R8 = float(value)
elif isinstance(value, (float, c_double)):
self.vt = VT_R8
self._.VT_R8 = value
elif isinstance(value, str_types):
self.vt = VT_BSTR
# do the c_wchar_p auto unicode conversion
self._.c_void_p = _SysAllocStringLen(value, len(value))
elif isinstance(value, datetime.datetime):
delta = value - _com_null_date
# a day has 24 * 60 * 60 = 86400 seconds
com_days = delta.days + (delta.seconds + delta.microseconds * 1e-6) / 86400.
self.vt = VT_DATE
self._.VT_R8 = com_days
elif comtypes.npsupport.isdatetime64(value):
com_days = value - comtypes.npsupport.com_null_date64
com_days /= comtypes.npsupport.numpy.timedelta64(1, 'D')
self.vt = VT_DATE
self._.VT_R8 = com_days
elif decimal is not None and isinstance(value, decimal.Decimal):
self._.VT_CY = int(round(value * 10000))
self.vt = VT_CY
elif isinstance(value, POINTER(IDispatch)):
CopyComPointer(value, byref(self._))
self.vt = VT_DISPATCH
elif isinstance(value, POINTER(IUnknown)):
CopyComPointer(value, byref(self._))
self.vt = VT_UNKNOWN
elif isinstance(value, (list, tuple)):
obj = _midlSAFEARRAY(VARIANT).create(value)
memmove(byref(self._), byref(obj), sizeof(obj))
self.vt = VT_ARRAY | obj._vartype_
elif isinstance(value, array.array):
vartype = _arraycode_to_vartype[value.typecode]
typ = _vartype_to_ctype[vartype]
obj = _midlSAFEARRAY(typ).create(value)
memmove(byref(self._), byref(obj), sizeof(obj))
self.vt = VT_ARRAY | obj._vartype_
elif comtypes.npsupport.isndarray(value):
# Try to convert a simple array of basic types.
descr = value.dtype.descr[0][1]
typ = comtypes.npsupport.typecodes.get(descr)
if typ is None:
# Try for variant
obj = _midlSAFEARRAY(VARIANT).create(value)
else:
obj = _midlSAFEARRAY(typ).create(value)
memmove(byref(self._), byref(obj), sizeof(obj))
self.vt = VT_ARRAY | obj._vartype_
elif isinstance(value, Structure) and hasattr(value, "_recordinfo_"):
guids = value._recordinfo_
from comtypes.typeinfo import GetRecordInfoFromGuids
ri = GetRecordInfoFromGuids(*guids)
self.vt = VT_RECORD
# Assigning a COM pointer to a structure field does NOT
# call AddRef(), have to call it manually:
ri.AddRef()
self._.pRecInfo = ri
self._.pvRecord = ri.RecordCreateCopy(byref(value))
elif isinstance(getattr(value, "_comobj", None), POINTER(IDispatch)):
CopyComPointer(value._comobj, byref(self._))
self.vt = VT_DISPATCH
elif isinstance(value, VARIANT):
_VariantCopy(self, value)
elif isinstance(value, c_ubyte):
self._.VT_UI1 = value
self.vt = VT_UI1
elif isinstance(value, c_char):
self._.VT_UI1 = ord(value.value)
self.vt = VT_UI1
elif isinstance(value, c_byte):
self._.VT_I1 = value
self.vt = VT_I1
elif isinstance(value, c_ushort):
self._.VT_UI2 = value
self.vt = VT_UI2
elif isinstance(value, c_short):
self._.VT_I2 = value
self.vt = VT_I2
elif isinstance(value, c_uint):
self.vt = VT_UI4
self._.VT_UI4 = value
elif isinstance(value, c_float):
self.vt = VT_R4
self._.VT_R4 = value
elif isinstance(value, c_int64):
self.vt = VT_I8
self._.VT_I8 = value
elif isinstance(value, c_uint64):
self.vt = VT_UI8
self._.VT_UI8 = value
elif isinstance(value, _byref_type):
ref = value._obj
self._.c_void_p = addressof(ref)
self.__keepref = value
self.vt = _ctype_to_vartype[type(ref)] | VT_BYREF
elif isinstance(value, _Pointer):
ref = value.contents
self._.c_void_p = addressof(ref)
self.__keepref = value
self.vt = _ctype_to_vartype[type(ref)] | VT_BYREF
else:
raise TypeError("Cannot put %r in VARIANT" % value)
# buffer -> SAFEARRAY of VT_UI1 ?
# c:/sf/pywin32/com/win32com/src/oleargs.cpp 197
def _get_value(self, dynamic=False):
vt = self.vt
if vt in (VT_EMPTY, VT_NULL):
return None
elif vt == VT_I1:
return self._.VT_I1
elif vt == VT_I2:
return self._.VT_I2
elif vt == VT_I4:
return self._.VT_I4
elif vt == VT_I8:
return self._.VT_I8
elif vt == VT_UI8:
return self._.VT_UI8
elif vt == VT_INT:
return self._.VT_INT
elif vt == VT_UI1:
return self._.VT_UI1
elif vt == VT_UI2:
return self._.VT_UI2
elif vt == VT_UI4:
return self._.VT_UI4
elif vt == VT_UINT:
return self._.VT_UINT
elif vt == VT_R4:
return self._.VT_R4
elif vt == VT_R8:
return self._.VT_R8
elif vt == VT_BOOL:
return self._.VT_BOOL
elif vt == VT_BSTR:
return self._.bstrVal
elif vt == VT_DATE:
days = self._.VT_R8
return datetime.timedelta(days=days) + _com_null_date
elif vt == VT_CY:
return self._.VT_CY / decimal.Decimal("10000")
elif vt == VT_UNKNOWN:
val = self._.c_void_p
if not val:
# We should/could return a NULL COM pointer.
# But the code generation must be able to construct one
# from the __repr__ of it.
return None # XXX?
ptr = cast(val, POINTER(IUnknown))
# cast doesn't call AddRef (it should, imo!)
ptr.AddRef()
return ptr.__ctypes_from_outparam__()
elif vt == VT_DECIMAL:
return self.decVal.as_decimal()
elif vt == VT_DISPATCH:
val = self._.c_void_p
if not val:
# See above.
return None # XXX?
ptr = cast(val, POINTER(IDispatch))
# cast doesn't call AddRef (it should, imo!)
ptr.AddRef()
if not dynamic:
return ptr.__ctypes_from_outparam__()
else:
from comtypes.client.dynamic import Dispatch
return Dispatch(ptr)
# see also c:/sf/pywin32/com/win32com/src/oleargs.cpp
elif self.vt & VT_BYREF:
return self
elif vt == VT_RECORD:
from comtypes.client import GetModule
from comtypes.typeinfo import IRecordInfo
# Retrieving a COM pointer from a structure field does NOT
# call AddRef(), have to call it manually:
punk = self._.pRecInfo
punk.AddRef()
ri = punk.QueryInterface(IRecordInfo)
# find typelib
tlib = ri.GetTypeInfo().GetContainingTypeLib()[0]
# load typelib wrapper module
mod = GetModule(tlib)
# retrive the type and create an instance
value = getattr(mod, ri.GetName())()
# copy data into the instance
ri.RecordCopy(self._.pvRecord, byref(value))
return value
elif self.vt & VT_ARRAY:
typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
return cast(self._.pparray, _midlSAFEARRAY(typ)).unpack()
else:
raise NotImplementedError("typecode %d = 0x%x)" % (vt, vt))
def __getitem__(self, index):
if index != 0:
raise IndexError(index)
if self.vt == VT_BYREF|VT_VARIANT:
v = VARIANT()
# apparently VariantCopyInd doesn't work always with
# VT_BYREF|VT_VARIANT, so do it manually.
v = cast(self._.c_void_p, POINTER(VARIANT))[0]
return v.value
else:
v = VARIANT()
_VariantCopyInd(v, self)
return v.value
# these are missing:
## getter[VT_ERROR]
## getter[VT_ARRAY]
## getter[VT_BYREF|VT_UI1]
## getter[VT_BYREF|VT_I2]
## getter[VT_BYREF|VT_I4]
## getter[VT_BYREF|VT_R4]
## getter[VT_BYREF|VT_R8]
## getter[VT_BYREF|VT_BOOL]
## getter[VT_BYREF|VT_ERROR]
## getter[VT_BYREF|VT_CY]
## getter[VT_BYREF|VT_DATE]
## getter[VT_BYREF|VT_BSTR]
## getter[VT_BYREF|VT_UNKNOWN]
## getter[VT_BYREF|VT_DISPATCH]
## getter[VT_BYREF|VT_ARRAY]
## getter[VT_BYREF|VT_VARIANT]
## getter[VT_BYREF]
## getter[VT_BYREF|VT_DECIMAL]
## getter[VT_BYREF|VT_I1]
## getter[VT_BYREF|VT_UI2]
## getter[VT_BYREF|VT_UI4]
## getter[VT_BYREF|VT_INT]
## getter[VT_BYREF|VT_UINT]
value = property(_get_value, _set_value)
def __ctypes_from_outparam__(self):
# XXX Manual resource management, because of the VARIANT bug:
result = self.value
self.value = None
return result
def ChangeType(self, typecode):
_VariantChangeType(self,
self,
0,
typecode)
VARIANT = tagVARIANT
VARIANTARG = VARIANT
_oleaut32 = OleDLL("oleaut32")
_VariantChangeType = _oleaut32.VariantChangeType
_VariantChangeType.argtypes = (POINTER(VARIANT), POINTER(VARIANT), c_ushort, VARTYPE)
_VariantClear = _oleaut32.VariantClear
_VariantClear.argtypes = (POINTER(VARIANT),)
_SysAllocStringLen = windll.oleaut32.SysAllocStringLen
_SysAllocStringLen.argtypes = c_wchar_p, c_uint
_SysAllocStringLen.restype = c_void_p
_VariantCopy = _oleaut32.VariantCopy
_VariantCopy.argtypes = POINTER(VARIANT), POINTER(VARIANT)
_VariantCopyInd = _oleaut32.VariantCopyInd
_VariantCopyInd.argtypes = POINTER(VARIANT), POINTER(VARIANT)
# some commonly used VARIANT instances
VARIANT.null = VARIANT(None)
VARIANT.empty = VARIANT()
VARIANT.missing = v = VARIANT()
v.vt = VT_ERROR
v._.VT_I4 = 0x80020004
del v
_carg_obj = type(byref(c_int()))
from ctypes import Array as _CArrayType
@comtypes.patcher.Patch(POINTER(VARIANT))
class _(object):
# Override the default .from_param classmethod of POINTER(VARIANT).
# This allows to pass values which can be stored in VARIANTs as
# function parameters declared as POINTER(VARIANT). See
# InternetExplorer's Navigate2() method, or Word's Close() method, for
# examples.
@classmethod
def from_param(cls, arg):
# accept POINTER(VARIANT) instance
if isinstance(arg, POINTER(VARIANT)):
return arg
# accept byref(VARIANT) instance
if isinstance(arg, _carg_obj) and isinstance(arg._obj, VARIANT):
return arg
# accept VARIANT instance
if isinstance(arg, VARIANT):
return byref(arg)
if isinstance(arg, _CArrayType) and arg._type_ is VARIANT:
# accept array of VARIANTs
return arg
# anything else which can be converted to a VARIANT.
return byref(VARIANT(arg))
def __setitem__(self, index, value):
# This is to support the same sematics as a pointer instance:
# variant[0] = value
self[index].value = value
################################################################
# interfaces, structures, ...
class IEnumVARIANT(IUnknown):
_iid_ = GUID('{00020404-0000-0000-C000-000000000046}')
_idlflags_ = ['hidden']
_dynamic = False
def __iter__(self):
return self
if sys.version_info >= (3, 0):
def __next__(self):
item, fetched = self.Next(1)
if fetched:
return item
raise StopIteration
else:
def next(self):
item, fetched = self.Next(1)
if fetched:
return item
raise StopIteration
def __getitem__(self, index):
self.Reset()
# Does not yet work.
## if isinstance(index, slice):
## self.Skip(index.start or 0)
## return self.Next(index.stop or sys.maxint)
self.Skip(index)
item, fetched = self.Next(1)
if fetched:
return item
raise IndexError
def Next(self, celt):
fetched = c_ulong()
if celt == 1:
v = VARIANT()
self.__com_Next(celt, v, fetched)
return v._get_value(dynamic=self._dynamic), fetched.value
array = (VARIANT * celt)()
self.__com_Next(celt, array, fetched)
result = [v._get_value(dynamic=self._dynamic) for v in array[:fetched.value]]
for v in array:
v.value = None
return result
IEnumVARIANT._methods_ = [
COMMETHOD([], HRESULT, 'Next',
( ['in'], c_ulong, 'celt' ),
( ['out'], POINTER(VARIANT), 'rgvar' ),
( ['out'], POINTER(c_ulong), 'pceltFetched' )),
COMMETHOD([], HRESULT, 'Skip',
( ['in'], c_ulong, 'celt' )),
COMMETHOD([], HRESULT, 'Reset'),
COMMETHOD([], HRESULT, 'Clone',
( ['out'], POINTER(POINTER(IEnumVARIANT)), 'ppenum' )),
]
##from _ctypes import VARIANT_set
##import new
##VARIANT.value = property(VARIANT._get_value, new.instancemethod(VARIANT_set, None, VARIANT))
class tagEXCEPINFO(Structure):
if TYPE_CHECKING:
wCode = hints.AnnoField() # type: int
wReserved = hints.AnnoField() # type: int
bstrSource = hints.AnnoField() # type: str
bstrDescription = hints.AnnoField() # type: str
bstrHelpFile = hints.AnnoField() # type: str
dwHelpContext = hints.AnnoField() # type: int
pvReserved = hints.AnnoField() # type: Optional[int]
pfnDeferredFillIn = hints.AnnoField() # type: Optional[int]
scode = hints.AnnoField() # type: int
def __repr__(self):
return "<EXCEPINFO %s>" % \
((self.wCode, self.bstrSource, self.bstrDescription, self.bstrHelpFile, self.dwHelpContext,
self.pfnDeferredFillIn, self.scode),)
tagEXCEPINFO._fields_ = [
('wCode', WORD),
('wReserved', WORD),
('bstrSource', BSTR),
('bstrDescription', BSTR),
('bstrHelpFile', BSTR),
('dwHelpContext', DWORD),
('pvReserved', c_void_p),
## ('pfnDeferredFillIn', WINFUNCTYPE(HRESULT, POINTER(tagEXCEPINFO))),
('pfnDeferredFillIn', c_void_p),
('scode', SCODE),
]
EXCEPINFO = tagEXCEPINFO
class tagDISPPARAMS(Structure):
if TYPE_CHECKING:
rgvarg = hints.AnnoField() # type: Array[VARIANT]
rgdispidNamedArgs = hints.AnnoField() # type: _Pointer[DISPID]
cArgs = hints.AnnoField() # type: int
cNamedArgs = hints.AnnoField() # type: int
_fields_ = [
# C:/Programme/gccxml/bin/Vc71/PlatformSDK/oaidl.h 696
('rgvarg', POINTER(VARIANTARG)),
('rgdispidNamedArgs', POINTER(DISPID)),
('cArgs', UINT),
('cNamedArgs', UINT),
]
def __del__(self):
if self._b_needsfree_:
for i in range(self.cArgs):
self.rgvarg[i].value = None
DISPPARAMS = tagDISPPARAMS
DISPID_VALUE = 0
DISPID_UNKNOWN = -1
DISPID_PROPERTYPUT = -3
DISPID_NEWENUM = -4
DISPID_EVALUATE = -5
DISPID_CONSTRUCTOR = -6
DISPID_DESTRUCTOR = -7
DISPID_COLLECT = -8
if TYPE_CHECKING:
RawGetIDsOfNamesFunc = Callable[
[_byref_type, Array[c_wchar_p], int, int, Array[DISPID]], int,
]
RawInvokeFunc = Callable[
[
int, _byref_type, int, int, # dispIdMember, riid, lcid, wFlags
_UnionT[_byref_type, DISPPARAMS], # *pDispParams
_UnionT[_byref_type, VARIANT], # pVarResult
_UnionT[_byref_type, EXCEPINFO, None], # pExcepInfo
_UnionT[_byref_type, c_uint], # puArgErr
],
int
]
class IDispatch(IUnknown):
if TYPE_CHECKING:
_disp_methods_ = hints.AnnoField() # type: ClassVar[List[comtypes._DispMemberSpec]]
_GetTypeInfo = hints.AnnoField() # type: Callable[[int, int], IUnknown]
__com_GetIDsOfNames = hints.AnnoField() # type: RawGetIDsOfNamesFunc
__com_Invoke = hints.AnnoField() # type: RawInvokeFunc
_iid_ = GUID("{00020400-0000-0000-C000-000000000046}")
_methods_ = [
COMMETHOD([], HRESULT, 'GetTypeInfoCount',
(['out'], POINTER(UINT) ) ),
COMMETHOD([], HRESULT, 'GetTypeInfo',
(['in'], UINT, 'index'),
(['in'], LCID, 'lcid', 0),
# Normally, we would declare this parameter in this way:
# (['out'], POINTER(POINTER(ITypeInfo)) ) ),
# but we cannot import comtypes.typeinfo at the top level (recursive imports!).
(['out'], POINTER(POINTER(IUnknown)) ) ),
STDMETHOD(HRESULT, 'GetIDsOfNames', [POINTER(IID), POINTER(c_wchar_p),
UINT, LCID, POINTER(DISPID)]),
STDMETHOD(HRESULT, 'Invoke', [DISPID, POINTER(IID), LCID, WORD,
POINTER(DISPPARAMS), POINTER(VARIANT),
POINTER(EXCEPINFO), POINTER(UINT)]),
]
def GetTypeInfo(self, index, lcid=0):
# type: (int, int) -> hints.ITypeInfo
"""Return type information. Index 0 specifies typeinfo for IDispatch"""
import comtypes.typeinfo
result = self._GetTypeInfo(index, lcid)
return result.QueryInterface(comtypes.typeinfo.ITypeInfo)
def GetIDsOfNames(self, *names, **kw):
# type: (str, Any) -> List[int]
"""Map string names to integer ids."""
lcid = kw.pop("lcid", 0)
assert not kw
arr = (c_wchar_p * len(names))(*names)
ids = (DISPID * len(names))()
self.__com_GetIDsOfNames(riid_null, arr, len(names), lcid, ids)
return ids[:]
def _invoke(self, memid, invkind, lcid, *args):
# type: (int, int, int, Any) -> Any
var = VARIANT()
argerr = c_uint()
dp = DISPPARAMS()
if args:
array = (VARIANT * len(args))()
for i, a in enumerate(args[::-1]):
array[i].value = a
dp.cArgs = len(args)
if invkind in (DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF):
dp.cNamedArgs = 1
dp.rgdispidNamedArgs = pointer(DISPID(DISPID_PROPERTYPUT))
dp.rgvarg = array
self.__com_Invoke(memid, riid_null, lcid, invkind,
dp, var, None, argerr)
return var._get_value(dynamic=True)
def Invoke(self, dispid, *args, **kw):
# type: (int, Any, Any) -> Any
"""Invoke a method or property."""
# Memory management in Dispatch::Invoke calls:
# http://msdn.microsoft.com/library/en-us/automat/htm/chap5_4x2q.asp
# Quote:
# The *CALLING* code is responsible for releasing all strings and
# objects referred to by rgvarg[ ] or placed in *pVarResult.
#
# For comtypes this is handled in DISPPARAMS.__del__ and VARIANT.__del__.
_invkind = kw.pop("_invkind", 1) # DISPATCH_METHOD
_lcid = kw.pop("_lcid", 0)
if kw:
raise ValueError("named parameters not yet implemented")
result = VARIANT()
excepinfo = EXCEPINFO()
argerr = c_uint()
if _invkind in (DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF): # propput
array = (VARIANT * len(args))()
for i, a in enumerate(args[::-1]):
array[i].value = a
dp = DISPPARAMS()
dp.cArgs = len(args)
dp.cNamedArgs = 1
dp.rgvarg = array
dp.rgdispidNamedArgs = pointer(DISPID(DISPID_PROPERTYPUT))
else:
array = (VARIANT * len(args))()
for i, a in enumerate(args[::-1]):
array[i].value = a
dp = DISPPARAMS()
dp.cArgs = len(args)
dp.cNamedArgs = 0
dp.rgvarg = array
try:
self.__com_Invoke(dispid, riid_null, _lcid, _invkind, byref(dp),
byref(result), byref(excepinfo), byref(argerr))
except COMError as err:
(hresult, text, details) = err.args
if hresult == DISP_E_EXCEPTION:
details = (excepinfo.bstrDescription, excepinfo.bstrSource,
excepinfo.bstrHelpFile, excepinfo.dwHelpContext,
excepinfo.scode)
raise COMError(hresult, text, details)
elif hresult == DISP_E_PARAMNOTFOUND:
# MSDN says: You get the error DISP_E_PARAMNOTFOUND
# when you try to set a property and you have not
# initialized the cNamedArgs and rgdispidNamedArgs
# elements of your DISPPARAMS structure.
#
# So, this looks like a bug.
raise COMError(hresult, text, argerr.value)
elif hresult == DISP_E_TYPEMISMATCH:
# MSDN: One or more of the arguments could not be
# coerced.
#
# Hm, should we raise TypeError, or COMError?
raise COMError(hresult, text,
("TypeError: Parameter %s" % (argerr.value + 1),
args))
raise
return result._get_value(dynamic=True)
# XXX Would separate methods for _METHOD, _PROPERTYGET and _PROPERTYPUT be better?
################################################################
# safearrays
# XXX Only one-dimensional arrays are currently implemented
# map ctypes types to VARTYPE values
_arraycode_to_vartype = {
"d": VT_R8,
"f": VT_R4,
"l": VT_I4,
"i": VT_INT,
"h": VT_I2,
"b": VT_I1,
"I": VT_UINT,
"L": VT_UI4,
"H": VT_UI2,
"B": VT_UI1,
}
_ctype_to_vartype = {
c_byte: VT_I1,
c_ubyte: VT_UI1,
c_short: VT_I2,
c_ushort: VT_UI2,
c_long: VT_I4,
c_ulong: VT_UI4,
c_float: VT_R4,
c_double: VT_R8,
c_longlong: VT_I8,
c_ulonglong: VT_UI8,
VARIANT_BOOL: VT_BOOL,
BSTR: VT_BSTR,
VARIANT: VT_VARIANT,
# SAFEARRAY(VARIANT *)
#
# It is unlear to me if this is allowed or not. Apparently there
# are typelibs that define such an argument type, but it may be
# that these are buggy.
#
# Point is that SafeArrayCreateEx(VT_VARIANT|VT_BYREF, ..) fails.
# The MSDN docs for SafeArrayCreate() have a notice that neither
# VT_ARRAY not VT_BYREF may be set, this notice is missing however
# for SafeArrayCreateEx().
#
# We have this code here to make sure that comtypes can import
# such a typelib, although calling ths method will fail because
# such an array cannot be created.
POINTER(VARIANT): VT_BYREF|VT_VARIANT,
# This is needed to import Esri ArcObjects (esriSystem.olb).
POINTER(BSTR): VT_BYREF|VT_BSTR,
# These are not yet implemented:
## POINTER(IUnknown): VT_UNKNOWN,
## POINTER(IDispatch): VT_DISPATCH,
}
_vartype_to_ctype = {}
for c, v in _ctype_to_vartype.items():
_vartype_to_ctype[v] = c
_vartype_to_ctype[VT_INT] = _vartype_to_ctype[VT_I4]
_vartype_to_ctype[VT_UINT] = _vartype_to_ctype[VT_UI4]
_ctype_to_vartype[c_char] = VT_UI1
try:
from comtypes.safearray import _midlSAFEARRAY
except (ImportError, AttributeError):
pass
__known_symbols__ = [
'CURRENCY', 'CY', 'tagCY', 'DECIMAL', 'tagDEC', 'DISPATCH_METHOD',
'DISPATCH_PROPERTYGET', 'DISPATCH_PROPERTYPUT', 'DISPATCH_PROPERTYPUTREF',
'DISPID', 'DISPID_COLLECT', 'DISPID_CONSTRUCTOR', 'DISPID_DESTRUCTOR',
'DISPID_EVALUATE', 'DISPID_NEWENUM', 'DISPID_PROPERTYPUT',
'DISPID_UNKNOWN', 'DISPID_VALUE', 'DISPPARAMS', 'tagDISPPARAMS',
'EXCEPINFO', 'tagEXCEPINFO', 'IDispatch', 'IEnumVARIANT', 'IID_NULL',
'INVOKE_FUNC', 'INVOKE_PROPERTYGET', 'INVOKE_PROPERTYPUT',
'INVOKE_PROPERTYPUTREF', 'INVOKEKIND', 'tagINVOKEKIND', '_midlSAFEARRAY',
'SCODE', '_SysAllocStringLen', 'VARENUM', 'VARIANT','tagVARIANT',
'VARIANTARG', '_VariantChangeType', '_VariantClear', '_VariantCopy',
'_VariantCopyInd', 'VARTYPE', 'VT_ARRAY', 'VT_BLOB', 'VT_BLOB_OBJECT',
'VT_BOOL', 'VT_BSTR', 'VT_BSTR_BLOB', 'VT_BYREF', 'VT_CARRAY', 'VT_CF',
'VT_CLSID', 'VT_CY', 'VT_DATE', 'VT_DECIMAL', 'VT_DISPATCH', 'VT_EMPTY',
'VT_ERROR', 'VT_FILETIME', 'VT_HRESULT', 'VT_I1', 'VT_I2', 'VT_I4',
'VT_I8', 'VT_ILLEGAL', 'VT_ILLEGALMASKED', 'VT_INT', 'VT_INT_PTR',
'VT_LPSTR', 'VT_LPWSTR', 'VT_NULL', 'VT_PTR', 'VT_R4', 'VT_R8',
'VT_RECORD', 'VT_RESERVED', 'VT_SAFEARRAY', 'VT_STORAGE',
'VT_STORED_OBJECT', 'VT_STREAM', 'VT_STREAMED_OBJECT', 'VT_TYPEMASK',
'VT_UI1', 'VT_UI2', 'VT_UI4', 'VT_UI8', 'VT_UINT', 'VT_UINT_PTR',
'VT_UNKNOWN', 'VT_USERDEFINED', 'VT_VARIANT', 'VT_VECTOR',
'VT_VERSIONED_STREAM', 'VT_VOID',
]