]>
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 | |
d980b3e1 | 7 | // Copyright: (c) 1998, Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
d980b3e1 JS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
d980b3e1 JS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if defined(__BORLANDC__) | |
7520f3da | 15 | #pragma hdrstop |
d980b3e1 JS |
16 | #endif |
17 | ||
f6bcfd97 | 18 | // With Borland C++, all samples crash if this is compiled in. |
ca5c6ac3 VZ |
19 | #if (defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) || defined(__CYGWIN10__) |
20 | #undef wxUSE_OLE_AUTOMATION | |
21 | #define wxUSE_OLE_AUTOMATION 0 | |
22 | #endif | |
23 | ||
e4db172a WS |
24 | #ifndef WX_PRECOMP |
25 | #include "wx/log.h" | |
18680f86 | 26 | #include "wx/math.h" |
e4db172a WS |
27 | #endif |
28 | ||
5283098e | 29 | #define _FORCENAMELESSUNION |
4676948b | 30 | #include "wx/msw/private.h" |
ed5317e5 | 31 | #include "wx/msw/ole/oleutils.h" |
42e69d6b | 32 | #include "wx/msw/ole/automtn.h" |
4676948b JS |
33 | |
34 | #ifdef __WXWINCE__ | |
35 | #include "wx/msw/wince/time.h" | |
36 | #else | |
f6bcfd97 | 37 | #include <time.h> |
4676948b | 38 | #endif |
f6bcfd97 | 39 | |
7dee726c RS |
40 | #include <wtypes.h> |
41 | #include <unknwn.h> | |
4676948b | 42 | |
7dee726c RS |
43 | #include <ole2.h> |
44 | #define _huge | |
4676948b JS |
45 | |
46 | #ifndef __WXWINCE__ | |
42e69d6b | 47 | #include <ole2ver.h> |
4676948b JS |
48 | #endif |
49 | ||
42e69d6b | 50 | #include <oleauto.h> |
17b74d79 | 51 | |
61bfe4b0 JS |
52 | #if wxUSE_DATETIME |
53 | #include "wx/datetime.h" | |
b0f76951 | 54 | #endif // wxUSE_DATETIME |
d980b3e1 | 55 | |
a2fd865b VS |
56 | #if wxUSE_OLE_AUTOMATION |
57 | ||
cf1a582a VZ |
58 | #include <wx/vector.h> |
59 | ||
6eefca4f | 60 | // Report an OLE error when calling the specified method to the user via wxLog. |
84f4eef8 VZ |
61 | static void |
62 | ShowException(const wxString& member, | |
63 | HRESULT hr, | |
6eefca4f VZ |
64 | EXCEPINFO *pexcep = NULL, |
65 | unsigned int uiArgErr = 0); | |
84f4eef8 VZ |
66 | |
67 | // wxAutomationObject | |
d980b3e1 JS |
68 | |
69 | wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr) | |
70 | { | |
0a0e6a5b | 71 | m_dispatchPtr = dispatchPtr; |
f239af65 | 72 | m_lcid = LOCALE_SYSTEM_DEFAULT; |
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]), | |
f239af65 | 164 | 1 + namedArgCount, m_lcid, &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 | 189 | dispparams.rgdispidNamedArgs = &dispIds[0] + 1; |
5d8cdb5d | 190 | dispparams.rgvarg = oleArgs.empty() ? NULL : &oleArgs[0]; |
0a0e6a5b WS |
191 | dispparams.cArgs = noArgs; |
192 | dispparams.cNamedArgs = namedArgCount; | |
193 | ||
c50ab33d VZ |
194 | EXCEPINFO excep; |
195 | wxZeroMemory(excep); | |
0a0e6a5b | 196 | |
f239af65 | 197 | hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, m_lcid, |
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 | } |
226fa6db VZ |
224 | // Mustn't free the SAFEARRAY if it is contained in the retValue |
225 | if ((vReturn.vt & VT_ARRAY) && | |
226 | retValue.GetType() == wxS("safearray")) | |
227 | { | |
228 | vReturn.parray = NULL; | |
229 | } | |
12335fa6 | 230 | } |
0a0e6a5b WS |
231 | } |
232 | return true; | |
d980b3e1 JS |
233 | } |
234 | ||
235 | // Invoke a member function | |
236 | wxVariant wxAutomationObject::CallMethod(const wxString& member, int noArgs, wxVariant args[]) | |
237 | { | |
0a0e6a5b WS |
238 | wxVariant retVariant; |
239 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, args)) | |
240 | { | |
241 | retVariant.MakeNull(); | |
242 | } | |
243 | return retVariant; | |
d980b3e1 JS |
244 | } |
245 | ||
24f4ad95 JS |
246 | wxVariant wxAutomationObject::CallMethodArray(const wxString& member, int noArgs, const wxVariant **args) |
247 | { | |
0a0e6a5b WS |
248 | wxVariant retVariant; |
249 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, NULL, args)) | |
250 | { | |
251 | retVariant.MakeNull(); | |
252 | } | |
253 | return retVariant; | |
24f4ad95 JS |
254 | } |
255 | ||
d980b3e1 | 256 | wxVariant wxAutomationObject::CallMethod(const wxString& member, |
0a0e6a5b WS |
257 | const wxVariant& arg1, const wxVariant& arg2, |
258 | const wxVariant& arg3, const wxVariant& arg4, | |
259 | const wxVariant& arg5, const wxVariant& arg6) | |
d980b3e1 | 260 | { |
0a0e6a5b WS |
261 | const wxVariant** args = new const wxVariant*[6]; |
262 | int i = 0; | |
263 | if (!arg1.IsNull()) | |
264 | { | |
265 | args[i] = & arg1; | |
266 | i ++; | |
267 | } | |
268 | if (!arg2.IsNull()) | |
269 | { | |
270 | args[i] = & arg2; | |
271 | i ++; | |
272 | } | |
273 | if (!arg3.IsNull()) | |
274 | { | |
275 | args[i] = & arg3; | |
276 | i ++; | |
277 | } | |
278 | if (!arg4.IsNull()) | |
279 | { | |
280 | args[i] = & arg4; | |
281 | i ++; | |
282 | } | |
283 | if (!arg5.IsNull()) | |
284 | { | |
285 | args[i] = & arg5; | |
286 | i ++; | |
287 | } | |
288 | if (!arg6.IsNull()) | |
289 | { | |
290 | args[i] = & arg6; | |
291 | i ++; | |
292 | } | |
293 | wxVariant retVariant; | |
294 | if (!Invoke(member, DISPATCH_METHOD, retVariant, i, NULL, args)) | |
295 | { | |
296 | retVariant.MakeNull(); | |
297 | } | |
298 | delete[] args; | |
299 | return retVariant; | |
d980b3e1 JS |
300 | } |
301 | ||
302 | // Get/Set property | |
24f4ad95 JS |
303 | wxVariant wxAutomationObject::GetPropertyArray(const wxString& property, int noArgs, const wxVariant **args) const |
304 | { | |
0a0e6a5b WS |
305 | wxVariant retVariant; |
306 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args)) | |
307 | { | |
308 | retVariant.MakeNull(); | |
309 | } | |
310 | return retVariant; | |
24f4ad95 | 311 | } |
d980b3e1 JS |
312 | wxVariant wxAutomationObject::GetProperty(const wxString& property, int noArgs, wxVariant args[]) const |
313 | { | |
0a0e6a5b WS |
314 | wxVariant retVariant; |
315 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
316 | { | |
317 | retVariant.MakeNull(); | |
318 | } | |
319 | return retVariant; | |
d980b3e1 JS |
320 | } |
321 | ||
322 | wxVariant wxAutomationObject::GetProperty(const wxString& property, | |
0a0e6a5b WS |
323 | const wxVariant& arg1, const wxVariant& arg2, |
324 | const wxVariant& arg3, const wxVariant& arg4, | |
325 | const wxVariant& arg5, const wxVariant& arg6) | |
d980b3e1 | 326 | { |
0a0e6a5b WS |
327 | const wxVariant** args = new const wxVariant*[6]; |
328 | int i = 0; | |
329 | if (!arg1.IsNull()) | |
330 | { | |
331 | args[i] = & arg1; | |
332 | i ++; | |
333 | } | |
334 | if (!arg2.IsNull()) | |
335 | { | |
336 | args[i] = & arg2; | |
337 | i ++; | |
338 | } | |
339 | if (!arg3.IsNull()) | |
340 | { | |
341 | args[i] = & arg3; | |
342 | i ++; | |
343 | } | |
344 | if (!arg4.IsNull()) | |
345 | { | |
346 | args[i] = & arg4; | |
347 | i ++; | |
348 | } | |
349 | if (!arg5.IsNull()) | |
350 | { | |
351 | args[i] = & arg5; | |
352 | i ++; | |
353 | } | |
354 | if (!arg6.IsNull()) | |
355 | { | |
356 | args[i] = & arg6; | |
357 | i ++; | |
358 | } | |
359 | wxVariant retVariant; | |
360 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, i, NULL, args)) | |
361 | { | |
362 | retVariant.MakeNull(); | |
363 | } | |
364 | delete[] args; | |
365 | return retVariant; | |
d980b3e1 JS |
366 | } |
367 | ||
368 | bool wxAutomationObject::PutProperty(const wxString& property, int noArgs, wxVariant args[]) | |
369 | { | |
0a0e6a5b WS |
370 | wxVariant retVariant; |
371 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, args)) | |
372 | { | |
373 | return false; | |
374 | } | |
375 | return true; | |
d980b3e1 JS |
376 | } |
377 | ||
24f4ad95 JS |
378 | bool wxAutomationObject::PutPropertyArray(const wxString& property, int noArgs, const wxVariant **args) |
379 | { | |
0a0e6a5b WS |
380 | wxVariant retVariant; |
381 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, NULL, args)) | |
382 | { | |
383 | return false; | |
384 | } | |
385 | return true; | |
24f4ad95 JS |
386 | } |
387 | ||
d980b3e1 | 388 | bool wxAutomationObject::PutProperty(const wxString& property, |
0a0e6a5b WS |
389 | const wxVariant& arg1, const wxVariant& arg2, |
390 | const wxVariant& arg3, const wxVariant& arg4, | |
391 | const wxVariant& arg5, const wxVariant& arg6) | |
d980b3e1 | 392 | { |
0a0e6a5b WS |
393 | const wxVariant** args = new const wxVariant*[6]; |
394 | int i = 0; | |
395 | if (!arg1.IsNull()) | |
396 | { | |
397 | args[i] = & arg1; | |
398 | i ++; | |
399 | } | |
400 | if (!arg2.IsNull()) | |
401 | { | |
402 | args[i] = & arg2; | |
403 | i ++; | |
404 | } | |
405 | if (!arg3.IsNull()) | |
406 | { | |
407 | args[i] = & arg3; | |
408 | i ++; | |
409 | } | |
410 | if (!arg4.IsNull()) | |
411 | { | |
412 | args[i] = & arg4; | |
413 | i ++; | |
414 | } | |
415 | if (!arg5.IsNull()) | |
416 | { | |
417 | args[i] = & arg5; | |
418 | i ++; | |
419 | } | |
420 | if (!arg6.IsNull()) | |
421 | { | |
422 | args[i] = & arg6; | |
423 | i ++; | |
424 | } | |
425 | wxVariant retVariant; | |
426 | bool ret = Invoke(property, DISPATCH_PROPERTYPUT, retVariant, i, NULL, args); | |
427 | delete[] args; | |
428 | return ret; | |
d980b3e1 JS |
429 | } |
430 | ||
431 | ||
432 | // Uses DISPATCH_PROPERTYGET | |
433 | // and returns a dispatch pointer. The calling code should call Release | |
434 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
435 | // with it and letting the destructor call Release. | |
436 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, wxVariant args[]) const | |
437 | { | |
0a0e6a5b WS |
438 | wxVariant retVariant; |
439 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
440 | { | |
441 | if (retVariant.GetType() == wxT("void*")) | |
442 | { | |
443 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
444 | } | |
445 | } | |
446 | ||
d3b9f782 | 447 | return NULL; |
d980b3e1 JS |
448 | } |
449 | ||
24f4ad95 JS |
450 | // Uses DISPATCH_PROPERTYGET |
451 | // and returns a dispatch pointer. The calling code should call Release | |
452 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
453 | // with it and letting the destructor call Release. | |
454 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, const wxVariant **args) const | |
455 | { | |
0a0e6a5b WS |
456 | wxVariant retVariant; |
457 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args)) | |
458 | { | |
459 | if (retVariant.GetType() == wxT("void*")) | |
460 | { | |
461 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
462 | } | |
463 | } | |
464 | ||
d3b9f782 | 465 | return NULL; |
24f4ad95 JS |
466 | } |
467 | ||
468 | ||
d980b3e1 JS |
469 | // A way of initialising another wxAutomationObject with a dispatch object |
470 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, wxVariant args[]) const | |
471 | { | |
0a0e6a5b WS |
472 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); |
473 | if (dispatch) | |
474 | { | |
475 | obj.SetDispatchPtr(dispatch); | |
f239af65 | 476 | obj.SetLCID(GetLCID()); |
0a0e6a5b WS |
477 | return true; |
478 | } | |
479 | else | |
480 | return false; | |
d980b3e1 JS |
481 | } |
482 | ||
24f4ad95 JS |
483 | // A way of initialising another wxAutomationObject with a dispatch object |
484 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, const wxVariant **args) const | |
485 | { | |
0a0e6a5b WS |
486 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); |
487 | if (dispatch) | |
488 | { | |
489 | obj.SetDispatchPtr(dispatch); | |
f239af65 | 490 | obj.SetLCID(GetLCID()); |
0a0e6a5b WS |
491 | return true; |
492 | } | |
493 | else | |
494 | return false; | |
24f4ad95 JS |
495 | } |
496 | ||
6eefca4f VZ |
497 | namespace |
498 | { | |
499 | ||
500 | HRESULT wxCLSIDFromProgID(const wxString& progId, CLSID& clsId) | |
501 | { | |
502 | HRESULT hr = CLSIDFromProgID(wxBasicString(progId), &clsId); | |
503 | if ( FAILED(hr) ) | |
504 | { | |
505 | wxLogSysError(hr, _("Failed to find CLSID of \"%s\""), progId); | |
506 | } | |
507 | return hr; | |
508 | } | |
509 | ||
510 | void *DoCreateInstance(const wxString& progId, const CLSID& clsId) | |
511 | { | |
512 | // get the server IDispatch interface | |
513 | // | |
514 | // NB: using CLSCTX_INPROC_HANDLER results in failure when getting | |
515 | // Automation interface for Microsoft Office applications so don't use | |
516 | // CLSCTX_ALL which includes it | |
517 | void *pDispatch = NULL; | |
518 | HRESULT hr = CoCreateInstance(clsId, NULL, CLSCTX_SERVER, | |
519 | IID_IDispatch, &pDispatch); | |
520 | if (FAILED(hr)) | |
521 | { | |
522 | wxLogSysError(hr, _("Failed to create an instance of \"%s\""), progId); | |
523 | return NULL; | |
524 | } | |
525 | ||
526 | return pDispatch; | |
527 | } | |
528 | ||
529 | } // anonymous namespace | |
530 | ||
d980b3e1 | 531 | // Get a dispatch pointer from the current object associated |
27d76879 | 532 | // with a ProgID |
6eefca4f | 533 | bool wxAutomationObject::GetInstance(const wxString& progId, int flags) const |
d980b3e1 | 534 | { |
0a0e6a5b WS |
535 | if (m_dispatchPtr) |
536 | return false; | |
537 | ||
538 | CLSID clsId; | |
6eefca4f | 539 | HRESULT hr = wxCLSIDFromProgID(progId, clsId); |
84f4eef8 | 540 | if (FAILED(hr)) |
0a0e6a5b | 541 | return false; |
0a0e6a5b | 542 | |
6eefca4f | 543 | IUnknown *pUnk = NULL; |
84f4eef8 VZ |
544 | hr = GetActiveObject(clsId, NULL, &pUnk); |
545 | if (FAILED(hr)) | |
0a0e6a5b | 546 | { |
6eefca4f VZ |
547 | if ( flags & wxAutomationInstance_CreateIfNeeded ) |
548 | { | |
549 | const_cast<wxAutomationObject *>(this)-> | |
550 | m_dispatchPtr = DoCreateInstance(progId, clsId); | |
551 | if ( m_dispatchPtr ) | |
552 | return true; | |
553 | } | |
554 | else | |
555 | { | |
1244d2e0 VZ |
556 | // Log an error except if we're supposed to fail silently when the |
557 | // error is that no current instance exists. | |
558 | if ( hr != MK_E_UNAVAILABLE || | |
559 | !(flags & wxAutomationInstance_SilentIfNone) ) | |
560 | { | |
561 | wxLogSysError(hr, | |
562 | _("Cannot get an active instance of \"%s\""), | |
563 | progId); | |
564 | } | |
6eefca4f VZ |
565 | } |
566 | ||
0a0e6a5b WS |
567 | return false; |
568 | } | |
569 | ||
84f4eef8 VZ |
570 | hr = pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr); |
571 | if (FAILED(hr)) | |
0a0e6a5b | 572 | { |
6eefca4f VZ |
573 | wxLogSysError(hr, |
574 | _("Failed to get OLE automation interface for \"%s\""), | |
575 | progId); | |
0a0e6a5b WS |
576 | return false; |
577 | } | |
578 | ||
579 | return true; | |
d980b3e1 JS |
580 | } |
581 | ||
582 | // Get a dispatch pointer from a new object associated | |
27d76879 VZ |
583 | // with the given ProgID |
584 | bool wxAutomationObject::CreateInstance(const wxString& progId) const | |
d980b3e1 | 585 | { |
0a0e6a5b WS |
586 | if (m_dispatchPtr) |
587 | return false; | |
588 | ||
589 | CLSID clsId; | |
6eefca4f | 590 | HRESULT hr = wxCLSIDFromProgID(progId, clsId); |
84f4eef8 | 591 | if (FAILED(hr)) |
0a0e6a5b | 592 | return false; |
0a0e6a5b | 593 | |
6eefca4f VZ |
594 | const_cast<wxAutomationObject *>(this)-> |
595 | m_dispatchPtr = DoCreateInstance(progId, clsId); | |
0a0e6a5b | 596 | |
6eefca4f | 597 | return m_dispatchPtr != NULL; |
d980b3e1 JS |
598 | } |
599 | ||
f239af65 VZ |
600 | LCID wxAutomationObject::GetLCID() const |
601 | { | |
602 | return m_lcid; | |
603 | } | |
604 | ||
605 | void wxAutomationObject::SetLCID(LCID lcid) | |
606 | { | |
607 | m_lcid = lcid; | |
608 | } | |
609 | ||
84f4eef8 VZ |
610 | static void |
611 | ShowException(const wxString& member, | |
612 | HRESULT hr, | |
613 | EXCEPINFO *pexcep, | |
614 | unsigned int uiArgErr) | |
d980b3e1 | 615 | { |
84f4eef8 | 616 | wxString message; |
0a0e6a5b WS |
617 | switch (GetScode(hr)) |
618 | { | |
619 | case DISP_E_UNKNOWNNAME: | |
6eefca4f | 620 | message = _("Unknown name or named argument."); |
0a0e6a5b WS |
621 | break; |
622 | ||
623 | case DISP_E_BADPARAMCOUNT: | |
6eefca4f | 624 | message = _("Incorrect number of arguments."); |
0a0e6a5b WS |
625 | break; |
626 | ||
627 | case DISP_E_EXCEPTION: | |
6eefca4f VZ |
628 | if ( pexcep ) |
629 | { | |
630 | if ( pexcep->bstrDescription ) | |
631 | message << pexcep->bstrDescription << wxS(" "); | |
632 | message += wxString::Format(wxS("error code %u"), pexcep->wCode); | |
633 | } | |
634 | else | |
84f4eef8 | 635 | { |
6eefca4f | 636 | message = _("Unknown exception"); |
84f4eef8 | 637 | } |
0a0e6a5b WS |
638 | break; |
639 | ||
640 | case DISP_E_MEMBERNOTFOUND: | |
6eefca4f | 641 | message = _("Method or property not found."); |
0a0e6a5b WS |
642 | break; |
643 | ||
644 | case DISP_E_OVERFLOW: | |
6eefca4f | 645 | message = _("Overflow while coercing argument values."); |
0a0e6a5b WS |
646 | break; |
647 | ||
648 | case DISP_E_NONAMEDARGS: | |
6eefca4f | 649 | message = _("Object implementation does not support named arguments."); |
0a0e6a5b WS |
650 | break; |
651 | ||
652 | case DISP_E_UNKNOWNLCID: | |
6eefca4f | 653 | message = _("The locale ID is unknown."); |
0a0e6a5b WS |
654 | break; |
655 | ||
656 | case DISP_E_PARAMNOTOPTIONAL: | |
6eefca4f | 657 | message = _("Missing a required parameter."); |
0a0e6a5b WS |
658 | break; |
659 | ||
660 | case DISP_E_PARAMNOTFOUND: | |
6eefca4f | 661 | message.Printf(_("Argument %u not found."), uiArgErr); |
0a0e6a5b WS |
662 | break; |
663 | ||
664 | case DISP_E_TYPEMISMATCH: | |
6eefca4f | 665 | message.Printf(_("Type mismatch in argument %u."), uiArgErr); |
84f4eef8 VZ |
666 | break; |
667 | ||
668 | case ERROR_FILE_NOT_FOUND: | |
6eefca4f | 669 | message = _("The system cannot find the file specified."); |
84f4eef8 VZ |
670 | break; |
671 | ||
672 | case REGDB_E_CLASSNOTREG: | |
6eefca4f | 673 | message = _("Class not registered."); |
0a0e6a5b WS |
674 | break; |
675 | ||
676 | default: | |
6eefca4f | 677 | message.Printf(_("Unknown error %08x"), hr); |
0a0e6a5b WS |
678 | break; |
679 | } | |
680 | ||
6eefca4f | 681 | wxLogError(_("OLE Automation error in %s: %s"), member, message); |
d980b3e1 JS |
682 | } |
683 | ||
84f4eef8 | 684 | #endif // wxUSE_OLE_AUTOMATION |