]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #ifdef __GNUG__ | |
13 | #pragma implementation "automtn.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #if defined(__BORLANDC__) | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #include "wx/log.h" | |
24 | ||
25 | #include <math.h> | |
26 | #include <time.h> | |
27 | ||
28 | #include "wx/msw/ole/automtn.h" | |
29 | ||
30 | #include "wx/msw/private.h" | |
31 | ||
32 | #include <ole2ver.h> | |
33 | #include <oleauto.h> | |
34 | ||
35 | // wrapper around BSTR type (by Vadim Zeitlin) | |
36 | ||
37 | class WXDLLEXPORT BasicString | |
38 | { | |
39 | public: | |
40 | // ctors & dtor | |
41 | BasicString(const char *sz); | |
42 | ~BasicString(); | |
43 | ||
44 | // accessors | |
45 | // just get the string | |
46 | operator BSTR() const { return m_wzBuf; } | |
47 | // retrieve a copy of our string - caller must SysFreeString() it later! | |
48 | BSTR Get() const { return SysAllocString(m_wzBuf); } | |
49 | ||
50 | private: | |
51 | // @@@ not implemented (but should be) | |
52 | BasicString(const BasicString&); | |
53 | BasicString& operator=(const BasicString&); | |
54 | ||
55 | OLECHAR *m_wzBuf; // actual string | |
56 | }; | |
57 | ||
58 | // Convert variants | |
59 | static bool ConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant) ; | |
60 | static bool ConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant) ; | |
61 | ||
62 | // Convert string to Unicode | |
63 | static BSTR ConvertStringToOle(const wxString& str); | |
64 | ||
65 | // Convert string from BSTR to wxString | |
66 | static wxString ConvertStringFromOle(BSTR bStr); | |
67 | ||
68 | // Verifies will fail if the needed buffer size is too large | |
69 | #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp | |
70 | #define MIN_DATE (-657434L) // about year 100 | |
71 | #define MAX_DATE 2958465L // about year 9999 | |
72 | ||
73 | // Half a second, expressed in days | |
74 | #define HALF_SECOND (1.0/172800.0) | |
75 | ||
76 | // One-based array of days in year at month start | |
77 | static int rgMonthDays[13] = | |
78 | {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; | |
79 | ||
80 | static BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay, | |
81 | WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest); | |
82 | static BOOL TmFromOleDate(DATE dtSrc, struct tm& tmDest); | |
83 | static void TmConvertToStandardFormat(struct tm& tmSrc); | |
84 | static double DoubleFromDate(DATE dt); | |
85 | static DATE DateFromDouble(double dbl); | |
86 | ||
87 | static void ClearVariant(VARIANTARG *pvarg) ; | |
88 | static void ReleaseVariant(VARIANTARG *pvarg) ; | |
89 | // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr); | |
90 | ||
91 | /* | |
92 | * wxAutomationObject | |
93 | */ | |
94 | ||
95 | wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr) | |
96 | { | |
97 | m_dispatchPtr = dispatchPtr; | |
98 | } | |
99 | ||
100 | wxAutomationObject::~wxAutomationObject() | |
101 | { | |
102 | if (m_dispatchPtr) | |
103 | { | |
104 | ((IDispatch*)m_dispatchPtr)->Release(); | |
105 | m_dispatchPtr = NULL; | |
106 | } | |
107 | } | |
108 | ||
109 | #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i])) | |
110 | ||
111 | // For Put/Get, no named arguments are allowed. | |
112 | bool wxAutomationObject::Invoke(const wxString& member, int action, | |
113 | wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const | |
114 | { | |
115 | if (!m_dispatchPtr) | |
116 | return FALSE; | |
117 | ||
118 | // nonConstMember is necessary because the wxString class doesn't have enough consts... | |
119 | wxString nonConstMember(member); | |
120 | ||
121 | int ch = nonConstMember.Find('.'); | |
122 | if (ch != -1) | |
123 | { | |
124 | // Use dot notation to get the next object | |
125 | wxString member2(nonConstMember.Left((size_t) ch)); | |
126 | wxString rest(nonConstMember.Right(nonConstMember.Length() - ch - 1)); | |
127 | wxAutomationObject obj; | |
128 | if (!GetObject(obj, member2)) | |
129 | return FALSE; | |
130 | return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs); | |
131 | } | |
132 | ||
133 | VARIANTARG vReturn; | |
134 | ClearVariant(& vReturn); | |
135 | ||
136 | VARIANTARG* vReturnPtr = & vReturn; | |
137 | ||
138 | // Find number of names args | |
139 | int namedArgCount = 0; | |
140 | int i; | |
141 | for (i = 0; i < noArgs; i++) | |
142 | if (!INVOKEARG(i).GetName().IsNull()) | |
143 | { | |
144 | namedArgCount ++; | |
145 | } | |
146 | ||
147 | int namedArgStringCount = namedArgCount + 1; | |
148 | BSTR* argNames = new BSTR[namedArgStringCount]; | |
149 | argNames[0] = ConvertStringToOle(member); | |
150 | ||
151 | // Note that arguments are specified in reverse order | |
152 | // (all totally logical; hey, we're dealing with OLE here.) | |
153 | ||
154 | int j = 0; | |
155 | for (i = 0; i < namedArgCount; i++) | |
156 | { | |
157 | if (!INVOKEARG(i).GetName().IsNull()) | |
158 | { | |
159 | argNames[(namedArgCount-j)] = ConvertStringToOle(INVOKEARG(i).GetName()); | |
160 | j ++; | |
161 | } | |
162 | } | |
163 | ||
164 | // + 1 for the member name, + 1 again in case we're a 'put' | |
165 | DISPID* dispIds = new DISPID[namedArgCount + 2]; | |
166 | ||
167 | HRESULT hr; | |
168 | DISPPARAMS dispparams; | |
169 | unsigned int uiArgErr; | |
170 | EXCEPINFO excep; | |
171 | ||
172 | // Get the IDs for the member and its arguments. GetIDsOfNames expects the | |
173 | // member name as the first name, followed by argument names (if any). | |
174 | hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, argNames, | |
175 | 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, dispIds); | |
176 | if (FAILED(hr)) | |
177 | { | |
178 | // ShowException(szMember, hr, NULL, 0); | |
179 | return FALSE; | |
180 | } | |
181 | ||
182 | // if doing a property put(ref), we need to adjust the first argument to have a | |
183 | // named arg of DISPID_PROPERTYPUT. | |
184 | if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF)) | |
185 | { | |
186 | namedArgCount = 1; | |
187 | dispIds[1] = DISPID_PROPERTYPUT; | |
188 | vReturnPtr = (VARIANTARG*) NULL; | |
189 | } | |
190 | ||
191 | // Convert the wxVariants to VARIANTARGs | |
192 | VARIANTARG* oleArgs = new VARIANTARG[noArgs]; | |
193 | for (i = 0; i < noArgs; i++) | |
194 | { | |
195 | // Again, reverse args | |
196 | if (!ConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i])) | |
197 | return FALSE; // TODO: clean up memory at this point | |
198 | } | |
199 | ||
200 | dispparams.rgdispidNamedArgs = dispIds + 1; | |
201 | dispparams.rgvarg = oleArgs; | |
202 | dispparams.cArgs = noArgs; | |
203 | dispparams.cNamedArgs = namedArgCount; | |
204 | ||
205 | excep.pfnDeferredFillIn = NULL; | |
206 | ||
207 | hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT, | |
208 | action, &dispparams, vReturnPtr, &excep, &uiArgErr); | |
209 | ||
210 | for (i = 0; i < namedArgStringCount; i++) | |
211 | { | |
212 | SysFreeString(argNames[i]); | |
213 | } | |
214 | delete[] argNames; | |
215 | delete[] dispIds; | |
216 | ||
217 | for (i = 0; i < noArgs; i++) | |
218 | ReleaseVariant(& oleArgs[i]) ; | |
219 | delete[] oleArgs; | |
220 | ||
221 | if (FAILED(hr)) | |
222 | { | |
223 | // display the exception information if appropriate: | |
224 | // ShowException((const char*) member, hr, &excep, uiArgErr); | |
225 | ||
226 | // free exception structure information | |
227 | SysFreeString(excep.bstrSource); | |
228 | SysFreeString(excep.bstrDescription); | |
229 | SysFreeString(excep.bstrHelpFile); | |
230 | ||
231 | if (vReturnPtr) | |
232 | ReleaseVariant(vReturnPtr); | |
233 | return FALSE; | |
234 | } | |
235 | else | |
236 | { | |
237 | if (vReturnPtr) | |
238 | { | |
239 | // Convert result to wxVariant form | |
240 | ConvertOleToVariant(vReturn, retValue); | |
241 | // Mustn't release the dispatch pointer | |
242 | if (vReturn.vt == VT_DISPATCH) | |
243 | { | |
244 | vReturn.pdispVal = (IDispatch*) NULL; | |
245 | } | |
246 | ReleaseVariant(& vReturn); | |
247 | } | |
248 | } | |
249 | return TRUE; | |
250 | } | |
251 | ||
252 | // Invoke a member function | |
253 | wxVariant wxAutomationObject::CallMethod(const wxString& member, int noArgs, wxVariant args[]) | |
254 | { | |
255 | wxVariant retVariant; | |
256 | if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, args)) | |
257 | { | |
258 | retVariant.MakeNull(); | |
259 | } | |
260 | return retVariant; | |
261 | } | |
262 | ||
263 | wxVariant wxAutomationObject::CallMethod(const wxString& member, | |
264 | const wxVariant& arg1, const wxVariant& arg2, | |
265 | const wxVariant& arg3, const wxVariant& arg4, | |
266 | const wxVariant& arg5, const wxVariant& arg6) | |
267 | { | |
268 | const wxVariant** args = new const wxVariant*[6]; | |
269 | int i = 0; | |
270 | if (!arg1.IsNull()) | |
271 | { | |
272 | args[i] = & arg1; | |
273 | i ++; | |
274 | } | |
275 | if (!arg2.IsNull()) | |
276 | { | |
277 | args[i] = & arg2; | |
278 | i ++; | |
279 | } | |
280 | if (!arg3.IsNull()) | |
281 | { | |
282 | args[i] = & arg3; | |
283 | i ++; | |
284 | } | |
285 | if (!arg4.IsNull()) | |
286 | { | |
287 | args[i] = & arg4; | |
288 | i ++; | |
289 | } | |
290 | if (!arg5.IsNull()) | |
291 | { | |
292 | args[i] = & arg5; | |
293 | i ++; | |
294 | } | |
295 | if (!arg6.IsNull()) | |
296 | { | |
297 | args[i] = & arg6; | |
298 | i ++; | |
299 | } | |
300 | wxVariant retVariant; | |
301 | if (!Invoke(member, DISPATCH_METHOD, retVariant, i, NULL, args)) | |
302 | { | |
303 | retVariant.MakeNull(); | |
304 | } | |
305 | delete[] args; | |
306 | return retVariant; | |
307 | } | |
308 | ||
309 | // Get/Set property | |
310 | wxVariant wxAutomationObject::GetProperty(const wxString& property, int noArgs, wxVariant args[]) const | |
311 | { | |
312 | wxVariant retVariant; | |
313 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
314 | { | |
315 | retVariant.MakeNull(); | |
316 | } | |
317 | return retVariant; | |
318 | } | |
319 | ||
320 | wxVariant wxAutomationObject::GetProperty(const wxString& property, | |
321 | const wxVariant& arg1, const wxVariant& arg2, | |
322 | const wxVariant& arg3, const wxVariant& arg4, | |
323 | const wxVariant& arg5, const wxVariant& arg6) | |
324 | { | |
325 | const wxVariant** args = new const wxVariant*[6]; | |
326 | int i = 0; | |
327 | if (!arg1.IsNull()) | |
328 | { | |
329 | args[i] = & arg1; | |
330 | i ++; | |
331 | } | |
332 | if (!arg2.IsNull()) | |
333 | { | |
334 | args[i] = & arg2; | |
335 | i ++; | |
336 | } | |
337 | if (!arg3.IsNull()) | |
338 | { | |
339 | args[i] = & arg3; | |
340 | i ++; | |
341 | } | |
342 | if (!arg4.IsNull()) | |
343 | { | |
344 | args[i] = & arg4; | |
345 | i ++; | |
346 | } | |
347 | if (!arg5.IsNull()) | |
348 | { | |
349 | args[i] = & arg5; | |
350 | i ++; | |
351 | } | |
352 | if (!arg6.IsNull()) | |
353 | { | |
354 | args[i] = & arg6; | |
355 | i ++; | |
356 | } | |
357 | wxVariant retVariant; | |
358 | if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, i, NULL, args)) | |
359 | { | |
360 | retVariant.MakeNull(); | |
361 | } | |
362 | delete[] args; | |
363 | return retVariant; | |
364 | } | |
365 | ||
366 | bool wxAutomationObject::PutProperty(const wxString& property, int noArgs, wxVariant args[]) | |
367 | { | |
368 | wxVariant retVariant; | |
369 | if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, args)) | |
370 | { | |
371 | return FALSE; | |
372 | } | |
373 | return TRUE; | |
374 | } | |
375 | ||
376 | bool wxAutomationObject::PutProperty(const wxString& property, | |
377 | const wxVariant& arg1, const wxVariant& arg2, | |
378 | const wxVariant& arg3, const wxVariant& arg4, | |
379 | const wxVariant& arg5, const wxVariant& arg6) | |
380 | { | |
381 | const wxVariant** args = new const wxVariant*[6]; | |
382 | int i = 0; | |
383 | if (!arg1.IsNull()) | |
384 | { | |
385 | args[i] = & arg1; | |
386 | i ++; | |
387 | } | |
388 | if (!arg2.IsNull()) | |
389 | { | |
390 | args[i] = & arg2; | |
391 | i ++; | |
392 | } | |
393 | if (!arg3.IsNull()) | |
394 | { | |
395 | args[i] = & arg3; | |
396 | i ++; | |
397 | } | |
398 | if (!arg4.IsNull()) | |
399 | { | |
400 | args[i] = & arg4; | |
401 | i ++; | |
402 | } | |
403 | if (!arg5.IsNull()) | |
404 | { | |
405 | args[i] = & arg5; | |
406 | i ++; | |
407 | } | |
408 | if (!arg6.IsNull()) | |
409 | { | |
410 | args[i] = & arg6; | |
411 | i ++; | |
412 | } | |
413 | wxVariant retVariant; | |
414 | bool ret = Invoke(property, DISPATCH_PROPERTYPUT, retVariant, i, NULL, args); | |
415 | delete[] args; | |
416 | return ret; | |
417 | } | |
418 | ||
419 | ||
420 | // Uses DISPATCH_PROPERTYGET | |
421 | // and returns a dispatch pointer. The calling code should call Release | |
422 | // on the pointer, though this could be implicit by constructing an wxAutomationObject | |
423 | // with it and letting the destructor call Release. | |
424 | WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, wxVariant args[]) const | |
425 | { | |
426 | wxVariant retVariant; | |
427 | if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args)) | |
428 | { | |
429 | if (retVariant.GetType() == "void*") | |
430 | { | |
431 | return (WXIDISPATCH*) retVariant.GetVoidPtr(); | |
432 | } | |
433 | else | |
434 | { | |
435 | return (WXIDISPATCH*) NULL; | |
436 | } | |
437 | } | |
438 | else | |
439 | return (WXIDISPATCH*) NULL; | |
440 | } | |
441 | ||
442 | // A way of initialising another wxAutomationObject with a dispatch object | |
443 | bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, wxVariant args[]) const | |
444 | { | |
445 | WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args); | |
446 | if (dispatch) | |
447 | { | |
448 | obj.SetDispatchPtr(dispatch); | |
449 | return TRUE; | |
450 | } | |
451 | else | |
452 | return FALSE; | |
453 | } | |
454 | ||
455 | // Get a dispatch pointer from the current object associated | |
456 | // with a class id | |
457 | bool wxAutomationObject::GetInstance(const wxString& classId) const | |
458 | { | |
459 | if (m_dispatchPtr) | |
460 | return FALSE; | |
461 | ||
462 | CLSID clsId; | |
463 | IUnknown * pUnk = NULL; | |
464 | ||
465 | BasicString unicodeName((const char*) classId); | |
466 | ||
467 | if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId))) | |
468 | { | |
469 | wxLogWarning("Cannot obtain CLSID from ProgID"); | |
470 | return FALSE; | |
471 | } | |
472 | ||
473 | if (FAILED(GetActiveObject(clsId, NULL, &pUnk))) | |
474 | { | |
475 | wxLogWarning("Cannot find an active object"); | |
476 | return FALSE; | |
477 | } | |
478 | ||
479 | if (pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr) != S_OK) | |
480 | { | |
481 | wxLogWarning("Cannot find IDispatch interface"); | |
482 | return FALSE; | |
483 | } | |
484 | ||
485 | return TRUE; | |
486 | } | |
487 | ||
488 | // Get a dispatch pointer from a new object associated | |
489 | // with the given class id | |
490 | bool wxAutomationObject::CreateInstance(const wxString& classId) const | |
491 | { | |
492 | if (m_dispatchPtr) | |
493 | return FALSE; | |
494 | ||
495 | CLSID clsId; | |
496 | IUnknown * pUnk = NULL; | |
497 | ||
498 | BasicString unicodeName((const char*) classId); | |
499 | ||
500 | if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId))) | |
501 | { | |
502 | wxLogWarning("Cannot obtain CLSID from ProgID"); | |
503 | return FALSE; | |
504 | } | |
505 | ||
506 | // start a new copy of Excel, grab the IDispatch interface | |
507 | if (FAILED(CoCreateInstance(clsId, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&m_dispatchPtr))) | |
508 | { | |
509 | wxLogWarning("Cannot start an instance of this class."); | |
510 | return FALSE; | |
511 | } | |
512 | ||
513 | return TRUE; | |
514 | } | |
515 | ||
516 | ||
517 | bool ConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant) | |
518 | { | |
519 | ClearVariant(&oleVariant); | |
520 | if (variant.IsNull()) | |
521 | { | |
522 | oleVariant.vt = VT_NULL; | |
523 | return TRUE; | |
524 | } | |
525 | ||
526 | wxString type(variant.GetType()); | |
527 | ||
528 | if (type == "long") | |
529 | { | |
530 | oleVariant.vt = VT_I4; | |
531 | oleVariant.lVal = variant.GetLong() ; | |
532 | } | |
533 | else if (type == "double") | |
534 | { | |
535 | oleVariant.vt = VT_R8; | |
536 | oleVariant.dblVal = variant.GetDouble(); | |
537 | } | |
538 | else if (type == "bool") | |
539 | { | |
540 | oleVariant.vt = VT_BOOL; | |
541 | // 'bool' required for VC++ 4 apparently | |
542 | #if defined(__WATCOMC__) || (defined(__VISUALC__) && (__VISUALC__ <= 1000)) | |
543 | oleVariant.bool = variant.GetBool(); | |
544 | #else | |
545 | oleVariant.boolVal = variant.GetBool(); | |
546 | #endif | |
547 | } | |
548 | else if (type == "string") | |
549 | { | |
550 | wxString str( variant.GetString() ); | |
551 | oleVariant.vt = VT_BSTR; | |
552 | oleVariant.bstrVal = ConvertStringToOle(str); | |
553 | } | |
554 | else if (type == "date") | |
555 | { | |
556 | wxDate date( variant.GetDate() ); | |
557 | oleVariant.vt = VT_DATE; | |
558 | ||
559 | if (!OleDateFromTm(date.GetYear(), date.GetMonth(), date.GetDay(), | |
560 | 0, 0, 0, oleVariant.date)) | |
561 | return FALSE; | |
562 | } | |
563 | else if (type == "time") | |
564 | { | |
565 | wxTime time( variant.GetTime() ); | |
566 | oleVariant.vt = VT_DATE; | |
567 | ||
568 | if (!OleDateFromTm(time.GetYear(), time.GetMonth(), time.GetDay(), | |
569 | time.GetHour(), time.GetMinute(), time.GetSecond(), oleVariant.date)) | |
570 | return FALSE; | |
571 | } | |
572 | else if (type == "void*") | |
573 | { | |
574 | oleVariant.vt = VT_DISPATCH; | |
575 | oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr(); | |
576 | } | |
577 | else if (type == "list" || type == "stringlist") | |
578 | { | |
579 | oleVariant.vt = VT_VARIANT | VT_ARRAY; | |
580 | ||
581 | SAFEARRAY *psa; | |
582 | SAFEARRAYBOUND saBound; | |
583 | VARIANTARG *pvargBase; | |
584 | VARIANTARG *pvarg; | |
585 | int i, j; | |
586 | ||
587 | int iCount = variant.GetCount(); | |
588 | ||
589 | saBound.lLbound = 0; | |
590 | saBound.cElements = iCount; | |
591 | ||
592 | psa = SafeArrayCreate(VT_VARIANT, 1, &saBound); | |
593 | if (psa == NULL) | |
594 | return FALSE; | |
595 | ||
596 | SafeArrayAccessData(psa, (void**)&pvargBase); | |
597 | ||
598 | pvarg = pvargBase; | |
599 | for (i = 0; i < iCount; i++) | |
600 | { | |
601 | // copy each string in the list of strings | |
602 | wxVariant eachVariant(variant[i]); | |
603 | if (!ConvertVariantToOle(eachVariant, * pvarg)) | |
604 | { | |
605 | // memory failure: back out and free strings alloc'ed up to | |
606 | // now, and then the array itself. | |
607 | pvarg = pvargBase; | |
608 | for (j = 0; j < i; j++) | |
609 | { | |
610 | SysFreeString(pvarg->bstrVal); | |
611 | pvarg++; | |
612 | } | |
613 | SafeArrayDestroy(psa); | |
614 | return FALSE; | |
615 | } | |
616 | pvarg++; | |
617 | } | |
618 | ||
619 | SafeArrayUnaccessData(psa); | |
620 | ||
621 | oleVariant.parray = psa; | |
622 | } | |
623 | else | |
624 | { | |
625 | oleVariant.vt = VT_NULL; | |
626 | return FALSE; | |
627 | } | |
628 | return TRUE; | |
629 | } | |
630 | ||
631 | #ifndef VT_TYPEMASK | |
632 | #define VT_TYPEMASK 0xfff | |
633 | #endif | |
634 | ||
635 | bool ConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant) | |
636 | { | |
637 | switch (oleVariant.vt & VT_TYPEMASK) | |
638 | { | |
639 | case VT_BSTR: | |
640 | { | |
641 | wxString str(ConvertStringFromOle(oleVariant.bstrVal)); | |
642 | variant = str; | |
643 | break; | |
644 | } | |
645 | case VT_DATE: | |
646 | { | |
647 | struct tm tmTemp; | |
648 | if (!TmFromOleDate(oleVariant.date, tmTemp)) | |
649 | return FALSE; | |
650 | ||
651 | wxDate date(tmTemp.tm_yday, tmTemp.tm_mon, tmTemp.tm_year); | |
652 | wxTime time(date, tmTemp.tm_hour, tmTemp.tm_min, tmTemp.tm_sec); | |
653 | ||
654 | variant = time; | |
655 | break; | |
656 | } | |
657 | case VT_I4: | |
658 | { | |
659 | variant = (long) oleVariant.lVal; | |
660 | break; | |
661 | } | |
662 | case VT_I2: | |
663 | { | |
664 | variant = (long) oleVariant.iVal; | |
665 | break; | |
666 | } | |
667 | ||
668 | case VT_BOOL: | |
669 | { | |
670 | #if defined(__WATCOMC__) || (defined(_MSC_VER) && (_MSC_VER <= 1000)) //GC | |
671 | variant = (bool) (oleVariant.bool != 0); | |
672 | #else | |
673 | variant = (bool) (oleVariant.boolVal != 0); | |
674 | #endif | |
675 | break; | |
676 | } | |
677 | case VT_R8: | |
678 | { | |
679 | variant = oleVariant.dblVal; | |
680 | break; | |
681 | } | |
682 | case VT_ARRAY: | |
683 | { | |
684 | variant.ClearList(); | |
685 | ||
686 | int cDims, cElements, i; | |
687 | VARIANTARG* pvdata; | |
688 | ||
689 | // Iterate the dimensions: number of elements is x*y*z | |
690 | for (cDims = 0, cElements = 1; | |
691 | cDims < oleVariant.parray->cDims; cDims ++) | |
692 | cElements *= oleVariant.parray->rgsabound[cDims].cElements; | |
693 | ||
694 | // Get a pointer to the data | |
695 | HRESULT hr = SafeArrayAccessData(oleVariant.parray, (void HUGEP* FAR*) & pvdata); | |
696 | if (hr != NOERROR) | |
697 | return FALSE; | |
698 | // Iterate the data. | |
699 | for (i = 0; i < cElements; i++) | |
700 | { | |
701 | VARIANTARG& oleElement = pvdata[i]; | |
702 | wxVariant vElement; | |
703 | if (!ConvertOleToVariant(oleElement, vElement)) | |
704 | return FALSE; | |
705 | ||
706 | variant.Append(vElement); | |
707 | } | |
708 | SafeArrayUnaccessData(oleVariant.parray); | |
709 | break; | |
710 | } | |
711 | case VT_DISPATCH: | |
712 | { | |
713 | variant = (void*) oleVariant.pdispVal; | |
714 | break; | |
715 | } | |
716 | case VT_NULL: | |
717 | { | |
718 | variant.MakeNull(); | |
719 | break; | |
720 | } | |
721 | case VT_EMPTY: | |
722 | { | |
723 | break; // Ignore Empty Variant, used only during destruction of objects | |
724 | } | |
725 | default: | |
726 | { | |
727 | wxLogError("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"); | |
728 | return FALSE; | |
729 | } | |
730 | } | |
731 | return TRUE; | |
732 | } | |
733 | ||
734 | static BSTR ConvertStringToOle(const wxString& str) | |
735 | { | |
736 | /* | |
737 | unsigned int len = strlen((const char*) str); | |
738 | unsigned short* s = new unsigned short[len*2+2]; | |
739 | unsigned int i; | |
740 | memset(s, 0, len*2+2); | |
741 | for (i=0; i < len; i++) | |
742 | s[i*2] = str[i]; | |
743 | */ | |
744 | BasicString bstr((const char*) str); | |
745 | return bstr.Get(); | |
746 | } | |
747 | ||
748 | static wxString ConvertStringFromOle(BSTR bStr) | |
749 | { | |
750 | int len = SysStringLen(bStr) + 1; | |
751 | char *buf = new char[len]; | |
752 | int i = wcstombs( buf, bStr, len); | |
753 | ||
754 | wxString str(buf); | |
755 | delete[] buf; | |
756 | return str; | |
757 | } | |
758 | ||
759 | // ---------------------------------------------------------------------------- | |
760 | // BasicString | |
761 | // ---------------------------------------------------------------------------- | |
762 | ||
763 | // ctor takes an ANSI string and transforms it to Unicode | |
764 | BasicString::BasicString(const char *sz) | |
765 | { | |
766 | // get the size of required buffer | |
767 | UINT lenAnsi = strlen(sz); | |
768 | #ifdef __MWERKS__ | |
769 | UINT lenWide = lenAnsi * 2 ; | |
770 | #else | |
771 | UINT lenWide = mbstowcs(NULL, sz, lenAnsi); | |
772 | #endif | |
773 | ||
774 | if ( lenWide > 0 ) { | |
775 | m_wzBuf = new OLECHAR[lenWide + 1]; | |
776 | mbstowcs(m_wzBuf, sz, lenAnsi); | |
777 | m_wzBuf[lenWide] = L'\0'; | |
778 | } | |
779 | else { | |
780 | m_wzBuf = NULL; | |
781 | } | |
782 | } | |
783 | ||
784 | // dtor frees memory | |
785 | BasicString::~BasicString() | |
786 | { | |
787 | delete [] m_wzBuf; | |
788 | } | |
789 | ||
790 | ///////////////////////////////////////////////////////////////////////////// | |
791 | // COleDateTime class HELPERS - implementation | |
792 | ||
793 | BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay, | |
794 | WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest) | |
795 | { | |
796 | // Validate year and month (ignore day of week and milliseconds) | |
797 | if (wYear > 9999 || wMonth < 1 || wMonth > 12) | |
798 | return FALSE; | |
799 | ||
800 | // Check for leap year and set the number of days in the month | |
801 | BOOL bLeapYear = ((wYear & 3) == 0) && | |
802 | ((wYear % 100) != 0 || (wYear % 400) == 0); | |
803 | ||
804 | int nDaysInMonth = | |
805 | rgMonthDays[wMonth] - rgMonthDays[wMonth-1] + | |
806 | ((bLeapYear && wDay == 29 && wMonth == 2) ? 1 : 0); | |
807 | ||
808 | // Finish validating the date | |
809 | if (wDay < 1 || wDay > nDaysInMonth || | |
810 | wHour > 23 || wMinute > 59 || | |
811 | wSecond > 59) | |
812 | { | |
813 | return FALSE; | |
814 | } | |
815 | ||
816 | // Cache the date in days and time in fractional days | |
817 | long nDate; | |
818 | double dblTime; | |
819 | ||
820 | //It is a valid date; make Jan 1, 1AD be 1 | |
821 | nDate = wYear*365L + wYear/4 - wYear/100 + wYear/400 + | |
822 | rgMonthDays[wMonth-1] + wDay; | |
823 | ||
824 | // If leap year and it's before March, subtract 1: | |
825 | if (wMonth <= 2 && bLeapYear) | |
826 | --nDate; | |
827 | ||
828 | // Offset so that 12/30/1899 is 0 | |
829 | nDate -= 693959L; | |
830 | ||
831 | dblTime = (((long)wHour * 3600L) + // hrs in seconds | |
832 | ((long)wMinute * 60L) + // mins in seconds | |
833 | ((long)wSecond)) / 86400.; | |
834 | ||
835 | dtDest = (double) nDate + ((nDate >= 0) ? dblTime : -dblTime); | |
836 | ||
837 | return TRUE; | |
838 | } | |
839 | ||
840 | BOOL TmFromOleDate(DATE dtSrc, struct tm& tmDest) | |
841 | { | |
842 | // The legal range does not actually span year 0 to 9999. | |
843 | if (dtSrc > MAX_DATE || dtSrc < MIN_DATE) // about year 100 to about 9999 | |
844 | return FALSE; | |
845 | ||
846 | long nDays; // Number of days since Dec. 30, 1899 | |
847 | long nDaysAbsolute; // Number of days since 1/1/0 | |
848 | long nSecsInDay; // Time in seconds since midnight | |
849 | long nMinutesInDay; // Minutes in day | |
850 | ||
851 | long n400Years; // Number of 400 year increments since 1/1/0 | |
852 | long n400Century; // Century within 400 year block (0,1,2 or 3) | |
853 | long n4Years; // Number of 4 year increments since 1/1/0 | |
854 | long n4Day; // Day within 4 year block | |
855 | // (0 is 1/1/yr1, 1460 is 12/31/yr4) | |
856 | long n4Yr; // Year within 4 year block (0,1,2 or 3) | |
857 | BOOL bLeap4 = TRUE; // TRUE if 4 year block includes leap year | |
858 | ||
859 | double dblDate = dtSrc; // tempory serial date | |
860 | ||
861 | // If a valid date, then this conversion should not overflow | |
862 | nDays = (long)dblDate; | |
863 | ||
864 | // Round to the second | |
865 | dblDate += ((dtSrc > 0.0) ? HALF_SECOND : -HALF_SECOND); | |
866 | ||
867 | nDaysAbsolute = (long)dblDate + 693959L; // Add days from 1/1/0 to 12/30/1899 | |
868 | ||
869 | dblDate = fabs(dblDate); | |
870 | nSecsInDay = (long)((dblDate - floor(dblDate)) * 86400.); | |
871 | ||
872 | // Calculate the day of week (sun=1, mon=2...) | |
873 | // -1 because 1/1/0 is Sat. +1 because we want 1-based | |
874 | tmDest.tm_wday = (int)((nDaysAbsolute - 1) % 7L) + 1; | |
875 | ||
876 | // Leap years every 4 yrs except centuries not multiples of 400. | |
877 | n400Years = (long)(nDaysAbsolute / 146097L); | |
878 | ||
879 | // Set nDaysAbsolute to day within 400-year block | |
880 | nDaysAbsolute %= 146097L; | |
881 | ||
882 | // -1 because first century has extra day | |
883 | n400Century = (long)((nDaysAbsolute - 1) / 36524L); | |
884 | ||
885 | // Non-leap century | |
886 | if (n400Century != 0) | |
887 | { | |
888 | // Set nDaysAbsolute to day within century | |
889 | nDaysAbsolute = (nDaysAbsolute - 1) % 36524L; | |
890 | ||
891 | // +1 because 1st 4 year increment has 1460 days | |
892 | n4Years = (long)((nDaysAbsolute + 1) / 1461L); | |
893 | ||
894 | if (n4Years != 0) | |
895 | n4Day = (long)((nDaysAbsolute + 1) % 1461L); | |
896 | else | |
897 | { | |
898 | bLeap4 = FALSE; | |
899 | n4Day = (long)nDaysAbsolute; | |
900 | } | |
901 | } | |
902 | else | |
903 | { | |
904 | // Leap century - not special case! | |
905 | n4Years = (long)(nDaysAbsolute / 1461L); | |
906 | n4Day = (long)(nDaysAbsolute % 1461L); | |
907 | } | |
908 | ||
909 | if (bLeap4) | |
910 | { | |
911 | // -1 because first year has 366 days | |
912 | n4Yr = (n4Day - 1) / 365; | |
913 | ||
914 | if (n4Yr != 0) | |
915 | n4Day = (n4Day - 1) % 365; | |
916 | } | |
917 | else | |
918 | { | |
919 | n4Yr = n4Day / 365; | |
920 | n4Day %= 365; | |
921 | } | |
922 | ||
923 | // n4Day is now 0-based day of year. Save 1-based day of year, year number | |
924 | tmDest.tm_yday = (int)n4Day + 1; | |
925 | tmDest.tm_year = n400Years * 400 + n400Century * 100 + n4Years * 4 + n4Yr; | |
926 | ||
927 | // Handle leap year: before, on, and after Feb. 29. | |
928 | if (n4Yr == 0 && bLeap4) | |
929 | { | |
930 | // Leap Year | |
931 | if (n4Day == 59) | |
932 | { | |
933 | /* Feb. 29 */ | |
934 | tmDest.tm_mon = 2; | |
935 | tmDest.tm_mday = 29; | |
936 | goto DoTime; | |
937 | } | |
938 | ||
939 | // Pretend it's not a leap year for month/day comp. | |
940 | if (n4Day >= 60) | |
941 | --n4Day; | |
942 | } | |
943 | ||
944 | // Make n4DaY a 1-based day of non-leap year and compute | |
945 | // month/day for everything but Feb. 29. | |
946 | ++n4Day; | |
947 | ||
948 | // Month number always >= n/32, so save some loop time */ | |
949 | for (tmDest.tm_mon = (n4Day >> 5) + 1; | |
950 | n4Day > rgMonthDays[tmDest.tm_mon]; tmDest.tm_mon++); | |
951 | ||
952 | tmDest.tm_mday = (int)(n4Day - rgMonthDays[tmDest.tm_mon-1]); | |
953 | ||
954 | DoTime: | |
955 | if (nSecsInDay == 0) | |
956 | tmDest.tm_hour = tmDest.tm_min = tmDest.tm_sec = 0; | |
957 | else | |
958 | { | |
959 | tmDest.tm_sec = (int)nSecsInDay % 60L; | |
960 | nMinutesInDay = nSecsInDay / 60L; | |
961 | tmDest.tm_min = (int)nMinutesInDay % 60; | |
962 | tmDest.tm_hour = (int)nMinutesInDay / 60; | |
963 | } | |
964 | ||
965 | return TRUE; | |
966 | } | |
967 | ||
968 | void TmConvertToStandardFormat(struct tm& tmSrc) | |
969 | { | |
970 | // Convert afx internal tm to format expected by runtimes (_tcsftime, etc) | |
971 | tmSrc.tm_year -= 1900; // year is based on 1900 | |
972 | tmSrc.tm_mon -= 1; // month of year is 0-based | |
973 | tmSrc.tm_wday -= 1; // day of week is 0-based | |
974 | tmSrc.tm_yday -= 1; // day of year is 0-based | |
975 | } | |
976 | ||
977 | double DoubleFromDate(DATE dt) | |
978 | { | |
979 | // No problem if positive | |
980 | if (dt >= 0) | |
981 | return dt; | |
982 | ||
983 | // If negative, must convert since negative dates not continuous | |
984 | // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25) | |
985 | double temp = ceil(dt); | |
986 | return temp - (dt - temp); | |
987 | } | |
988 | ||
989 | DATE DateFromDouble(double dbl) | |
990 | { | |
991 | // No problem if positive | |
992 | if (dbl >= 0) | |
993 | return dbl; | |
994 | ||
995 | // If negative, must convert since negative dates not continuous | |
996 | // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75) | |
997 | double temp = floor(dbl); // dbl is now whole part | |
998 | return temp + (temp - dbl); | |
999 | } | |
1000 | ||
1001 | /* | |
1002 | * ClearVariant | |
1003 | * | |
1004 | * Zeros a variant structure without regard to current contents | |
1005 | */ | |
1006 | static void ClearVariant(VARIANTARG *pvarg) | |
1007 | { | |
1008 | pvarg->vt = VT_EMPTY; | |
1009 | pvarg->wReserved1 = 0; | |
1010 | pvarg->wReserved2 = 0; | |
1011 | pvarg->wReserved3 = 0; | |
1012 | pvarg->lVal = 0; | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * ReleaseVariant | |
1017 | * | |
1018 | * Clears a particular variant structure and releases any external objects | |
1019 | * or memory contained in the variant. Supports the data types listed above. | |
1020 | */ | |
1021 | static void ReleaseVariant(VARIANTARG *pvarg) | |
1022 | { | |
1023 | VARTYPE vt; | |
1024 | VARIANTARG _huge *pvargArray; | |
1025 | long lLBound, lUBound, l; | |
1026 | ||
1027 | vt = pvarg->vt & 0xfff; // mask off flags | |
1028 | ||
1029 | // check if an array. If so, free its contents, then the array itself. | |
1030 | if (V_ISARRAY(pvarg)) | |
1031 | { | |
1032 | // variant arrays are all this routine currently knows about. Since a | |
1033 | // variant can contain anything (even other arrays), call ourselves | |
1034 | // recursively. | |
1035 | if (vt == VT_VARIANT) | |
1036 | { | |
1037 | SafeArrayGetLBound(pvarg->parray, 1, &lLBound); | |
1038 | SafeArrayGetUBound(pvarg->parray, 1, &lUBound); | |
1039 | ||
1040 | if (lUBound > lLBound) | |
1041 | { | |
1042 | lUBound -= lLBound; | |
1043 | ||
1044 | SafeArrayAccessData(pvarg->parray, (void**)&pvargArray); | |
1045 | ||
1046 | for (l = 0; l < lUBound; l++) | |
1047 | { | |
1048 | ReleaseVariant(pvargArray); | |
1049 | pvargArray++; | |
1050 | } | |
1051 | ||
1052 | SafeArrayUnaccessData(pvarg->parray); | |
1053 | } | |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | wxLogWarning("ReleaseVariant: Array contains non-variant type"); | |
1058 | } | |
1059 | ||
1060 | // Free the array itself. | |
1061 | SafeArrayDestroy(pvarg->parray); | |
1062 | } | |
1063 | else | |
1064 | { | |
1065 | switch (vt) | |
1066 | { | |
1067 | case VT_DISPATCH: | |
1068 | if (pvarg->pdispVal) | |
1069 | pvarg->pdispVal->Release(); | |
1070 | break; | |
1071 | ||
1072 | case VT_BSTR: | |
1073 | SysFreeString(pvarg->bstrVal); | |
1074 | break; | |
1075 | ||
1076 | case VT_I2: | |
1077 | case VT_BOOL: | |
1078 | case VT_R8: | |
1079 | case VT_ERROR: // to avoid erroring on an error return from Excel | |
1080 | // no work for these types | |
1081 | break; | |
1082 | ||
1083 | default: | |
1084 | wxLogWarning("ReleaseVariant: Unknown type"); | |
1085 | break; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | ClearVariant(pvarg); | |
1090 | } | |
1091 | ||
1092 | #if 0 | |
1093 | ||
1094 | void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr) | |
1095 | { | |
1096 | TCHAR szBuf[512]; | |
1097 | ||
1098 | switch (GetScode(hr)) | |
1099 | { | |
1100 | case DISP_E_UNKNOWNNAME: | |
1101 | wsprintf(szBuf, L"%s: Unknown name or named argument.", szMember); | |
1102 | break; | |
1103 | ||
1104 | case DISP_E_BADPARAMCOUNT: | |
1105 | wsprintf(szBuf, L"%s: Incorrect number of arguments.", szMember); | |
1106 | break; | |
1107 | ||
1108 | case DISP_E_EXCEPTION: | |
1109 | wsprintf(szBuf, L"%s: Error %d: ", szMember, pexcep->wCode); | |
1110 | if (pexcep->bstrDescription != NULL) | |
1111 | lstrcat(szBuf, pexcep->bstrDescription); | |
1112 | else | |
1113 | lstrcat(szBuf, L"<<No Description>>"); | |
1114 | break; | |
1115 | ||
1116 | case DISP_E_MEMBERNOTFOUND: | |
1117 | wsprintf(szBuf, L"%s: method or property not found.", szMember); | |
1118 | break; | |
1119 | ||
1120 | case DISP_E_OVERFLOW: | |
1121 | wsprintf(szBuf, L"%s: Overflow while coercing argument values.", szMember); | |
1122 | break; | |
1123 | ||
1124 | case DISP_E_NONAMEDARGS: | |
1125 | wsprintf(szBuf, L"%s: Object implementation does not support named arguments.", | |
1126 | szMember); | |
1127 | break; | |
1128 | ||
1129 | case DISP_E_UNKNOWNLCID: | |
1130 | wsprintf(szBuf, L"%s: The locale ID is unknown.", szMember); | |
1131 | break; | |
1132 | ||
1133 | case DISP_E_PARAMNOTOPTIONAL: | |
1134 | wsprintf(szBuf, L"%s: Missing a required parameter.", szMember); | |
1135 | break; | |
1136 | ||
1137 | case DISP_E_PARAMNOTFOUND: | |
1138 | wsprintf(szBuf, L"%s: Argument not found, argument %d.", szMember, uiArgErr); | |
1139 | break; | |
1140 | ||
1141 | case DISP_E_TYPEMISMATCH: | |
1142 | wsprintf(szBuf, L"%s: Type mismatch, argument %d.", szMember, uiArgErr); | |
1143 | break; | |
1144 | ||
1145 | default: | |
1146 | wsprintf(szBuf, L"%s: Unknown error occured.", szMember); | |
1147 | break; | |
1148 | } | |
1149 | ||
1150 | wxLogWarning(szBuf); | |
1151 | } | |
1152 | ||
1153 | #endif | |
1154 |