forked from Travis-Sun/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyIDispatch.cpp
More file actions
787 lines (713 loc) · 26.6 KB
/
Copy pathPyIDispatch.cpp
File metadata and controls
787 lines (713 loc) · 26.6 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
// PyIDispatch and PyIDispathEx implementation
// @doc
#include "stdafx.h"
#include "PythonCOM.h"
static BOOL HandledDispatchFailure(HRESULT hr, EXCEPINFO *einfo, UINT nArgErr, UINT cArgs)
{
if ( hr == DISP_E_EXCEPTION )
{
if ( einfo->scode != DISP_E_TYPEMISMATCH &&
einfo->scode != DISP_E_PARAMNOTFOUND )
nArgErr = (UINT)-1;
else
nArgErr = cArgs - nArgErr; /* convert to usable index */
PyCom_BuildPyExceptionFromEXCEPINFO(hr, einfo, nArgErr);
return TRUE;
}
if ( FAILED(hr) )
{
if ( hr != DISP_E_TYPEMISMATCH && hr != DISP_E_PARAMNOTFOUND )
nArgErr =(UINT)-1;
else
nArgErr = cArgs - nArgErr; /* convert to usable index */
PyCom_BuildPyExceptionFromEXCEPINFO(hr, NULL, nArgErr);
return TRUE;
}
return FALSE;
}
PyIDispatch::PyIDispatch(IUnknown *pDisp) :
PyIUnknown(pDisp)
{
ob_type = &type;
}
PyIDispatch::~PyIDispatch()
{
}
/*static*/ IDispatch *PyIDispatch::GetI(PyObject *self)
{
return (IDispatch *)PyIUnknown::GetI(self);
}
// @pymethod (int, ...)/int|PyIDispatch|GetIDsOfNames|Get the DISPID for the passed names.
PyObject *PyIDispatch::GetIDsOfNames(PyObject *self, PyObject *args)
{
// @rdesc If the first parameter is a sequence, the result will be a tuple of integers
// for each name in the sequence. If the first parameter is a single string, the result
// is a single integer with the ID of requested item.
UINT i;
// @pyparm string|name||A name to query for
// @pyparmalt1 [string, ...]|[name, ...]||A sequence of string names to query
// @comm Currently the LCID can not be specified, and LOCALE_SYSTEM_DEFAULT is used.
int argc = PyTuple_GET_SIZE(args);
if ( argc < 1 )
return PyErr_Format(PyExc_TypeError, "At least one argument must be supplied");
LCID lcid = LOCALE_SYSTEM_DEFAULT;
UINT offset = 0;
if ( argc > 1 ){
PyObject *ob = PyTuple_GET_ITEM(args, 0);
lcid=PyLong_AsLong(ob);
if (lcid==-1 && PyErr_Occurred()){
PyErr_Clear();
lcid=LOCALE_SYSTEM_DEFAULT;
}
else
offset = 1;
}
UINT cNames = argc - offset;
OLECHAR FAR* FAR* rgszNames = new LPOLESTR[cNames];
for ( i = 0 ; i < cNames; ++i )
{
PyObject *ob = PySequence_GetItem(args, i + offset);
if ( !ob )
{
for (;i>0;i--)
PyWinObject_FreeBstr(rgszNames[i-1]);
delete [] rgszNames;
return NULL;
}
if (!PyWinObject_AsBstr(ob, rgszNames+i)) {
for (;i>0;i--)
PyWinObject_FreeBstr(rgszNames[i-1]);
delete [] rgszNames;
return NULL;
}
Py_DECREF(ob);
}
DISPID FAR* rgdispid = new DISPID[cNames];
IDispatch *pMyDispatch = GetI(self);
if (pMyDispatch==NULL) return NULL;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatch->GetIDsOfNames(IID_NULL, rgszNames, cNames, lcid, rgdispid);
PY_INTERFACE_POSTCALL;
for (i=0;i<cNames;i++)
PyWinObject_FreeBstr(rgszNames[i]);
delete [] rgszNames;
if ( FAILED(hr) )
return PyCom_BuildPyException(hr, pMyDispatch, IID_IDispatch);
PyObject *result;
/* if we have just one name, then return a single DISPID (int) */
if ( cNames == 1 )
{
result = PyInt_FromLong(rgdispid[0]);
}
else
{
result = PyTuple_New(cNames);
if ( result )
{
for ( i = 0; i < cNames; ++i )
{
PyObject *ob = PyInt_FromLong(rgdispid[i]);
if ( !ob )
{
delete [] rgdispid;
return NULL;
}
PyTuple_SET_ITEM(result, i, ob);
}
}
}
delete [] rgdispid;
return result;
}
// Convert a PyTuple object into a DISPPARAM structure.
// numArgs specifies which of the LAST args in the tuple are valid.
// To convert all args, pass len(args)
static BOOL PyCom_MakeUntypedDISPPARAMS( PyObject *args, int numArgs, WORD wFlags, DISPPARAMS *pParm, PythonOleArgHelper **ppHelpers)
{
int argc = PyObject_Length(args);
DISPID dispidNamed = DISPID_PROPERTYPUT;
// Clean initialize
pParm->rgvarg = NULL;
pParm->rgdispidNamedArgs = NULL;
pParm->cArgs = numArgs;
pParm->cNamedArgs = 0;
if ( pParm->cArgs )
{
pParm->rgvarg = new VARIANTARG[pParm->cArgs];
PythonOleArgHelper *pHelpers = *ppHelpers = new PythonOleArgHelper[pParm->cArgs];
for ( UINT i = 0; i < pParm->cArgs; ++i )
{
VariantInit(&pParm->rgvarg[i]);
// args in reverse order.
if (!pHelpers[i].MakeObjToVariant(PyTuple_GET_ITEM(args, argc-i-1), &pParm->rgvarg[i], NULL) )
{
if ( !PyErr_Occurred() )
PyErr_Format(PyExc_TypeError, "Bad argument");
while ( i-- > 0 )
VariantClear(&pParm->rgvarg[i]);
delete [] pParm->rgvarg;
delete [] pHelpers;
return FALSE;
}
}
/* puts and putrefs need a named argument */
if ( wFlags & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF) )
{
pParm->rgdispidNamedArgs = new DISPID[1];
pParm->rgdispidNamedArgs[0] = DISPID_PROPERTYPUT;
pParm->cNamedArgs = 1;
}
} else {
*ppHelpers = NULL;
}
return TRUE;
}
static BOOL PyCom_FinishUntypedDISPPARAMS( DISPPARAMS *pParm, PythonOleArgHelper *pHelpers )
{
BOOL ok = TRUE;
if (pHelpers) {
for ( UINT i = 0; i < pParm->cArgs; ++i ) {
// Do magic so PyVariant objects get updated if appropriate.
if (pHelpers[i].m_bIsOut && pHelpers[i].m_pyVariant) {
PyObject *tmp = pHelpers[i].MakeVariantToObj(pParm->rgvarg+(pParm->cArgs-i-1));
if (!tmp) {
ok = FALSE;
break;
}
Py_DECREF(tmp);
}
}
delete [] pHelpers;
}
if ( pParm->rgvarg )
{
for ( UINT i = pParm->cArgs; i--; )
VariantClear(&pParm->rgvarg[i]);
delete [] pParm->rgvarg;
}
delete [] pParm->rgdispidNamedArgs;
return ok;
}
// @pymethod object|PyIDispatch|Invoke|Invokes a DISPID, using the passed arguments.
PyObject * PyIDispatch::Invoke(PyObject *self, PyObject *args)
{
/* Invoke(dispid, lcid, wflags, bResultWanted, arg1, arg2...) */
// should be no need to clear this error - but for the next few release
// I will keep it in place for release builds, and assert in debug
#ifdef _DEBUG
assert(!PyErr_Occurred());
#else
PyErr_Clear();
#endif
int argc = PyObject_Length(args);
if ( argc == -1 )
return NULL;
if ( argc < 4 )
return PyErr_Format(PyExc_TypeError, "not enough arguments (at least 4 needed)");
// @pyparm int|dispid||The dispid to use. Typically this value will come from <om PyIDispatch.GetIDsOfNames> or from a type library.
DISPID dispid = PyInt_AsLong(PyTuple_GET_ITEM(args, 0));
// @pyparm int|lcid||The locale id to use.
LCID lcid = PyInt_AsLong(PyTuple_GET_ITEM(args, 1));
// @pyparm int|flags||The flags for the call. The following flags can be used.
// @flagh Flag|Description
// @flag DISPATCH_METHOD|The member is invoked as a method. If a property has the same name, both this and the DISPATCH_PROPERTYGET flag may be set.
// @flag DISPATCH_PROPERTYGET|The member is retrieved as a property or data member.
// @flag DISPATCH_PROPERTYPUT|The member is changed as a property or data member.
// @flag DISPATCH_PROPERTYPUTREF|The member is changed by a reference assignment, rather than a value assignment. This flag is valid only when the property accepts a reference to an object.
UINT wFlags = PyInt_AsLong(PyTuple_GET_ITEM(args, 2));
// @pyparm int|bResultWanted||Indicates if the result of the call should be requested.
// @pyparm object, ...|params, ...||The parameters to pass.
BOOL bResultWanted = (BOOL)PyInt_AsLong(PyTuple_GET_ITEM(args, 3));
if ( PyErr_Occurred() )
return NULL;
IDispatch *pMyDispatch = GetI(self);
if ( pMyDispatch==NULL )
return NULL;
DISPPARAMS dispparams;
PythonOleArgHelper *helpers;
if (!PyCom_MakeUntypedDISPPARAMS(args, argc-4, wFlags, &dispparams, &helpers ))
return NULL;
VARIANT varResult;
VARIANT *pVarResultUse;
if ( bResultWanted ) {
VariantInit(&varResult);
pVarResultUse = &varResult;
} else
pVarResultUse = NULL;
// initialize EXCEPINFO struct
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
UINT nArgErr = (UINT)-1; // initialize to invalid arg
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatch->Invoke(dispid, IID_NULL, lcid, wFlags, &dispparams, pVarResultUse, &excepInfo, &nArgErr);
PY_INTERFACE_POSTCALL;
if (!PyCom_FinishUntypedDISPPARAMS(&dispparams, helpers) ||
HandledDispatchFailure(hr, &excepInfo, nArgErr, dispparams.cArgs) ) {
if ( pVarResultUse )
VariantClear(pVarResultUse);
return NULL;
}
// @rdesc If the bResultWanted parameter is False, then the result will be None.
// Otherwise, the result is determined by the COM object itself (and may still be None)
PyObject *result;
if (pVarResultUse) {
result = PyCom_PyObjectFromVariant(pVarResultUse);
VariantClear(pVarResultUse);
} else {
result = Py_None;
Py_INCREF(result);
}
return result;
}
// @pymethod object|PyIDispatch|InvokeTypes|Invokes a DISPID, using the passed arguments and type descriptions.
PyObject * PyIDispatch::InvokeTypes(PyObject *self, PyObject *args)
{
/* InvokeType(dispid, lcid, wflags, ELEMDESC resultType, ELEMDESC[] argTypes, arg1, arg2...) */
PyErr_Clear();
int argc = PyObject_Length(args);
if ( argc == -1 )
return NULL;
if ( argc < 5 )
return PyErr_Format(PyExc_TypeError, "not enough arguments (at least 5 needed)");
// @pyparm int|dispid||The dispid to use. Please see <om PyIDispatch.Invoke>.
DISPID dispid = PyInt_AsLong(PyTuple_GET_ITEM(args, 0));
// @pyparm int|lcid||The locale ID. Please see <om PyIDispatch.Invoke>.
LCID lcid = PyInt_AsLong(PyTuple_GET_ITEM(args, 1));
// @pyparm int|wFlags||Flags for the call. Please see <om PyIDispatch.Invoke>.
UINT wFlags = PyInt_AsLong(PyTuple_GET_ITEM(args, 2));
// @pyparm tuple|resultTypeDesc||A tuple describing the type of the
// result. See the comments for more information.
PyObject *resultElemDesc = PyTuple_GET_ITEM(args, 3);
// @pyparm (tuple, ...)|typeDescs||A sequence of tuples describing
// the types of the parameters for the function. See the comments
// for more information.
PyObject *argsElemDescArray = PyTuple_GET_ITEM(args, 4);
// @pyparm object, ...|args||The args to the function.
if ( PyErr_Occurred() )
return NULL;
int numArgs;
int argTypesLen = PyObject_Length(argsElemDescArray);
if (!PyTuple_Check(argsElemDescArray) || argTypesLen<argc-5)
return PyErr_Format(PyExc_TypeError, "The array of argument types must be a tuple whose size is <= to the number of arguments.");
// See how many _real_ entries - count until end or
// first param marked as Missing.
for (numArgs = 0;numArgs<argc-5; numArgs++) {
if (PyTuple_GET_ITEM(args, numArgs+5)->ob_type==&PyOleMissingType) {
break;
}
}
// these will all be cleared before returning
PythonOleArgHelper *ArgHelpers = NULL;
PyObject *result = NULL;
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPPARAMS dispparams = { NULL, NULL, 0, 0 };
PythonOleArgHelper resultArgHelper;
// This gets confusing. If we have typeinfo for a byref arg, but the
// arg is not specified by the user, then we _do_ present the arg to
// COM. If the arg does not exist, and it is not byref, then we do
// not present it _at_all - ie, the arg count does _not_ include it.
// So - first we must loop over the arg types, using this info to
// decide how big the arg array is!
// If we have type info for an arg but not specified by the user, we will still process
// the arg fully.
// Note numArgs can not be > argTypesLen (as checked above)
UINT numArgArray = 0;
UINT i;
if (argTypesLen>0) {
ArgHelpers = new PythonOleArgHelper[argTypesLen]; // new may! except.
if (ArgHelpers==NULL) {
PyErr_SetString(PyExc_MemoryError, "Allocating ArgHelpers array");
return NULL;
}
for ( i = 0; i < (UINT)argTypesLen; i++ ) {
if (!ArgHelpers[i].ParseTypeInformation(PyTuple_GET_ITEM(argsElemDescArray,i)))
goto error;
// We ignore "in" params specified as "Missing", but
// for byref (ie, "IsOut") args we still must process it.
if (i<(UINT)numArgs || ArgHelpers[i].m_bIsOut)
numArgArray++;
}
}
dispparams.cArgs = numArgArray;
if ( dispparams.cArgs ) {
dispparams.rgvarg = new VARIANTARG[dispparams.cArgs];
if (dispparams.rgvarg==NULL) {
PyErr_SetString(PyExc_MemoryError, "Allocating dispparams.rgvarg array");
goto error;
}
for ( i = dispparams.cArgs; i--; )
VariantInit(&dispparams.rgvarg[i]);
for ( i = 0; i < dispparams.cArgs; ++i ) {
// args in reverse order.
// arg-helpers in normal order.
UINT offset = dispparams.cArgs - i - 1;
// See if the user actually specified this arg.
PyObject *arg = i>=(UINT)numArgs ? Py_None : PyTuple_GET_ITEM(args, i+5);
if ( !ArgHelpers[i].MakeObjToVariant(arg, &dispparams.rgvarg[offset], PyTuple_GET_ITEM(argsElemDescArray,i)) )
goto error;
}
}
/* puts and putrefs need a named argument */
if ( wFlags & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF) )
{
dispparams.rgdispidNamedArgs = &dispidNamed;
dispparams.cNamedArgs = 1;
}
if (!resultArgHelper.ParseTypeInformation(resultElemDesc)) {
PyCom_BuildInternalPyException("The return type information could not be parsed");
goto error;
}
BOOL bResultWanted;
bResultWanted = (resultArgHelper.m_reqdType != VT_VOID && resultArgHelper.m_reqdType != VT_EMPTY);
VARIANT varResult;
VARIANT *pVarResultUse;
if ( bResultWanted ) {
VariantInit(&varResult);
pVarResultUse = &varResult;
} else
pVarResultUse = NULL;
// initialize EXCEPINFO struct
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
HRESULT hr;
UINT nArgErr;
IDispatch *pMyDispatch;
pMyDispatch = GetI(self);
if (pMyDispatch==NULL) goto error;
nArgErr = (UINT)-1; // initialize to invalid arg
{
PY_INTERFACE_PRECALL;
hr = pMyDispatch->Invoke(dispid, IID_NULL, lcid, wFlags, &dispparams, pVarResultUse, &excepInfo, &nArgErr);
PY_INTERFACE_POSTCALL;
}
if ( !HandledDispatchFailure(hr, &excepInfo, nArgErr, dispparams.cArgs) )
{
// Now get fancy with the args. Any args specified as BYREF get returned
// to Python.
int retSize = 0;
if (pVarResultUse)
retSize++;
for (UINT arg=0;arg<numArgArray;arg++)
if (ArgHelpers[arg].m_bIsOut)
retSize++;
if (retSize==0) { // result is None.
result = Py_None;
Py_INCREF(result);
} else if (retSize==1) { // result is a simple object.
if (pVarResultUse) { // only retval is actual result.
result = resultArgHelper.MakeVariantToObj(pVarResultUse);
} else { // only result in one of the params - seek it.
for (UINT arg=0;arg<numArgArray;arg++) {
if (ArgHelpers[arg].m_bIsOut) {
result = ArgHelpers[arg].MakeVariantToObj(dispparams.rgvarg+(numArgArray-arg-1));
break;
}
}
}
} else { // result is a tuple.
result = PyTuple_New(retSize);
int tupleItem = 0;
if (pVarResultUse) {
PyTuple_SetItem(result, tupleItem++, resultArgHelper.MakeVariantToObj(pVarResultUse));
}
// Loop over all the args, reverse order, setting the byrefs.
for (int arg=numArgArray-1;arg>=0;arg--)
if (ArgHelpers[numArgArray-arg-1].m_bIsOut)
PyTuple_SetItem(result, tupleItem++, ArgHelpers[numArgArray-arg-1].MakeVariantToObj(dispparams.rgvarg+(arg)));
}
}
if (pVarResultUse) VariantClear(pVarResultUse); // wipe the result.
error:
if ( dispparams.rgvarg )
{
for ( i = dispparams.cArgs; i--; )
VariantClear(&dispparams.rgvarg[i]);
delete [] dispparams.rgvarg;
}
delete [] ArgHelpers;
return result;
// @comm The Microsoft documentation for IDispatch should be used for all
// params except 'resultTypeDesc' and 'typeDescs'. 'resultTypeDesc' describes
// the return value of the function, and is a tuple of (type_id, flags).
// 'typeDescs' describes the type of each parameters, and is a list of the
// same (type_id, flags) tuple.
// @flagh item|Description
// @flag type_id|A valid "variant type" constant (eg, VT_I4 \| VT_ARRAY, VT_DATE, etc - see VARIANT at MSDN).
// @flag flags|One of the PARAMFLAG constants (eg, PARAMFLAG_FIN, PARAMFLAG_FOUT etc - see PARAMFLAG at MSDN).
// @ex An example from the makepy generated file for Word|
//
// class Cells(DispatchBaseClass):
// ...
// def SetWidth(self, ColumnWidth=..., RulerStyle=...):
// return self._oleobj_.InvokeTypes(202, LCID, 1, (24, 0), ((4, 1), (3, 1)),...)
// @ex The interesting bits are|
// resultTypeDesc: (24, 0) - (VT_VOID, <no flags>)
// typeDescs: ((4, 1), (3, 1)) - ((VT_R4, PARAMFLAG_FIN), (VT_I4, PARAMFLAG_FIN))
// @ex So, in this example, the function returns no value and takes 2 "in"
// params - ColumnWidth is a float, and RulerStule is an int.|
}
// @pymethod <o PyITypeInfo>|PyIDispatch|GetTypeInfo|Get type information for the object.
PyObject *PyIDispatch::GetTypeInfo(PyObject *self, PyObject *args)
{
LCID locale = LOCALE_USER_DEFAULT;
int index = 0;
// @pyparm int|locale|LOCALE_USER_DEFAULT|The locale to use.
// @pyparm int|index|0|The index of the typelibrary to fetch.
// Note that these params are reversed from the win32 call.
if (!PyArg_ParseTuple(args, "|ii:GetTypeInfo", &locale, &index))
return NULL;
IDispatch *pMyDispatch = GetI(self);
if (pMyDispatch==NULL) return NULL;
ITypeInfo *pti = NULL;
PY_INTERFACE_PRECALL;
SCODE sc = pMyDispatch->GetTypeInfo(index, locale, &pti);
PY_INTERFACE_POSTCALL;
if (sc!=S_OK) // S_OK is only acceptable result.
return PyCom_BuildPyException(sc, pMyDispatch, IID_IDispatch);
return PyCom_PyObjectFromIUnknown(pti, IID_ITypeInfo);
}
// @pymethod int|PyIDispatch|GetTypeInfoCount|Retrieves the number of <o PyITypeInfo>s the object provides.
PyObject *PyIDispatch::GetTypeInfoCount(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args,":GetTypeInfoCount"))
return NULL;
unsigned int ret;
IDispatch *pMyDispatch = GetI(self);
if (pMyDispatch==NULL) return NULL;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatch->GetTypeInfoCount(&ret);
PY_INTERFACE_POSTCALL;
if ( FAILED(hr) )
return PyCom_BuildPyException(hr, pMyDispatch, IID_IDispatch);
return Py_BuildValue("i", ret);
}
// @object PyIDispatch|A OLE automation client object.
static struct PyMethodDef PyIDispatch_methods[] =
{
{"Invoke", PyIDispatch::Invoke, 1}, // @pymeth Invoke|Invokes a DISPID, using the passed arguments.
{"InvokeTypes", PyIDispatch::InvokeTypes, 1}, // @pymeth InvokeTypes|Invokes a DISPID, using the passed arguments and type descriptions.
{"GetIDsOfNames", PyIDispatch::GetIDsOfNames, 1}, // @pymeth GetIDsOfNames|Get the DISPID for the passed names.
{"GetTypeInfo", PyIDispatch::GetTypeInfo, 1}, // @pymeth GetTypeInfo|Get type information for the object.
{"GetTypeInfoCount",PyIDispatch::GetTypeInfoCount, 1}, // @pymeth GetTypeInfoCount|Retrieves the number of <o PyITypeInfo>s the object provides.
{NULL, NULL}
};
PyComTypeObject PyIDispatch::type("PyIDispatch",
&PyIUnknown::type, // @base PyIDispatch|PyIUnknown
sizeof(PyIDispatch),
PyIDispatch_methods,
GET_PYCOM_CTOR(PyIDispatch));
#ifndef NO_PYCOM_IDISPATCHEX
//////////////////////////////////////////////////////////////////
//
// PyIDispatchEx
PyIDispatchEx::PyIDispatchEx(IUnknown *pDisp) :
PyIDispatch(pDisp)
{
ob_type = &type;
}
PyIDispatchEx::~PyIDispatchEx()
{
}
/*static*/ IDispatchEx *PyIDispatchEx::GetI(PyObject *self)
{
return (IDispatchEx *)PyIDispatch::GetI(self);
}
// @pymethod int|PyIDispatchEx|GetDispID|Returns the member id for a name
PyObject *PyIDispatchEx::GetDispID(PyObject *self, PyObject *args)
{
long fdex;
PyObject *obName;
if (!PyArg_ParseTuple(args, "Ol:GetDispId",
&obName, // @pyparm <o PyUnicode>|name||Passed in name to be mapped
&fdex)) // @pyparm int|fdex||Determines the options for obtaining the member identifier. This can be a combination of the fdex* constants:
return NULL;
PyWin_AutoFreeBstr name;
if (!PyWinObject_AsAutoFreeBstr(obName, &name))
return NULL;
IDispatchEx *pMyDispatchEx = GetI(self);
if (pMyDispatchEx==NULL) return NULL;
DISPID dispid;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatchEx->GetDispID(name, (DWORD)fdex, &dispid);
PY_INTERFACE_POSTCALL;
if (FAILED(hr)) return SetPythonCOMError(self, hr);
return PyInt_FromLong(dispid);
}
// @pymethod object|PyIDispatchEx|InvokeEx|Provides access to properties and methods exposed by a <o PyIDispatchEx> object.
PyObject *PyIDispatchEx::InvokeEx(PyObject *self, PyObject *args)
{
long dispid;
long lcid;
int flags;
PyObject *invokeArgs;
PyObject *types = Py_None;
PyObject *obReturnDesc = Py_None;
PyObject *obCaller = Py_None;
if (!PyArg_ParseTuple(args, "lliO|OOO:InvokeEx",
&dispid, // @pyparm int|dispid||
&lcid, // @pyparm int|lcid||
&flags, // @pyparm int|flags||
&invokeArgs, // @pyparm [object, ...]|args||The arguments.
&types, // @pyparm [object, ...]|types|None|A tuple of type description object, or None if type descriptions are not available.
&obReturnDesc, // @pyparm object\|int|returnDesc|1|If types==None, should be a BOOL indicating if the result is needed. If types is a tuple, then should a be type description.
&obCaller)) // @pyparm <o PyIServiceProvider>|serviceProvider|None|A service provider object supplied by the caller which allows the object to obtain services from the caller. Can be None.
return NULL;
if (!PyTuple_Check(invokeArgs)) {
PyErr_SetString(PyExc_TypeError, "The arguments must be a tuple.");
return NULL;
}
// TODO - We do not yet support the Type Description here
// (Im not even sure if we need it!)
if (types != Py_None || obReturnDesc != Py_None) {
PyErr_SetString(PyExc_TypeError, "Type descriptions are not yet supported.");
return NULL;
}
// TODO - Add support for PyIServiceProvider
if (obCaller != Py_None) {
PyErr_SetString(PyExc_TypeError, "If you really need IServiceProvider support, you are going to have to add it!.");
return NULL;
}
BOOL bResultWanted = TRUE;
IDispatchEx *pMyDispatch = GetI(self);
if ( pMyDispatch==NULL )
return NULL;
DISPPARAMS dispparams;
PythonOleArgHelper *helpers;
if (!PyCom_MakeUntypedDISPPARAMS(invokeArgs, PyObject_Length(invokeArgs), flags, &dispparams, &helpers ))
return NULL;
VARIANT varResult;
VARIANT *pVarResultUse;
if ( bResultWanted ) {
VariantInit(&varResult);
pVarResultUse = &varResult;
} else
pVarResultUse = NULL;
// initialize EXCEPINFO struct
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatch->InvokeEx((DISPID)dispid, (LCID)lcid, (WORD)flags, &dispparams, pVarResultUse, &excepInfo, NULL);
PY_INTERFACE_POSTCALL;
if (!PyCom_FinishUntypedDISPPARAMS(&dispparams, helpers) ||
HandledDispatchFailure(hr, &excepInfo, (UINT)-1, dispparams.cArgs) ) {
if ( pVarResultUse )
VariantClear(pVarResultUse);
return NULL;
}
PyObject *result;
if (pVarResultUse) {
result = PyCom_PyObjectFromVariant(pVarResultUse);
VariantClear(pVarResultUse);
} else {
result = Py_None;
Py_INCREF(result);
}
return result;
}
// @pymethod |PyIDispatchEx|DeleteMemberByName|
PyObject *PyIDispatchEx::DeleteMemberByName(PyObject *self, PyObject *args)
{
long fdex;
PyObject *obName;
if (!PyArg_ParseTuple(args, "Ol:DeleteMemberByName",
&obName, // @pyparm <o PyUnicode>|name||Passed in name to be mapped
&fdex)) // @pyparm int|fdex||Determines the options
return NULL;
PyWin_AutoFreeBstr name;
if (!PyWinObject_AsAutoFreeBstr(obName, &name))
return NULL;
IDispatchEx *pMyDispatchEx = GetI(self);
if (pMyDispatchEx==NULL) return NULL;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatchEx->DeleteMemberByName(name, (DWORD)fdex);
PY_INTERFACE_POSTCALL;
if (FAILED(hr)) return SetPythonCOMError(self, hr);
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod |PyIDispatchEx|DeleteMemberByDispID|
PyObject *PyIDispatchEx::DeleteMemberByDispID(PyObject *self, PyObject *args)
{
long dispid;
if (!PyArg_ParseTuple(args, "ll:DeleteMemberByDispID",
&dispid)) // @pyparm int|dispid||
return NULL;
IDispatchEx *pMyDispatchEx = GetI(self);
if (pMyDispatchEx==NULL) return NULL;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatchEx->DeleteMemberByDispID((DISPID)dispid);
PY_INTERFACE_POSTCALL;
if (FAILED(hr)) return SetPythonCOMError(self, hr);
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod int|PyIDispatchEx|GetMemberProperties|Returns mask of fdex* flags describing a member
PyObject *PyIDispatchEx::GetMemberProperties(PyObject *self, PyObject *args)
{
long fdex;
long dispid;
if (!PyArg_ParseTuple(args, "ll:GetMemberProperties",
&dispid, // @pyparm int|dispid||The member id
&fdex)) // @pyparm int|fdex||fdex* flags specifying which properties to return
return NULL;
IDispatchEx *pMyDispatchEx = GetI(self);
if (pMyDispatchEx==NULL) return NULL;
DWORD props;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatchEx->GetMemberProperties((DISPID)dispid, (DWORD)fdex, &props);
PY_INTERFACE_POSTCALL;
if (FAILED(hr)) return SetPythonCOMError(self, hr);
return PyInt_FromLong(props);
}
// @pymethod str|PyIDispatchEx|GetMemberName|Returns the name associated with a member id
PyObject *PyIDispatchEx::GetMemberName(PyObject *self, PyObject *args)
{
long dispid;
BSTR name;
if (!PyArg_ParseTuple(args, "l:GetMemberName",
&dispid)) // @pyparm int|dispid||The member id
return NULL;
IDispatchEx *pMyDispatchEx = GetI(self);
if (pMyDispatchEx==NULL) return NULL;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatchEx->GetMemberName(dispid, &name);
PY_INTERFACE_POSTCALL;
if (FAILED(hr)) return SetPythonCOMError(self, hr);
return PyWinObject_FromBstr(name, TRUE);
}
// @pymethod int|PyIDispatchEx|GetNextDispID|Enumerates member ids.
PyObject *PyIDispatchEx::GetNextDispID(PyObject *self, PyObject *args)
{
long fdex;
long dispid;
if (!PyArg_ParseTuple(args, "ll:GetNextDispID",
&fdex, // @pyparm int|fdex||Determines the options
&dispid)) // @pyparm int|dispid||Current member, or DISPID_STARTENUM to begin enumeration. GetNextDispID will retrieve the item in the enumeration after this one.
return NULL;
IDispatchEx *pMyDispatchEx = GetI(self);
if (pMyDispatchEx==NULL) return NULL;
DISPID retDispid;
PY_INTERFACE_PRECALL;
HRESULT hr = pMyDispatchEx->GetNextDispID((DWORD)fdex, (DISPID)dispid, &retDispid);
PY_INTERFACE_POSTCALL;
if (hr != S_OK) return SetPythonCOMError(self, hr);
return PyInt_FromLong(retDispid);
}
// @object PyIDispatchEx|A OLE automation client object that uses the IDispatchEx scripting interface..
static struct PyMethodDef PyIDispatchEx_methods[] =
{
{"GetDispID", PyIDispatchEx::GetDispID, 1 }, // @pymeth GetDispID|
{"InvokeEx", PyIDispatchEx::InvokeEx, 1 }, // @pymeth InvokeEx|Provides access to properties and methods exposed by a <o PyIDispatchEx> object.
{"DeleteMemberByName", PyIDispatchEx::DeleteMemberByName, 1 }, // @pymeth DeleteMemberByName|
{"DeleteMemberByDispID",PyIDispatchEx::DeleteMemberByDispID, 1 }, // @pymeth DeleteMemberByDispID|
{"GetMemberProperties", PyIDispatchEx::GetMemberProperties, 1 }, // @pymeth GetMemberProperties|
{"GetMemberName", PyIDispatchEx::GetMemberName, 1 }, // @pymeth GetMemberName|Returns the name associated with a member id
{"GetNextDispID", PyIDispatchEx::GetNextDispID, 1 }, // @pymeth GetNextDispID|Enumerates member ids.
{NULL, NULL}
};
PyComTypeObject PyIDispatchEx::type("PyIDispatchEx",
&PyIDispatch::type, // @base PyIDispatchEx|PyIDispatch
sizeof(PyIDispatchEx),
PyIDispatchEx_methods,
GET_PYCOM_CTOR(PyIDispatchEx));
#endif // NO_PYCOM_IDISPATCHEX