]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/automtn.cpp
honor wxMAXIMIZE frame style, fixes #11631
[wxWidgets.git] / src / msw / ole / automtn.cpp
CommitLineData
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
d980b3e1
JS
57// static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
58
a2fd865b
VS
59#if wxUSE_OLE_AUTOMATION
60
d980b3e1
JS
61/*
62 * wxAutomationObject
63 */
64
65wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr)
66{
0a0e6a5b 67 m_dispatchPtr = dispatchPtr;
d980b3e1
JS
68}
69
70wxAutomationObject::~wxAutomationObject()
71{
0a0e6a5b
WS
72 if (m_dispatchPtr)
73 {
74 ((IDispatch*)m_dispatchPtr)->Release();
75 m_dispatchPtr = NULL;
76 }
d980b3e1
JS
77}
78
79#define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
80
81// For Put/Get, no named arguments are allowed.
82bool wxAutomationObject::Invoke(const wxString& member, int action,
83 wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const
84{
0a0e6a5b
WS
85 if (!m_dispatchPtr)
86 return false;
87
88 // nonConstMember is necessary because the wxString class doesn't have enough consts...
89 wxString nonConstMember(member);
90
91 int ch = nonConstMember.Find('.');
92 if (ch != -1)
93 {
94 // Use dot notation to get the next object
95 wxString member2(nonConstMember.Left((size_t) ch));
e4db172a 96 wxString rest(nonConstMember.Right(nonConstMember.length() - ch - 1));
0a0e6a5b
WS
97 wxAutomationObject obj;
98 if (!GetObject(obj, member2))
99 return false;
100 return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs);
101 }
102
103 VARIANTARG vReturn;
1fbfbfb0 104 VariantInit(& vReturn);
0a0e6a5b
WS
105
106 VARIANTARG* vReturnPtr = & vReturn;
107
108 // Find number of names args
109 int namedArgCount = 0;
110 int i;
111 for (i = 0; i < noArgs; i++)
112 if (!INVOKEARG(i).GetName().IsNull())
12335fa6 113 {
0a0e6a5b
WS
114 namedArgCount ++;
115 }
116
117 int namedArgStringCount = namedArgCount + 1;
118 BSTR* argNames = new BSTR[namedArgStringCount];
119 argNames[0] = wxConvertStringToOle(member);
120
121 // Note that arguments are specified in reverse order
122 // (all totally logical; hey, we're dealing with OLE here.)
123
124 int j = 0;
125 for (i = 0; i < namedArgCount; i++)
126 {
127 if (!INVOKEARG(i).GetName().IsNull())
128 {
129 argNames[(namedArgCount-j)] = wxConvertStringToOle(INVOKEARG(i).GetName());
130 j ++;
131 }
132 }
133
134 // + 1 for the member name, + 1 again in case we're a 'put'
135 DISPID* dispIds = new DISPID[namedArgCount + 2];
136
137 HRESULT hr;
138 DISPPARAMS dispparams;
139 unsigned int uiArgErr;
0a0e6a5b
WS
140
141 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
142 // member name as the first name, followed by argument names (if any).
143 hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, argNames,
144 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, dispIds);
145 if (FAILED(hr))
146 {
147// ShowException(szMember, hr, NULL, 0);
148 delete[] argNames;
149 delete[] dispIds;
150 return false;
151 }
152
153 // if doing a property put(ref), we need to adjust the first argument to have a
154 // named arg of DISPID_PROPERTYPUT.
155 if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF))
156 {
157 namedArgCount = 1;
158 dispIds[1] = DISPID_PROPERTYPUT;
d3b9f782 159 vReturnPtr = NULL;
0a0e6a5b
WS
160 }
161
162 // Convert the wxVariants to VARIANTARGs
163 VARIANTARG* oleArgs = new VARIANTARG[noArgs];
164 for (i = 0; i < noArgs; i++)
165 {
166 // Again, reverse args
167 if (!wxConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i]))
168 {
169 delete[] argNames;
170 delete[] dispIds;
12335fa6 171 delete[] oleArgs;
0a0e6a5b
WS
172 return false;
173 }
174 }
175
176 dispparams.rgdispidNamedArgs = dispIds + 1;
177 dispparams.rgvarg = oleArgs;
178 dispparams.cArgs = noArgs;
179 dispparams.cNamedArgs = namedArgCount;
180
c50ab33d
VZ
181 EXCEPINFO excep;
182 wxZeroMemory(excep);
0a0e6a5b
WS
183
184 hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT,
5c519b6c 185 (WORD)action, &dispparams, vReturnPtr, &excep, &uiArgErr);
0a0e6a5b
WS
186
187 for (i = 0; i < namedArgStringCount; i++)
188 {
189 SysFreeString(argNames[i]);
190 }
191 delete[] argNames;
192 delete[] dispIds;
193
194 for (i = 0; i < noArgs; i++)
1fbfbfb0 195 VariantClear(& oleArgs[i]) ;
0a0e6a5b
WS
196 delete[] oleArgs;
197
198 if (FAILED(hr))
199 {
200 // display the exception information if appropriate:
201// ShowException((const char*) member, hr, &excep, uiArgErr);
202
203 // free exception structure information
204 SysFreeString(excep.bstrSource);
205 SysFreeString(excep.bstrDescription);
206 SysFreeString(excep.bstrHelpFile);
207
208 if (vReturnPtr)
1fbfbfb0 209 VariantClear(vReturnPtr);
0a0e6a5b
WS
210 return false;
211 }
212 else
213 {
214 if (vReturnPtr)
215 {
216 // Convert result to wxVariant form
217 wxConvertOleToVariant(vReturn, retValue);
218 // Mustn't release the dispatch pointer
219 if (vReturn.vt == VT_DISPATCH)
220 {
d3b9f782 221 vReturn.pdispVal = NULL;
0a0e6a5b 222 }
1fbfbfb0 223 VariantClear(& vReturn);
12335fa6 224 }
0a0e6a5b
WS
225 }
226 return true;
d980b3e1
JS
227}
228
229// Invoke a member function
230wxVariant 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
240wxVariant 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 250wxVariant 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
297wxVariant 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
306wxVariant 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
316wxVariant 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
362bool 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
372bool 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 382bool 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.
430WXIDISPATCH* 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.
448WXIDISPATCH* 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
464bool 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
477bool 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
d980b3e1
JS
489// Get a dispatch pointer from the current object associated
490// with a class id
491bool wxAutomationObject::GetInstance(const wxString& classId) const
492{
0a0e6a5b
WS
493 if (m_dispatchPtr)
494 return false;
495
496 CLSID clsId;
497 IUnknown * pUnk = NULL;
498
5149f75b 499 wxBasicString unicodeName(classId);
0a0e6a5b
WS
500
501 if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId)))
502 {
503 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
504 return false;
505 }
506
507 if (FAILED(GetActiveObject(clsId, NULL, &pUnk)))
508 {
509 wxLogWarning(wxT("Cannot find an active object"));
510 return false;
511 }
512
513 if (pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr) != S_OK)
514 {
515 wxLogWarning(wxT("Cannot find IDispatch interface"));
516 return false;
517 }
518
519 return true;
d980b3e1
JS
520}
521
522// Get a dispatch pointer from a new object associated
523// with the given class id
524bool wxAutomationObject::CreateInstance(const wxString& classId) const
525{
0a0e6a5b
WS
526 if (m_dispatchPtr)
527 return false;
528
529 CLSID clsId;
530
5149f75b 531 wxBasicString unicodeName(classId);
0a0e6a5b
WS
532
533 if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId)))
534 {
535 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
536 return false;
537 }
538
06936905
VZ
539 // get the server IDispatch interface
540 //
541 // NB: using CLSCTX_INPROC_HANDLER results in failure when getting
542 // Automation interface for Microsoft Office applications so don't use
543 // CLSCTX_ALL which includes it
544 if (FAILED(CoCreateInstance(clsId, NULL, CLSCTX_SERVER, IID_IDispatch,
545 (void**)&m_dispatchPtr)))
0a0e6a5b
WS
546 {
547 wxLogWarning(wxT("Cannot start an instance of this class."));
548 return false;
549 }
550
551 return true;
d980b3e1
JS
552}
553
a2fd865b 554#endif // wxUSE_OLE_AUTOMATION
d980b3e1
JS
555
556#if 0
557
558void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr)
559{
0a0e6a5b
WS
560 TCHAR szBuf[512];
561
562 switch (GetScode(hr))
563 {
564 case DISP_E_UNKNOWNNAME:
565 wsprintf(szBuf, L"%s: Unknown name or named argument.", szMember);
566 break;
567
568 case DISP_E_BADPARAMCOUNT:
569 wsprintf(szBuf, L"%s: Incorrect number of arguments.", szMember);
570 break;
571
572 case DISP_E_EXCEPTION:
573 wsprintf(szBuf, L"%s: Error %d: ", szMember, pexcep->wCode);
574 if (pexcep->bstrDescription != NULL)
575 lstrcat(szBuf, pexcep->bstrDescription);
576 else
577 lstrcat(szBuf, L"<<No Description>>");
578 break;
579
580 case DISP_E_MEMBERNOTFOUND:
581 wsprintf(szBuf, L"%s: method or property not found.", szMember);
582 break;
583
584 case DISP_E_OVERFLOW:
585 wsprintf(szBuf, L"%s: Overflow while coercing argument values.", szMember);
586 break;
587
588 case DISP_E_NONAMEDARGS:
589 wsprintf(szBuf, L"%s: Object implementation does not support named arguments.",
590 szMember);
591 break;
592
593 case DISP_E_UNKNOWNLCID:
594 wsprintf(szBuf, L"%s: The locale ID is unknown.", szMember);
595 break;
596
597 case DISP_E_PARAMNOTOPTIONAL:
598 wsprintf(szBuf, L"%s: Missing a required parameter.", szMember);
599 break;
600
601 case DISP_E_PARAMNOTFOUND:
602 wsprintf(szBuf, L"%s: Argument not found, argument %d.", szMember, uiArgErr);
603 break;
604
605 case DISP_E_TYPEMISMATCH:
606 wsprintf(szBuf, L"%s: Type mismatch, argument %d.", szMember, uiArgErr);
607 break;
608
609 default:
3103e8a9 610 wsprintf(szBuf, L"%s: Unknown error occurred.", szMember);
0a0e6a5b
WS
611 break;
612 }
613
614 wxLogWarning(szBuf);
d980b3e1
JS
615}
616
617#endif