]>
Commit | Line | Data |
---|---|---|
d980b3e1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
11f104e5 | 2 | // Name: src/msw/ole/automtn.cpp |
d980b3e1 JS |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
d980b3e1 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
d980b3e1 JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if defined(__BORLANDC__) | |
7520f3da | 16 | #pragma hdrstop |
d980b3e1 JS |
17 | #endif |
18 | ||
f6bcfd97 | 19 | // With Borland C++, all samples crash if this is compiled in. |
ca5c6ac3 VZ |
20 | #if (defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) || defined(__CYGWIN10__) |
21 | #undef wxUSE_OLE_AUTOMATION | |
22 | #define wxUSE_OLE_AUTOMATION 0 | |
23 | #endif | |
24 | ||
e4db172a WS |
25 | #ifndef WX_PRECOMP |
26 | #include "wx/log.h" | |
18680f86 | 27 | #include "wx/math.h" |
e4db172a WS |
28 | #endif |
29 | ||
5283098e | 30 | #define _FORCENAMELESSUNION |
4676948b | 31 | #include "wx/msw/private.h" |
ed5317e5 | 32 | #include "wx/msw/ole/oleutils.h" |
42e69d6b | 33 | #include "wx/msw/ole/automtn.h" |
4676948b JS |
34 | |
35 | #ifdef __WXWINCE__ | |
36 | #include "wx/msw/wince/time.h" | |
37 | #else | |
f6bcfd97 | 38 | #include <time.h> |
4676948b | 39 | #endif |
f6bcfd97 | 40 | |
7dee726c RS |
41 | #include <wtypes.h> |
42 | #include <unknwn.h> | |
4676948b | 43 | |
7dee726c RS |
44 | #include <ole2.h> |
45 | #define _huge | |
4676948b JS |
46 | |
47 | #ifndef __WXWINCE__ | |
42e69d6b | 48 | #include <ole2ver.h> |
4676948b JS |
49 | #endif |
50 | ||
42e69d6b | 51 | #include <oleauto.h> |
17b74d79 | 52 | |
61bfe4b0 JS |
53 | #if wxUSE_DATETIME |
54 | #include "wx/datetime.h" | |
b0f76951 | 55 | #endif // wxUSE_DATETIME |
d980b3e1 | 56 | |
a2fd865b VS |
57 | #if wxUSE_OLE_AUTOMATION |
58 | ||
cf1a582a VZ |
59 | #include <wx/vector.h> |
60 | ||
6eefca4f | 61 | // Report an OLE error when calling the specified method to the user via wxLog. |
84f4eef8 VZ |
62 | static void |
63 | ShowException(const wxString& member, | |
64 | HRESULT hr, | |
6eefca4f VZ |
65 | EXCEPINFO *pexcep = NULL, |
66 | unsigned int uiArgErr = 0); | |
84f4eef8 VZ |
67 | |
68 | // wxAutomationObject | |
d980b3e1 JS |
69 | |
70 | wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr) | |
71 | { | |
0a0e6a5b | 72 | m_dispatchPtr = dispatchPtr; |
d980b3e1 JS |
73 | } |
74 | ||
75 | wxAutomationObject::~wxAutomationObject() | |
76 | { | |
0a0e6a5b WS |
77 | if (m_dispatchPtr) |
78 | { | |
79 | ((IDispatch*)m_dispatchPtr)->Release(); | |
80 | m_dispatchPtr = NULL; | |
81 | } | |
d980b3e1 JS |
82 | } |
83 | ||
cf1a582a VZ |
84 | namespace |
85 | { | |
86 | ||
87 | // A simple helper that ensures that VARIANT is destroyed on scope exit. | |
88 | struct wxOleVariantArg : VARIANTARG | |
89 | { | |
90 | wxOleVariantArg() { VariantInit(this); } | |
91 | ~wxOleVariantArg() { VariantClear(this); } | |
92 | }; | |
93 | ||
94 | } // anonymous namespace | |
95 | ||
96 | ||
d980b3e1 JS |
97 | #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i])) |
98 | ||
99 | // For Put/Get, no named arguments are allowed. | |
cf1a582a VZ |
100 | // WARNING: if args contain IDispatches, their reference count will be decreased |
101 | // by one after Invoke() returns! | |
d980b3e1 JS |
102 | bool wxAutomationObject::Invoke(const wxString& member, int action, |
103 | wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const | |
104 | { | |
0a0e6a5b WS |
105 | if (!m_dispatchPtr) |
106 | return false; | |
107 | ||
d3ffaafb | 108 | int ch = member.Find('.'); |
0a0e6a5b WS |
109 | if (ch != -1) |
110 | { | |
111 | // Use dot notation to get the next object | |
d3ffaafb VZ |
112 | wxString member2(member.Left((size_t) ch)); |
113 | wxString rest(member.Right(member.length() - ch - 1)); | |
0a0e6a5b WS |
114 | wxAutomationObject obj; |
115 | if (!GetObject(obj, member2)) | |
116 | return false; | |
117 | return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs); | |
118 | } | |
119 | ||
cf1a582a VZ |
120 | wxOleVariantArg vReturn; |
121 | wxOleVariantArg* vReturnPtr = & vReturn; | |
0a0e6a5b WS |
122 | |
123 | // Find number of names args | |
124 | int namedArgCount = 0; | |
125 | int i; | |
126 | for (i = 0; i < noArgs; i++) | |
cf1a582a | 127 | { |
6636ef8d | 128 | if ( !INVOKEARG(i).GetName().empty() ) |
12335fa6 | 129 | { |
0a0e6a5b WS |
130 | namedArgCount ++; |
131 | } | |
cf1a582a | 132 | } |
0a0e6a5b WS |
133 | |
134 | int namedArgStringCount = namedArgCount + 1; | |
cf1a582a VZ |
135 | wxVector<wxBasicString> argNames(namedArgStringCount, wxString()); |
136 | argNames[0] = member; | |
0a0e6a5b WS |
137 | |
138 | // Note that arguments are specified in reverse order | |
139 | // (all totally logical; hey, we're dealing with OLE here.) | |
140 | ||
141 | int j = 0; | |
142 | for (i = 0; i < namedArgCount; i++) | |
143 | { | |
6636ef8d | 144 | if ( !INVOKEARG(i).GetName().empty() ) |
0a0e6a5b | 145 | { |
cf1a582a | 146 | argNames[(namedArgCount-j)] = INVOKEARG(i).GetName(); |
0a0e6a5b WS |
147 | j ++; |
148 | } | |
149 | } | |
150 | ||
151 | // + 1 for the member name, + 1 again in case we're a 'put' | |
cf1a582a | 152 | wxVector<DISPID> dispIds(namedArgCount + 2); |
0a0e6a5b WS |
153 | |
154 | HRESULT hr; | |
155 | DISPPARAMS dispparams; | |
156 | unsigned int uiArgErr; | |
0a0e6a5b WS |
157 | |
158 | // Get the IDs for the member and its arguments. GetIDsOfNames expects the | |
159 | // member name as the first name, followed by argument names (if any). | |
cf1a582a VZ |
160 | hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, |
161 | // We rely on the fact that wxBasicString is | |
162 | // just BSTR with some methods here. | |
163 | reinterpret_cast<BSTR *>(&argNames[0]), | |
164 | 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, &dispIds[0]); | |
0a0e6a5b WS |
165 | if (FAILED(hr)) |
166 | { | |
6eefca4f | 167 | ShowException(member, hr); |
0a0e6a5b WS |
168 | return false; |
169 | } | |
170 | ||
171 | // if doing a property put(ref), we need to adjust the first argument to have a | |
172 | // named arg of DISPID_PROPERTYPUT. | |
173 | if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF)) | |
174 | { | |
175 | namedArgCount = 1; | |
176 | dispIds[1] = DISPID_PROPERTYPUT; | |
d3b9f782 | 177 | vReturnPtr = NULL; |
0a0e6a5b WS |
178 | } |
179 | ||
180 | // Convert the wxVariants to VARIANTARGs | |
cf1a582a | 181 | wxVector<wxOleVariantArg> oleArgs(noArgs); |
0a0e6a5b WS |
182 | for (i = 0; i < noArgs; i++) |
183 | { | |
184 | // Again, reverse args | |
185 | if (!wxConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i])) | |
0a0e6a5b | 186 | return false; |
0a0e6a5b WS |
187 | } |
188 | ||
cf1a582a VZ |
189 | dispparams.rgdispidNamedArgs = &dispIds[0] + 1; |
190 | dispparams.rgvarg = &oleArgs[0]; | |
0a0e6a5b WS |
191 | dispparams.cArgs = noArgs; |
192 | dispparams.cNamedArgs = namedArgCount; | |
193 | ||
c50ab33d VZ |
194 | EXCEPINFO excep; |
195 | wxZeroMemory(excep); | |
0a0e6a5b WS |
196 | |
197 | hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT, | |
5c519b6c | 198 | (WORD)action, &dispparams, vReturnPtr, &excep, &uiArgErr); |
0a0e6a5b | 199 | |
0a0e6a5b WS |
200 | if (FAILED(hr)) |
201 | { | |
202 | // display the exception information if appropriate: | |
84f4eef8 | 203 | ShowException(member, hr, &excep, uiArgErr); |
0a0e6a5b WS |
204 | |
205 | // free exception structure information | |
206 | SysFreeString(excep.bstrSource); | |
207 | SysFreeString(excep.bstrDescription); | |
208 | SysFreeString(excep.bstrHelpFile); | |
209 | ||
0a0e6a5b WS |
210 | return false; |
211 | } | |
212 | else | |
213 | { | |
214 | if (vReturnPtr) | |
215 | { | |
216 | // Convert result to wxVariant form | |
cf1a582a VZ |
217 | if (!wxConvertOleToVariant(vReturn, retValue)) |
218 | return false; | |
0a0e6a5b WS |
219 | // Mustn't release the dispatch pointer |
220 | if (vReturn.vt == VT_DISPATCH) | |
221 | { | |
d3b9f782 | 222 | vReturn.pdispVal = NULL; |
0a0e6a5b | 223 | } |
12335fa6 | 224 | } |
0a0e6a5b WS |
225 | } |
226 | return true; | |
d980b3e1 JS |
227 | } |
228 | ||
229 | // Invoke a member function | |
230 | wxVariant wxAutomationObject::CallMethod(const wxString& member, int noArgs, wxVariant args[]) | |
231 | { | |
0a0e6a5b WS |
232 | wxVariant retVariant; |
233 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, args)) | |
234 | { | |
235 | retVariant.MakeNull(); | |
236 | } | |
237 | return retVariant; | |
d980b3e1 JS |
238 | } |
239 | ||
24f4ad95 JS |
240 | wxVariant wxAutomationObject::CallMethodArray(const wxString& member, int noArgs, const wxVariant **args) |
241 | { | |
0a0e6a5b WS |
242 | wxVariant retVariant; |
243 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, NULL, args)) | |
244 | { | |
245 | retVariant.MakeNull(); | |
246 | } | |
247 | return retVariant; | |
24f4ad95 JS |
248 | } |
249 | ||
d980b3e1 | 250 | wxVariant wxAutomationObject::CallMethod(const wxString& member, |
0a0e6a5b WS |
251 | const wxVariant& arg1, const wxVariant& arg2, |
252 | const wxVariant& arg3, const wxVariant& arg4, | |
253 | const wxVariant& arg5, const wxVariant& arg6) | |
d980b3e1 | 254 | { |
0a0e6a5b WS |
255 | const wxVariant** args = new const wxVariant*[6]; |
256 | int i = 0; | |
257 | if (!arg1.IsNull()) | |
258 | { | |
259 | args[i] = & arg1; | |
260 | i ++; | |
261 | } | |
262 | if (!arg2.IsNull()) | |
263 | { | |
264 | args[i] = & arg2; | |
265 | i ++; | |
266 | } | |
267 | if (!arg3.IsNull()) | |
268 | { | |
269 | args[i] = & arg3; | |
270 | i ++; | |
271 | } | |
272 | if (!arg4.IsNull()) | |
273 | { | |
274 | args[i] = & arg4; | |
275 | i ++; | |
276 | } | |
277 | if (!arg5.IsNull()) | |
278 | { | |
279 | args[i] = & arg5; | |
280 | i ++; | |
281 | } | |
282 | if (!arg6.IsNull()) | |
283 | { | |
284 | args[i] = & arg6; | |
285 | i ++; | |
286 | } | |
287 | wxVariant retVariant; | |
288 | if (!Invoke(member, DISPATCH_METHOD, retVariant, i, NULL, args)) | |
289 | { | |
290 | retVariant.MakeNull(); | |
291 | } | |
292 | delete[] args; | |
293 | return retVariant; | |
d980b3e1 JS |
294 | } |
295 | ||
296 | // Get/Set property | |
24f4ad95 JS |
297 | wxVariant wxAutomationObject::GetPropertyArray(const wxString& property, int noArgs, const wxVariant **args) const |
298 | { | |
0a0e6a5b WS |
299 | wxVariant retVariant; |
300 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args)) | |
301 | { | |
302 | retVariant.MakeNull(); | |
303 | } | |
304 | return retVariant; | |
24f4ad95 | 305 | } |
d980b3e1 JS |
306 | wxVariant wxAutomationObject::GetProperty(const wxString& property, int noArgs, wxVariant args[]) const |
307 | { | |
0a0e6a5b WS |
308 | wxVariant retVariant; |
309 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
310 | { | |
311 | retVariant.MakeNull(); | |
312 | } | |
313 | return retVariant; | |
d980b3e1 JS |
314 | } |
315 | ||
316 | wxVariant wxAutomationObject::GetProperty(const wxString& property, | |
0a0e6a5b WS |
317 | const wxVariant& arg1, const wxVariant& arg2, |
318 | const wxVariant& arg3, const wxVariant& arg4, | |
319 | const wxVariant& arg5, const wxVariant& arg6) | |
d980b3e1 | 320 | { |
0a0e6a5b WS |
321 | const wxVariant** args = new const wxVariant*[6]; |
322 | int i = 0; | |
323 | if (!arg1.IsNull()) | |
324 | { | |
325 | args[i] = & arg1; | |
326 | i ++; | |
327 | } | |
328 | if (!arg2.IsNull()) | |
329 | { | |
330 | args[i] = & arg2; | |
331 | i ++; | |
332 | } | |
333 | if (!arg3.IsNull()) | |
334 | { | |
335 | args[i] = & arg3; | |
336 | i ++; | |
337 | } | |
338 | if (!arg4.IsNull()) | |
339 | { | |
340 | args[i] = & arg4; | |
341 | i ++; | |
342 | } | |
343 | if (!arg5.IsNull()) | |
344 | { | |
345 | args[i] = & arg5; | |
346 | i ++; | |
347 | } | |
348 | if (!arg6.IsNull()) | |
349 | { | |
350 | args[i] = & arg6; | |
351 | i ++; | |
352 | } | |
353 | wxVariant retVariant; | |
354 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, i, NULL, args)) | |
355 | { | |
356 | retVariant.MakeNull(); | |
357 | } | |
358 | delete[] args; | |
359 | return retVariant; | |
d980b3e1 JS |
360 | } |
361 | ||
362 | bool wxAutomationObject::PutProperty(const wxString& property, int noArgs, wxVariant args[]) | |
363 | { | |
0a0e6a5b WS |
364 | wxVariant retVariant; |
365 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, args)) | |
366 | { | |
367 | return false; | |
368 | } | |
369 | return true; | |
d980b3e1 JS |
370 | } |
371 | ||
24f4ad95 JS |
372 | bool wxAutomationObject::PutPropertyArray(const wxString& property, int noArgs, const wxVariant **args) |
373 | { | |
0a0e6a5b WS |
374 | wxVariant retVariant; |
375 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, NULL, args)) | |
376 | { | |
377 | return false; | |
378 | } | |
379 | return true; | |
24f4ad95 JS |
380 | } |
381 | ||
d980b3e1 | 382 | bool wxAutomationObject::PutProperty(const wxString& property, |
0a0e6a5b WS |
383 | const wxVariant& arg1, const wxVariant& arg2, |
384 | const wxVariant& arg3, const wxVariant& arg4, | |
385 | const wxVariant& arg5, const wxVariant& arg6) | |
d980b3e1 | 386 | { |
0a0e6a5b WS |
387 | const wxVariant** args = new const wxVariant*[6]; |
388 | int i = 0; | |
389 | if (!arg1.IsNull()) | |
390 | { | |
391 | args[i] = & arg1; | |
392 | i ++; | |
393 | } | |
394 | if (!arg2.IsNull()) | |
395 | { | |
396 | args[i] = & arg2; | |
397 | i ++; | |
398 | } | |
399 | if (!arg3.IsNull()) | |
400 | { | |
401 | args[i] = & arg3; | |
402 | i ++; | |
403 | } | |
404 | if (!arg4.IsNull()) | |
405 | { | |
406 | args[i] = & arg4; | |
407 | i ++; | |
408 | } | |
409 | if (!arg5.IsNull()) | |
410 | { | |
411 | args[i] = & arg5; | |
412 | i ++; | |
413 | } | |
414 | if (!arg6.IsNull()) | |
415 | { | |
416 | args[i] = & arg6; | |
417 | i ++; | |
418 | } | |
419 | wxVariant retVariant; | |
420 | bool ret = Invoke(property, DISPATCH_PROPERTYPUT, retVariant, i, NULL, args); | |
421 | delete[] args; | |
422 | return ret; | |
d980b3e1 JS |
423 | } |
424 | ||
425 | ||
426 | // Uses DISPATCH_PROPERTYGET | |
427 | // and returns a dispatch pointer. The calling code should call Release | |
428 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
429 | // with it and letting the destructor call Release. | |
430 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, wxVariant args[]) const | |
431 | { | |
0a0e6a5b WS |
432 | wxVariant retVariant; |
433 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
434 | { | |
435 | if (retVariant.GetType() == wxT("void*")) | |
436 | { | |
437 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
438 | } | |
439 | } | |
440 | ||
d3b9f782 | 441 | return NULL; |
d980b3e1 JS |
442 | } |
443 | ||
24f4ad95 JS |
444 | // Uses DISPATCH_PROPERTYGET |
445 | // and returns a dispatch pointer. The calling code should call Release | |
446 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
447 | // with it and letting the destructor call Release. | |
448 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, const wxVariant **args) const | |
449 | { | |
0a0e6a5b WS |
450 | wxVariant retVariant; |
451 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args)) | |
452 | { | |
453 | if (retVariant.GetType() == wxT("void*")) | |
454 | { | |
455 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
456 | } | |
457 | } | |
458 | ||
d3b9f782 | 459 | return NULL; |
24f4ad95 JS |
460 | } |
461 | ||
462 | ||
d980b3e1 JS |
463 | // A way of initialising another wxAutomationObject with a dispatch object |
464 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, wxVariant args[]) const | |
465 | { | |
0a0e6a5b WS |
466 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); |
467 | if (dispatch) | |
468 | { | |
469 | obj.SetDispatchPtr(dispatch); | |
470 | return true; | |
471 | } | |
472 | else | |
473 | return false; | |
d980b3e1 JS |
474 | } |
475 | ||
24f4ad95 JS |
476 | // A way of initialising another wxAutomationObject with a dispatch object |
477 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, const wxVariant **args) const | |
478 | { | |
0a0e6a5b WS |
479 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); |
480 | if (dispatch) | |
481 | { | |
482 | obj.SetDispatchPtr(dispatch); | |
483 | return true; | |
484 | } | |
485 | else | |
486 | return false; | |
24f4ad95 JS |
487 | } |
488 | ||
6eefca4f VZ |
489 | namespace |
490 | { | |
491 | ||
492 | HRESULT wxCLSIDFromProgID(const wxString& progId, CLSID& clsId) | |
493 | { | |
494 | HRESULT hr = CLSIDFromProgID(wxBasicString(progId), &clsId); | |
495 | if ( FAILED(hr) ) | |
496 | { | |
497 | wxLogSysError(hr, _("Failed to find CLSID of \"%s\""), progId); | |
498 | } | |
499 | return hr; | |
500 | } | |
501 | ||
502 | void *DoCreateInstance(const wxString& progId, const CLSID& clsId) | |
503 | { | |
504 | // get the server IDispatch interface | |
505 | // | |
506 | // NB: using CLSCTX_INPROC_HANDLER results in failure when getting | |
507 | // Automation interface for Microsoft Office applications so don't use | |
508 | // CLSCTX_ALL which includes it | |
509 | void *pDispatch = NULL; | |
510 | HRESULT hr = CoCreateInstance(clsId, NULL, CLSCTX_SERVER, | |
511 | IID_IDispatch, &pDispatch); | |
512 | if (FAILED(hr)) | |
513 | { | |
514 | wxLogSysError(hr, _("Failed to create an instance of \"%s\""), progId); | |
515 | return NULL; | |
516 | } | |
517 | ||
518 | return pDispatch; | |
519 | } | |
520 | ||
521 | } // anonymous namespace | |
522 | ||
d980b3e1 | 523 | // Get a dispatch pointer from the current object associated |
27d76879 | 524 | // with a ProgID |
6eefca4f | 525 | bool wxAutomationObject::GetInstance(const wxString& progId, int flags) const |
d980b3e1 | 526 | { |
0a0e6a5b WS |
527 | if (m_dispatchPtr) |
528 | return false; | |
529 | ||
530 | CLSID clsId; | |
6eefca4f | 531 | HRESULT hr = wxCLSIDFromProgID(progId, clsId); |
84f4eef8 | 532 | if (FAILED(hr)) |
0a0e6a5b | 533 | return false; |
0a0e6a5b | 534 | |
6eefca4f | 535 | IUnknown *pUnk = NULL; |
84f4eef8 VZ |
536 | hr = GetActiveObject(clsId, NULL, &pUnk); |
537 | if (FAILED(hr)) | |
0a0e6a5b | 538 | { |
6eefca4f VZ |
539 | if ( flags & wxAutomationInstance_CreateIfNeeded ) |
540 | { | |
541 | const_cast<wxAutomationObject *>(this)-> | |
542 | m_dispatchPtr = DoCreateInstance(progId, clsId); | |
543 | if ( m_dispatchPtr ) | |
544 | return true; | |
545 | } | |
546 | else | |
547 | { | |
1244d2e0 VZ |
548 | // Log an error except if we're supposed to fail silently when the |
549 | // error is that no current instance exists. | |
550 | if ( hr != MK_E_UNAVAILABLE || | |
551 | !(flags & wxAutomationInstance_SilentIfNone) ) | |
552 | { | |
553 | wxLogSysError(hr, | |
554 | _("Cannot get an active instance of \"%s\""), | |
555 | progId); | |
556 | } | |
6eefca4f VZ |
557 | } |
558 | ||
0a0e6a5b WS |
559 | return false; |
560 | } | |
561 | ||
84f4eef8 VZ |
562 | hr = pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr); |
563 | if (FAILED(hr)) | |
0a0e6a5b | 564 | { |
6eefca4f VZ |
565 | wxLogSysError(hr, |
566 | _("Failed to get OLE automation interface for \"%s\""), | |
567 | progId); | |
0a0e6a5b WS |
568 | return false; |
569 | } | |
570 | ||
571 | return true; | |
d980b3e1 JS |
572 | } |
573 | ||
574 | // Get a dispatch pointer from a new object associated | |
27d76879 VZ |
575 | // with the given ProgID |
576 | bool wxAutomationObject::CreateInstance(const wxString& progId) const | |
d980b3e1 | 577 | { |
0a0e6a5b WS |
578 | if (m_dispatchPtr) |
579 | return false; | |
580 | ||
581 | CLSID clsId; | |
6eefca4f | 582 | HRESULT hr = wxCLSIDFromProgID(progId, clsId); |
84f4eef8 | 583 | if (FAILED(hr)) |
0a0e6a5b | 584 | return false; |
0a0e6a5b | 585 | |
6eefca4f VZ |
586 | const_cast<wxAutomationObject *>(this)-> |
587 | m_dispatchPtr = DoCreateInstance(progId, clsId); | |
0a0e6a5b | 588 | |
6eefca4f | 589 | return m_dispatchPtr != NULL; |
d980b3e1 JS |
590 | } |
591 | ||
84f4eef8 VZ |
592 | static void |
593 | ShowException(const wxString& member, | |
594 | HRESULT hr, | |
595 | EXCEPINFO *pexcep, | |
596 | unsigned int uiArgErr) | |
d980b3e1 | 597 | { |
84f4eef8 | 598 | wxString message; |
0a0e6a5b WS |
599 | switch (GetScode(hr)) |
600 | { | |
601 | case DISP_E_UNKNOWNNAME: | |
6eefca4f | 602 | message = _("Unknown name or named argument."); |
0a0e6a5b WS |
603 | break; |
604 | ||
605 | case DISP_E_BADPARAMCOUNT: | |
6eefca4f | 606 | message = _("Incorrect number of arguments."); |
0a0e6a5b WS |
607 | break; |
608 | ||
609 | case DISP_E_EXCEPTION: | |
6eefca4f VZ |
610 | if ( pexcep ) |
611 | { | |
612 | if ( pexcep->bstrDescription ) | |
613 | message << pexcep->bstrDescription << wxS(" "); | |
614 | message += wxString::Format(wxS("error code %u"), pexcep->wCode); | |
615 | } | |
616 | else | |
84f4eef8 | 617 | { |
6eefca4f | 618 | message = _("Unknown exception"); |
84f4eef8 | 619 | } |
0a0e6a5b WS |
620 | break; |
621 | ||
622 | case DISP_E_MEMBERNOTFOUND: | |
6eefca4f | 623 | message = _("Method or property not found."); |
0a0e6a5b WS |
624 | break; |
625 | ||
626 | case DISP_E_OVERFLOW: | |
6eefca4f | 627 | message = _("Overflow while coercing argument values."); |
0a0e6a5b WS |
628 | break; |
629 | ||
630 | case DISP_E_NONAMEDARGS: | |
6eefca4f | 631 | message = _("Object implementation does not support named arguments."); |
0a0e6a5b WS |
632 | break; |
633 | ||
634 | case DISP_E_UNKNOWNLCID: | |
6eefca4f | 635 | message = _("The locale ID is unknown."); |
0a0e6a5b WS |
636 | break; |
637 | ||
638 | case DISP_E_PARAMNOTOPTIONAL: | |
6eefca4f | 639 | message = _("Missing a required parameter."); |
0a0e6a5b WS |
640 | break; |
641 | ||
642 | case DISP_E_PARAMNOTFOUND: | |
6eefca4f | 643 | message.Printf(_("Argument %u not found."), uiArgErr); |
0a0e6a5b WS |
644 | break; |
645 | ||
646 | case DISP_E_TYPEMISMATCH: | |
6eefca4f | 647 | message.Printf(_("Type mismatch in argument %u."), uiArgErr); |
84f4eef8 VZ |
648 | break; |
649 | ||
650 | case ERROR_FILE_NOT_FOUND: | |
6eefca4f | 651 | message = _("The system cannot find the file specified."); |
84f4eef8 VZ |
652 | break; |
653 | ||
654 | case REGDB_E_CLASSNOTREG: | |
6eefca4f | 655 | message = _("Class not registered."); |
0a0e6a5b WS |
656 | break; |
657 | ||
658 | default: | |
6eefca4f | 659 | message.Printf(_("Unknown error %08x"), hr); |
0a0e6a5b WS |
660 | break; |
661 | } | |
662 | ||
6eefca4f | 663 | wxLogError(_("OLE Automation error in %s: %s"), member, message); |
d980b3e1 JS |
664 | } |
665 | ||
84f4eef8 | 666 | #endif // wxUSE_OLE_AUTOMATION |