]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/ole/automtn.cpp | |
3 | // Purpose: OLE automation utilities | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 11/6/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998, Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if defined(__BORLANDC__) | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | // Watcom C++ gives a linker error if this is compiled in. | |
20 | // With Borland C++, all samples crash if this is compiled in. | |
21 | #if wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__) | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/log.h" | |
25 | #include "wx/math.h" | |
26 | #endif | |
27 | ||
28 | #define _FORCENAMELESSUNION | |
29 | #include "wx/msw/private.h" | |
30 | #include "wx/msw/ole/oleutils.h" | |
31 | #include "wx/msw/ole/automtn.h" | |
32 | ||
33 | #ifdef __WXWINCE__ | |
34 | #include "wx/msw/wince/time.h" | |
35 | #else | |
36 | #include <time.h> | |
37 | #endif | |
38 | ||
39 | #include <wtypes.h> | |
40 | #include <unknwn.h> | |
41 | ||
42 | #include <ole2.h> | |
43 | #define _huge | |
44 | ||
45 | #ifndef __WXWINCE__ | |
46 | #include <ole2ver.h> | |
47 | #endif | |
48 | ||
49 | #include <oleauto.h> | |
50 | ||
51 | #if wxUSE_DATETIME | |
52 | #include "wx/datetime.h" | |
53 | #endif // wxUSE_TIMEDATE | |
54 | ||
55 | static void ClearVariant(VARIANTARG *pvarg) ; | |
56 | static void ReleaseVariant(VARIANTARG *pvarg) ; | |
57 | // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr); | |
58 | ||
59 | /* | |
60 | * wxAutomationObject | |
61 | */ | |
62 | ||
63 | wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr) | |
64 | { | |
65 | m_dispatchPtr = dispatchPtr; | |
66 | } | |
67 | ||
68 | wxAutomationObject::~wxAutomationObject() | |
69 | { | |
70 | if (m_dispatchPtr) | |
71 | { | |
72 | ((IDispatch*)m_dispatchPtr)->Release(); | |
73 | m_dispatchPtr = NULL; | |
74 | } | |
75 | } | |
76 | ||
77 | #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i])) | |
78 | ||
79 | // For Put/Get, no named arguments are allowed. | |
80 | bool wxAutomationObject::Invoke(const wxString& member, int action, | |
81 | wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const | |
82 | { | |
83 | if (!m_dispatchPtr) | |
84 | return false; | |
85 | ||
86 | // nonConstMember is necessary because the wxString class doesn't have enough consts... | |
87 | wxString nonConstMember(member); | |
88 | ||
89 | int ch = nonConstMember.Find('.'); | |
90 | if (ch != -1) | |
91 | { | |
92 | // Use dot notation to get the next object | |
93 | wxString member2(nonConstMember.Left((size_t) ch)); | |
94 | wxString rest(nonConstMember.Right(nonConstMember.length() - ch - 1)); | |
95 | wxAutomationObject obj; | |
96 | if (!GetObject(obj, member2)) | |
97 | return false; | |
98 | return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs); | |
99 | } | |
100 | ||
101 | VARIANTARG vReturn; | |
102 | ClearVariant(& vReturn); | |
103 | ||
104 | VARIANTARG* vReturnPtr = & vReturn; | |
105 | ||
106 | // Find number of names args | |
107 | int namedArgCount = 0; | |
108 | int i; | |
109 | for (i = 0; i < noArgs; i++) | |
110 | if (!INVOKEARG(i).GetName().IsNull()) | |
111 | { | |
112 | namedArgCount ++; | |
113 | } | |
114 | ||
115 | int namedArgStringCount = namedArgCount + 1; | |
116 | BSTR* argNames = new BSTR[namedArgStringCount]; | |
117 | argNames[0] = wxConvertStringToOle(member); | |
118 | ||
119 | // Note that arguments are specified in reverse order | |
120 | // (all totally logical; hey, we're dealing with OLE here.) | |
121 | ||
122 | int j = 0; | |
123 | for (i = 0; i < namedArgCount; i++) | |
124 | { | |
125 | if (!INVOKEARG(i).GetName().IsNull()) | |
126 | { | |
127 | argNames[(namedArgCount-j)] = wxConvertStringToOle(INVOKEARG(i).GetName()); | |
128 | j ++; | |
129 | } | |
130 | } | |
131 | ||
132 | // + 1 for the member name, + 1 again in case we're a 'put' | |
133 | DISPID* dispIds = new DISPID[namedArgCount + 2]; | |
134 | ||
135 | HRESULT hr; | |
136 | DISPPARAMS dispparams; | |
137 | unsigned int uiArgErr; | |
138 | EXCEPINFO excep; | |
139 | ||
140 | // Get the IDs for the member and its arguments. GetIDsOfNames expects the | |
141 | // member name as the first name, followed by argument names (if any). | |
142 | hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, argNames, | |
143 | 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, dispIds); | |
144 | if (FAILED(hr)) | |
145 | { | |
146 | // ShowException(szMember, hr, NULL, 0); | |
147 | delete[] argNames; | |
148 | delete[] dispIds; | |
149 | return false; | |
150 | } | |
151 | ||
152 | // if doing a property put(ref), we need to adjust the first argument to have a | |
153 | // named arg of DISPID_PROPERTYPUT. | |
154 | if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF)) | |
155 | { | |
156 | namedArgCount = 1; | |
157 | dispIds[1] = DISPID_PROPERTYPUT; | |
158 | vReturnPtr = (VARIANTARG*) NULL; | |
159 | } | |
160 | ||
161 | // Convert the wxVariants to VARIANTARGs | |
162 | VARIANTARG* oleArgs = new VARIANTARG[noArgs]; | |
163 | for (i = 0; i < noArgs; i++) | |
164 | { | |
165 | // Again, reverse args | |
166 | if (!wxConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i])) | |
167 | { | |
168 | delete[] argNames; | |
169 | delete[] dispIds; | |
170 | delete[] oleArgs; | |
171 | return false; | |
172 | } | |
173 | } | |
174 | ||
175 | dispparams.rgdispidNamedArgs = dispIds + 1; | |
176 | dispparams.rgvarg = oleArgs; | |
177 | dispparams.cArgs = noArgs; | |
178 | dispparams.cNamedArgs = namedArgCount; | |
179 | ||
180 | excep.pfnDeferredFillIn = NULL; | |
181 | ||
182 | hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT, | |
183 | (WORD)action, &dispparams, vReturnPtr, &excep, &uiArgErr); | |
184 | ||
185 | for (i = 0; i < namedArgStringCount; i++) | |
186 | { | |
187 | SysFreeString(argNames[i]); | |
188 | } | |
189 | delete[] argNames; | |
190 | delete[] dispIds; | |
191 | ||
192 | for (i = 0; i < noArgs; i++) | |
193 | ReleaseVariant(& oleArgs[i]) ; | |
194 | delete[] oleArgs; | |
195 | ||
196 | if (FAILED(hr)) | |
197 | { | |
198 | // display the exception information if appropriate: | |
199 | // ShowException((const char*) member, hr, &excep, uiArgErr); | |
200 | ||
201 | // free exception structure information | |
202 | SysFreeString(excep.bstrSource); | |
203 | SysFreeString(excep.bstrDescription); | |
204 | SysFreeString(excep.bstrHelpFile); | |
205 | ||
206 | if (vReturnPtr) | |
207 | ReleaseVariant(vReturnPtr); | |
208 | return false; | |
209 | } | |
210 | else | |
211 | { | |
212 | if (vReturnPtr) | |
213 | { | |
214 | // Convert result to wxVariant form | |
215 | wxConvertOleToVariant(vReturn, retValue); | |
216 | // Mustn't release the dispatch pointer | |
217 | if (vReturn.vt == VT_DISPATCH) | |
218 | { | |
219 | vReturn.pdispVal = (IDispatch*) NULL; | |
220 | } | |
221 | ReleaseVariant(& vReturn); | |
222 | } | |
223 | } | |
224 | return true; | |
225 | } | |
226 | ||
227 | // Invoke a member function | |
228 | wxVariant wxAutomationObject::CallMethod(const wxString& member, int noArgs, wxVariant args[]) | |
229 | { | |
230 | wxVariant retVariant; | |
231 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, args)) | |
232 | { | |
233 | retVariant.MakeNull(); | |
234 | } | |
235 | return retVariant; | |
236 | } | |
237 | ||
238 | wxVariant wxAutomationObject::CallMethodArray(const wxString& member, int noArgs, const wxVariant **args) | |
239 | { | |
240 | wxVariant retVariant; | |
241 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, NULL, args)) | |
242 | { | |
243 | retVariant.MakeNull(); | |
244 | } | |
245 | return retVariant; | |
246 | } | |
247 | ||
248 | wxVariant wxAutomationObject::CallMethod(const wxString& member, | |
249 | const wxVariant& arg1, const wxVariant& arg2, | |
250 | const wxVariant& arg3, const wxVariant& arg4, | |
251 | const wxVariant& arg5, const wxVariant& arg6) | |
252 | { | |
253 | const wxVariant** args = new const wxVariant*[6]; | |
254 | int i = 0; | |
255 | if (!arg1.IsNull()) | |
256 | { | |
257 | args[i] = & arg1; | |
258 | i ++; | |
259 | } | |
260 | if (!arg2.IsNull()) | |
261 | { | |
262 | args[i] = & arg2; | |
263 | i ++; | |
264 | } | |
265 | if (!arg3.IsNull()) | |
266 | { | |
267 | args[i] = & arg3; | |
268 | i ++; | |
269 | } | |
270 | if (!arg4.IsNull()) | |
271 | { | |
272 | args[i] = & arg4; | |
273 | i ++; | |
274 | } | |
275 | if (!arg5.IsNull()) | |
276 | { | |
277 | args[i] = & arg5; | |
278 | i ++; | |
279 | } | |
280 | if (!arg6.IsNull()) | |
281 | { | |
282 | args[i] = & arg6; | |
283 | i ++; | |
284 | } | |
285 | wxVariant retVariant; | |
286 | if (!Invoke(member, DISPATCH_METHOD, retVariant, i, NULL, args)) | |
287 | { | |
288 | retVariant.MakeNull(); | |
289 | } | |
290 | delete[] args; | |
291 | return retVariant; | |
292 | } | |
293 | ||
294 | // Get/Set property | |
295 | wxVariant wxAutomationObject::GetPropertyArray(const wxString& property, int noArgs, const wxVariant **args) const | |
296 | { | |
297 | wxVariant retVariant; | |
298 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args)) | |
299 | { | |
300 | retVariant.MakeNull(); | |
301 | } | |
302 | return retVariant; | |
303 | } | |
304 | wxVariant wxAutomationObject::GetProperty(const wxString& property, int noArgs, wxVariant args[]) const | |
305 | { | |
306 | wxVariant retVariant; | |
307 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
308 | { | |
309 | retVariant.MakeNull(); | |
310 | } | |
311 | return retVariant; | |
312 | } | |
313 | ||
314 | wxVariant wxAutomationObject::GetProperty(const wxString& property, | |
315 | const wxVariant& arg1, const wxVariant& arg2, | |
316 | const wxVariant& arg3, const wxVariant& arg4, | |
317 | const wxVariant& arg5, const wxVariant& arg6) | |
318 | { | |
319 | const wxVariant** args = new const wxVariant*[6]; | |
320 | int i = 0; | |
321 | if (!arg1.IsNull()) | |
322 | { | |
323 | args[i] = & arg1; | |
324 | i ++; | |
325 | } | |
326 | if (!arg2.IsNull()) | |
327 | { | |
328 | args[i] = & arg2; | |
329 | i ++; | |
330 | } | |
331 | if (!arg3.IsNull()) | |
332 | { | |
333 | args[i] = & arg3; | |
334 | i ++; | |
335 | } | |
336 | if (!arg4.IsNull()) | |
337 | { | |
338 | args[i] = & arg4; | |
339 | i ++; | |
340 | } | |
341 | if (!arg5.IsNull()) | |
342 | { | |
343 | args[i] = & arg5; | |
344 | i ++; | |
345 | } | |
346 | if (!arg6.IsNull()) | |
347 | { | |
348 | args[i] = & arg6; | |
349 | i ++; | |
350 | } | |
351 | wxVariant retVariant; | |
352 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, i, NULL, args)) | |
353 | { | |
354 | retVariant.MakeNull(); | |
355 | } | |
356 | delete[] args; | |
357 | return retVariant; | |
358 | } | |
359 | ||
360 | bool wxAutomationObject::PutProperty(const wxString& property, int noArgs, wxVariant args[]) | |
361 | { | |
362 | wxVariant retVariant; | |
363 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, args)) | |
364 | { | |
365 | return false; | |
366 | } | |
367 | return true; | |
368 | } | |
369 | ||
370 | bool wxAutomationObject::PutPropertyArray(const wxString& property, int noArgs, const wxVariant **args) | |
371 | { | |
372 | wxVariant retVariant; | |
373 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, NULL, args)) | |
374 | { | |
375 | return false; | |
376 | } | |
377 | return true; | |
378 | } | |
379 | ||
380 | bool wxAutomationObject::PutProperty(const wxString& property, | |
381 | const wxVariant& arg1, const wxVariant& arg2, | |
382 | const wxVariant& arg3, const wxVariant& arg4, | |
383 | const wxVariant& arg5, const wxVariant& arg6) | |
384 | { | |
385 | const wxVariant** args = new const wxVariant*[6]; | |
386 | int i = 0; | |
387 | if (!arg1.IsNull()) | |
388 | { | |
389 | args[i] = & arg1; | |
390 | i ++; | |
391 | } | |
392 | if (!arg2.IsNull()) | |
393 | { | |
394 | args[i] = & arg2; | |
395 | i ++; | |
396 | } | |
397 | if (!arg3.IsNull()) | |
398 | { | |
399 | args[i] = & arg3; | |
400 | i ++; | |
401 | } | |
402 | if (!arg4.IsNull()) | |
403 | { | |
404 | args[i] = & arg4; | |
405 | i ++; | |
406 | } | |
407 | if (!arg5.IsNull()) | |
408 | { | |
409 | args[i] = & arg5; | |
410 | i ++; | |
411 | } | |
412 | if (!arg6.IsNull()) | |
413 | { | |
414 | args[i] = & arg6; | |
415 | i ++; | |
416 | } | |
417 | wxVariant retVariant; | |
418 | bool ret = Invoke(property, DISPATCH_PROPERTYPUT, retVariant, i, NULL, args); | |
419 | delete[] args; | |
420 | return ret; | |
421 | } | |
422 | ||
423 | ||
424 | // Uses DISPATCH_PROPERTYGET | |
425 | // and returns a dispatch pointer. The calling code should call Release | |
426 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
427 | // with it and letting the destructor call Release. | |
428 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, wxVariant args[]) const | |
429 | { | |
430 | wxVariant retVariant; | |
431 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
432 | { | |
433 | if (retVariant.GetType() == wxT("void*")) | |
434 | { | |
435 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
436 | } | |
437 | } | |
438 | ||
439 | return (WXIDISPATCH*) NULL; | |
440 | } | |
441 | ||
442 | // Uses DISPATCH_PROPERTYGET | |
443 | // and returns a dispatch pointer. The calling code should call Release | |
444 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
445 | // with it and letting the destructor call Release. | |
446 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, const wxVariant **args) const | |
447 | { | |
448 | wxVariant retVariant; | |
449 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args)) | |
450 | { | |
451 | if (retVariant.GetType() == wxT("void*")) | |
452 | { | |
453 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
454 | } | |
455 | } | |
456 | ||
457 | return (WXIDISPATCH*) NULL; | |
458 | } | |
459 | ||
460 | ||
461 | // A way of initialising another wxAutomationObject with a dispatch object | |
462 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, wxVariant args[]) const | |
463 | { | |
464 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); | |
465 | if (dispatch) | |
466 | { | |
467 | obj.SetDispatchPtr(dispatch); | |
468 | return true; | |
469 | } | |
470 | else | |
471 | return false; | |
472 | } | |
473 | ||
474 | // A way of initialising another wxAutomationObject with a dispatch object | |
475 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, const wxVariant **args) const | |
476 | { | |
477 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); | |
478 | if (dispatch) | |
479 | { | |
480 | obj.SetDispatchPtr(dispatch); | |
481 | return true; | |
482 | } | |
483 | else | |
484 | return false; | |
485 | } | |
486 | ||
487 | // Get a dispatch pointer from the current object associated | |
488 | // with a class id | |
489 | bool wxAutomationObject::GetInstance(const wxString& classId) const | |
490 | { | |
491 | if (m_dispatchPtr) | |
492 | return false; | |
493 | ||
494 | CLSID clsId; | |
495 | IUnknown * pUnk = NULL; | |
496 | ||
497 | wxBasicString unicodeName(classId.mb_str()); | |
498 | ||
499 | if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId))) | |
500 | { | |
501 | wxLogWarning(wxT("Cannot obtain CLSID from ProgID")); | |
502 | return false; | |
503 | } | |
504 | ||
505 | if (FAILED(GetActiveObject(clsId, NULL, &pUnk))) | |
506 | { | |
507 | wxLogWarning(wxT("Cannot find an active object")); | |
508 | return false; | |
509 | } | |
510 | ||
511 | if (pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr) != S_OK) | |
512 | { | |
513 | wxLogWarning(wxT("Cannot find IDispatch interface")); | |
514 | return false; | |
515 | } | |
516 | ||
517 | return true; | |
518 | } | |
519 | ||
520 | // Get a dispatch pointer from a new object associated | |
521 | // with the given class id | |
522 | bool wxAutomationObject::CreateInstance(const wxString& classId) const | |
523 | { | |
524 | if (m_dispatchPtr) | |
525 | return false; | |
526 | ||
527 | CLSID clsId; | |
528 | ||
529 | wxBasicString unicodeName(classId.mb_str()); | |
530 | ||
531 | if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId))) | |
532 | { | |
533 | wxLogWarning(wxT("Cannot obtain CLSID from ProgID")); | |
534 | return false; | |
535 | } | |
536 | ||
537 | // start a new copy of Excel, grab the IDispatch interface | |
538 | if (FAILED(CoCreateInstance(clsId, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&m_dispatchPtr))) | |
539 | { | |
540 | wxLogWarning(wxT("Cannot start an instance of this class.")); | |
541 | return false; | |
542 | } | |
543 | ||
544 | return true; | |
545 | } | |
546 | ||
547 | ||
548 | WXDLLEXPORT bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant) | |
549 | { | |
550 | ClearVariant(&oleVariant); | |
551 | if (variant.IsNull()) | |
552 | { | |
553 | oleVariant.vt = VT_NULL; | |
554 | return true; | |
555 | } | |
556 | ||
557 | wxString type(variant.GetType()); | |
558 | ||
559 | ||
560 | if (type == wxT("long")) | |
561 | { | |
562 | oleVariant.vt = VT_I4; | |
563 | oleVariant.lVal = variant.GetLong() ; | |
564 | } | |
565 | // cVal not always present | |
566 | #ifndef __GNUWIN32__ | |
567 | else if (type == wxT("char")) | |
568 | { | |
569 | oleVariant.vt=VT_I1; // Signed Char | |
570 | oleVariant.cVal=variant.GetChar(); | |
571 | } | |
572 | #endif | |
573 | else if (type == wxT("double")) | |
574 | { | |
575 | oleVariant.vt = VT_R8; | |
576 | oleVariant.dblVal = variant.GetDouble(); | |
577 | } | |
578 | else if (type == wxT("bool")) | |
579 | { | |
580 | oleVariant.vt = VT_BOOL; | |
581 | // 'bool' required for VC++ 4 apparently | |
582 | #if (defined(__VISUALC__) && (__VISUALC__ <= 1000)) | |
583 | oleVariant.bool = variant.GetBool(); | |
584 | #else | |
585 | oleVariant.boolVal = variant.GetBool(); | |
586 | #endif | |
587 | } | |
588 | else if (type == wxT("string")) | |
589 | { | |
590 | wxString str( variant.GetString() ); | |
591 | oleVariant.vt = VT_BSTR; | |
592 | oleVariant.bstrVal = wxConvertStringToOle(str); | |
593 | } | |
594 | #if wxUSE_DATETIME | |
595 | else if (type == wxT("datetime")) | |
596 | { | |
597 | wxDateTime date( variant.GetDateTime() ); | |
598 | oleVariant.vt = VT_DATE; | |
599 | ||
600 | long dosDateTime = date.GetAsDOS(); | |
601 | short dosDate = short((dosDateTime & 0xFFFF0000) >> 16); | |
602 | short dosTime = short(dosDateTime & 0xFFFF); | |
603 | ||
604 | DosDateTimeToVariantTime(dosDate, dosTime, & oleVariant.date); | |
605 | } | |
606 | #endif | |
607 | else if (type == wxT("void*")) | |
608 | { | |
609 | oleVariant.vt = VT_DISPATCH; | |
610 | oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr(); | |
611 | } | |
612 | else if (type == wxT("list") || type == wxT("stringlist")) | |
613 | { | |
614 | oleVariant.vt = VT_VARIANT | VT_ARRAY; | |
615 | ||
616 | SAFEARRAY *psa; | |
617 | SAFEARRAYBOUND saBound; | |
618 | VARIANTARG *pvargBase; | |
619 | VARIANTARG *pvarg; | |
620 | int i, j; | |
621 | ||
622 | int iCount = variant.GetCount(); | |
623 | ||
624 | saBound.lLbound = 0; | |
625 | saBound.cElements = iCount; | |
626 | ||
627 | psa = SafeArrayCreate(VT_VARIANT, 1, &saBound); | |
628 | if (psa == NULL) | |
629 | return false; | |
630 | ||
631 | SafeArrayAccessData(psa, (void**)&pvargBase); | |
632 | ||
633 | pvarg = pvargBase; | |
634 | for (i = 0; i < iCount; i++) | |
635 | { | |
636 | // copy each string in the list of strings | |
637 | wxVariant eachVariant(variant[i]); | |
638 | if (!wxConvertVariantToOle(eachVariant, * pvarg)) | |
639 | { | |
640 | // memory failure: back out and free strings alloc'ed up to | |
641 | // now, and then the array itself. | |
642 | pvarg = pvargBase; | |
643 | for (j = 0; j < i; j++) | |
644 | { | |
645 | SysFreeString(pvarg->bstrVal); | |
646 | pvarg++; | |
647 | } | |
648 | SafeArrayDestroy(psa); | |
649 | return false; | |
650 | } | |
651 | pvarg++; | |
652 | } | |
653 | ||
654 | SafeArrayUnaccessData(psa); | |
655 | ||
656 | oleVariant.parray = psa; | |
657 | } | |
658 | else | |
659 | { | |
660 | oleVariant.vt = VT_NULL; | |
661 | return false; | |
662 | } | |
663 | return true; | |
664 | } | |
665 | ||
666 | #ifndef VT_TYPEMASK | |
667 | #define VT_TYPEMASK 0xfff | |
668 | #endif | |
669 | ||
670 | WXDLLEXPORT bool wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant) | |
671 | { | |
672 | switch (oleVariant.vt & VT_TYPEMASK) | |
673 | { | |
674 | case VT_BSTR: | |
675 | { | |
676 | wxString str(wxConvertStringFromOle(oleVariant.bstrVal)); | |
677 | variant = str; | |
678 | break; | |
679 | } | |
680 | case VT_DATE: | |
681 | { | |
682 | #if wxUSE_DATETIME | |
683 | unsigned short dosDate = 0; | |
684 | unsigned short dosTime = 0; | |
685 | VariantTimeToDosDateTime(oleVariant.date, & dosDate, & dosTime); | |
686 | ||
687 | long dosDateTime = (dosDate << 16) || dosTime; | |
688 | wxDateTime date; | |
689 | date.SetFromDOS(dosDateTime); | |
690 | variant = date; | |
691 | #endif | |
692 | break; | |
693 | } | |
694 | case VT_I4: | |
695 | { | |
696 | variant = (long) oleVariant.lVal; | |
697 | break; | |
698 | } | |
699 | case VT_I2: | |
700 | { | |
701 | variant = (long) oleVariant.iVal; | |
702 | break; | |
703 | } | |
704 | ||
705 | case VT_BOOL: | |
706 | { | |
707 | #if (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC | |
708 | #ifndef HAVE_BOOL // Can't use bool operator if no native bool type | |
709 | variant = (long) (oleVariant.bool != 0); | |
710 | #else | |
711 | variant = (bool) (oleVariant.bool != 0); | |
712 | #endif | |
713 | #else | |
714 | #ifndef HAVE_BOOL // Can't use bool operator if no native bool type | |
715 | variant = (long) (oleVariant.boolVal != 0); | |
716 | #else | |
717 | variant = (bool) (oleVariant.boolVal != 0); | |
718 | #endif | |
719 | #endif | |
720 | break; | |
721 | } | |
722 | case VT_R8: | |
723 | { | |
724 | variant = oleVariant.dblVal; | |
725 | break; | |
726 | } | |
727 | case VT_ARRAY: | |
728 | { | |
729 | variant.ClearList(); | |
730 | ||
731 | int cDims, cElements, i; | |
732 | VARIANTARG* pvdata; | |
733 | ||
734 | // Iterate the dimensions: number of elements is x*y*z | |
735 | for (cDims = 0, cElements = 1; | |
736 | cDims < oleVariant.parray->cDims; cDims ++) | |
737 | cElements *= oleVariant.parray->rgsabound[cDims].cElements; | |
738 | ||
739 | // Get a pointer to the data | |
740 | HRESULT hr = SafeArrayAccessData(oleVariant.parray, (void HUGEP* FAR*) & pvdata); | |
741 | if (hr != NOERROR) | |
742 | return false; | |
743 | // Iterate the data. | |
744 | for (i = 0; i < cElements; i++) | |
745 | { | |
746 | VARIANTARG& oleElement = pvdata[i]; | |
747 | wxVariant vElement; | |
748 | if (!wxConvertOleToVariant(oleElement, vElement)) | |
749 | return false; | |
750 | ||
751 | variant.Append(vElement); | |
752 | } | |
753 | SafeArrayUnaccessData(oleVariant.parray); | |
754 | break; | |
755 | } | |
756 | case VT_DISPATCH: | |
757 | { | |
758 | variant = (void*) oleVariant.pdispVal; | |
759 | break; | |
760 | } | |
761 | case VT_NULL: | |
762 | { | |
763 | variant.MakeNull(); | |
764 | break; | |
765 | } | |
766 | case VT_EMPTY: | |
767 | { | |
768 | break; // Ignore Empty Variant, used only during destruction of objects | |
769 | } | |
770 | default: | |
771 | { | |
772 | wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type")); | |
773 | return false; | |
774 | } | |
775 | } | |
776 | return true; | |
777 | } | |
778 | ||
779 | /* | |
780 | * ClearVariant | |
781 | * | |
782 | * Zeros a variant structure without regard to current contents | |
783 | */ | |
784 | static void ClearVariant(VARIANTARG *pvarg) | |
785 | { | |
786 | pvarg->vt = VT_EMPTY; | |
787 | pvarg->wReserved1 = 0; | |
788 | pvarg->wReserved2 = 0; | |
789 | pvarg->wReserved3 = 0; | |
790 | pvarg->lVal = 0; | |
791 | } | |
792 | ||
793 | /* | |
794 | * ReleaseVariant | |
795 | * | |
796 | * Clears a particular variant structure and releases any external objects | |
797 | * or memory contained in the variant. Supports the data types listed above. | |
798 | */ | |
799 | static void ReleaseVariant(VARIANTARG *pvarg) | |
800 | { | |
801 | VARTYPE vt; | |
802 | VARIANTARG _huge *pvargArray; | |
803 | long lLBound, lUBound, l; | |
804 | ||
805 | vt = (VARTYPE)(pvarg->vt & 0xfff); // mask off flags | |
806 | ||
807 | // check if an array. If so, free its contents, then the array itself. | |
808 | if (V_ISARRAY(pvarg)) | |
809 | { | |
810 | // variant arrays are all this routine currently knows about. Since a | |
811 | // variant can contain anything (even other arrays), call ourselves | |
812 | // recursively. | |
813 | if (vt == VT_VARIANT) | |
814 | { | |
815 | SafeArrayGetLBound(pvarg->parray, 1, &lLBound); | |
816 | SafeArrayGetUBound(pvarg->parray, 1, &lUBound); | |
817 | ||
818 | if (lUBound > lLBound) | |
819 | { | |
820 | lUBound -= lLBound; | |
821 | ||
822 | SafeArrayAccessData(pvarg->parray, (void**)&pvargArray); | |
823 | ||
824 | for (l = 0; l < lUBound; l++) | |
825 | { | |
826 | ReleaseVariant(pvargArray); | |
827 | pvargArray++; | |
828 | } | |
829 | ||
830 | SafeArrayUnaccessData(pvarg->parray); | |
831 | } | |
832 | } | |
833 | else | |
834 | { | |
835 | wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type")); | |
836 | } | |
837 | ||
838 | // Free the array itself. | |
839 | SafeArrayDestroy(pvarg->parray); | |
840 | } | |
841 | else | |
842 | { | |
843 | switch (vt) | |
844 | { | |
845 | case VT_DISPATCH: | |
846 | if (pvarg->pdispVal) | |
847 | pvarg->pdispVal->Release(); | |
848 | break; | |
849 | ||
850 | case VT_BSTR: | |
851 | SysFreeString(pvarg->bstrVal); | |
852 | break; | |
853 | ||
854 | case VT_I2: | |
855 | case VT_I4: | |
856 | case VT_BOOL: | |
857 | case VT_R8: | |
858 | case VT_ERROR: // to avoid erroring on an error return from Excel | |
859 | case VT_EMPTY: | |
860 | // no work for these types | |
861 | break; | |
862 | ||
863 | default: | |
864 | wxLogWarning(wxT("ReleaseVariant: Unknown type")); | |
865 | break; | |
866 | } | |
867 | } | |
868 | ||
869 | ClearVariant(pvarg); | |
870 | } | |
871 | ||
872 | #if 0 | |
873 | ||
874 | void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr) | |
875 | { | |
876 | TCHAR szBuf[512]; | |
877 | ||
878 | switch (GetScode(hr)) | |
879 | { | |
880 | case DISP_E_UNKNOWNNAME: | |
881 | wsprintf(szBuf, L"%s: Unknown name or named argument.", szMember); | |
882 | break; | |
883 | ||
884 | case DISP_E_BADPARAMCOUNT: | |
885 | wsprintf(szBuf, L"%s: Incorrect number of arguments.", szMember); | |
886 | break; | |
887 | ||
888 | case DISP_E_EXCEPTION: | |
889 | wsprintf(szBuf, L"%s: Error %d: ", szMember, pexcep->wCode); | |
890 | if (pexcep->bstrDescription != NULL) | |
891 | lstrcat(szBuf, pexcep->bstrDescription); | |
892 | else | |
893 | lstrcat(szBuf, L"<<No Description>>"); | |
894 | break; | |
895 | ||
896 | case DISP_E_MEMBERNOTFOUND: | |
897 | wsprintf(szBuf, L"%s: method or property not found.", szMember); | |
898 | break; | |
899 | ||
900 | case DISP_E_OVERFLOW: | |
901 | wsprintf(szBuf, L"%s: Overflow while coercing argument values.", szMember); | |
902 | break; | |
903 | ||
904 | case DISP_E_NONAMEDARGS: | |
905 | wsprintf(szBuf, L"%s: Object implementation does not support named arguments.", | |
906 | szMember); | |
907 | break; | |
908 | ||
909 | case DISP_E_UNKNOWNLCID: | |
910 | wsprintf(szBuf, L"%s: The locale ID is unknown.", szMember); | |
911 | break; | |
912 | ||
913 | case DISP_E_PARAMNOTOPTIONAL: | |
914 | wsprintf(szBuf, L"%s: Missing a required parameter.", szMember); | |
915 | break; | |
916 | ||
917 | case DISP_E_PARAMNOTFOUND: | |
918 | wsprintf(szBuf, L"%s: Argument not found, argument %d.", szMember, uiArgErr); | |
919 | break; | |
920 | ||
921 | case DISP_E_TYPEMISMATCH: | |
922 | wsprintf(szBuf, L"%s: Type mismatch, argument %d.", szMember, uiArgErr); | |
923 | break; | |
924 | ||
925 | default: | |
926 | wsprintf(szBuf, L"%s: Unknown error occurred.", szMember); | |
927 | break; | |
928 | } | |
929 | ||
930 | wxLogWarning(szBuf); | |
931 | } | |
932 | ||
933 | #endif | |
934 | ||
935 | #endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__) |