]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/automtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE automation utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998, Julian Smart
9 // Licence: wxWindows Licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "automtn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
24 #include "wx/msw/ole/automtn.h"
32 // Convert string to Unicode
33 static BSTR
ConvertStringToOle(const wxString
& str
);
35 // Convert string from BSTR to wxString
36 static wxString
ConvertStringFromOle(BSTR bStr
);
38 // Verifies will fail if the needed buffer size is too large
39 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
40 #define MIN_DATE (-657434L) // about year 100
41 #define MAX_DATE 2958465L // about year 9999
43 // Half a second, expressed in days
44 #define HALF_SECOND (1.0/172800.0)
46 // One-based array of days in year at month start
47 static int rgMonthDays
[13] =
48 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
50 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
51 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
52 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
53 static void TmConvertToStandardFormat(struct tm
& tmSrc
);
54 static double DoubleFromDate(DATE dt
);
55 static DATE
DateFromDouble(double dbl
);
57 static void ClearVariant(VARIANTARG
*pvarg
) ;
58 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
59 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
65 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
67 m_dispatchPtr
= dispatchPtr
;
70 wxAutomationObject::~wxAutomationObject()
74 ((IDispatch
*)m_dispatchPtr
)->Release();
79 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
81 // For Put/Get, no named arguments are allowed.
82 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
83 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
88 // nonConstMember is necessary because the wxString class doesn't have enough consts...
89 wxString
nonConstMember(member
);
91 int ch
= nonConstMember
.Find('.');
94 // Use dot notation to get the next object
95 wxString
member2(nonConstMember
.Left((size_t) ch
));
96 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
97 wxAutomationObject obj
;
98 if (!GetObject(obj
, member2
))
100 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
104 ClearVariant(& vReturn
);
106 VARIANTARG
* vReturnPtr
= & vReturn
;
108 // Find number of names args
109 int namedArgCount
= 0;
111 for (i
= 0; i
< noArgs
; i
++)
112 if (!INVOKEARG(i
).GetName().IsNull())
117 int namedArgStringCount
= namedArgCount
+ 1;
118 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
119 argNames
[0] = ConvertStringToOle(member
);
121 // Note that arguments are specified in reverse order
122 // (all totally logical; hey, we're dealing with OLE here.)
125 for (i
= 0; i
< namedArgCount
; i
++)
127 if (!INVOKEARG(i
).GetName().IsNull())
129 argNames
[(namedArgCount
-j
)] = ConvertStringToOle(INVOKEARG(i
).GetName());
134 // + 1 for the member name, + 1 again in case we're a 'put'
135 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
138 DISPPARAMS dispparams
;
139 unsigned int uiArgErr
;
142 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
143 // member name as the first name, followed by argument names (if any).
144 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
145 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
148 // ShowException(szMember, hr, NULL, 0);
152 // if doing a property put(ref), we need to adjust the first argument to have a
153 // named arg of DISPID_PROPERTYPUT.
154 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
157 dispIds
[1] = DISPID_PROPERTYPUT
;
158 vReturnPtr
= (VARIANTARG
*) NULL
;
161 // Convert the wxVariants to VARIANTARGs
162 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
163 for (i
= 0; i
< noArgs
; i
++)
165 // Again, reverse args
166 if (!ConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
167 return FALSE
; // TODO: clean up memory at this point
170 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
171 dispparams
.rgvarg
= oleArgs
;
172 dispparams
.cArgs
= noArgs
;
173 dispparams
.cNamedArgs
= namedArgCount
;
175 excep
.pfnDeferredFillIn
= NULL
;
177 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
178 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
180 for (i
= 0; i
< namedArgStringCount
; i
++)
182 SysFreeString(argNames
[i
]);
187 for (i
= 0; i
< noArgs
; i
++)
188 ReleaseVariant(& oleArgs
[i
]) ;
193 // display the exception information if appropriate:
194 // ShowException((const char*) member, hr, &excep, uiArgErr);
196 // free exception structure information
197 SysFreeString(excep
.bstrSource
);
198 SysFreeString(excep
.bstrDescription
);
199 SysFreeString(excep
.bstrHelpFile
);
202 ReleaseVariant(vReturnPtr
);
209 // Convert result to wxVariant form
210 ConvertOleToVariant(vReturn
, retValue
);
211 // Mustn't release the dispatch pointer
212 if (vReturn
.vt
== VT_DISPATCH
)
214 vReturn
.pdispVal
= (IDispatch
*) NULL
;
216 ReleaseVariant(& vReturn
);
222 // Invoke a member function
223 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
225 wxVariant retVariant
;
226 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
228 retVariant
.MakeNull();
233 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
234 const wxVariant
& arg1
, const wxVariant
& arg2
,
235 const wxVariant
& arg3
, const wxVariant
& arg4
,
236 const wxVariant
& arg5
, const wxVariant
& arg6
)
238 const wxVariant
** args
= new const wxVariant
*[6];
270 wxVariant retVariant
;
271 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
273 retVariant
.MakeNull();
280 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
282 wxVariant retVariant
;
283 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
285 retVariant
.MakeNull();
290 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
291 const wxVariant
& arg1
, const wxVariant
& arg2
,
292 const wxVariant
& arg3
, const wxVariant
& arg4
,
293 const wxVariant
& arg5
, const wxVariant
& arg6
)
295 const wxVariant
** args
= new const wxVariant
*[6];
327 wxVariant retVariant
;
328 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
330 retVariant
.MakeNull();
336 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
338 wxVariant retVariant
;
339 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
346 bool wxAutomationObject::PutProperty(const wxString
& property
,
347 const wxVariant
& arg1
, const wxVariant
& arg2
,
348 const wxVariant
& arg3
, const wxVariant
& arg4
,
349 const wxVariant
& arg5
, const wxVariant
& arg6
)
351 const wxVariant
** args
= new const wxVariant
*[6];
383 wxVariant retVariant
;
384 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
390 // Uses DISPATCH_PROPERTYGET
391 // and returns a dispatch pointer. The calling code should call Release
392 // on the pointer, though this could be implicit by constructing an wxAutomationObject
393 // with it and letting the destructor call Release.
394 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
396 wxVariant retVariant
;
397 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
399 if (retVariant
.GetType() == "void*")
401 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
405 return (WXIDISPATCH
*) NULL
;
409 return (WXIDISPATCH
*) NULL
;
412 // A way of initialising another wxAutomationObject with a dispatch object
413 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
415 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
418 obj
.SetDispatchPtr(dispatch
);
425 // Get a dispatch pointer from the current object associated
427 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
433 IUnknown
* pUnk
= NULL
;
435 BasicString
unicodeName((const char*) classId
);
437 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
439 wxLogWarning("Cannot obtain CLSID from ProgID");
443 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
445 wxLogWarning("Cannot find an active object");
449 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
451 wxLogWarning("Cannot find IDispatch interface");
458 // Get a dispatch pointer from a new object associated
459 // with the given class id
460 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
466 IUnknown
* pUnk
= NULL
;
468 BasicString
unicodeName((const char*) classId
);
470 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
472 wxLogWarning("Cannot obtain CLSID from ProgID");
476 // start a new copy of Excel, grab the IDispatch interface
477 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
479 wxLogWarning("Cannot start an instance of this class.");
487 bool wxAutomationObject::ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
489 ClearVariant(&oleVariant
);
490 if (variant
.IsNull())
492 oleVariant
.vt
= VT_NULL
;
496 wxString
type(variant
.GetType());
500 oleVariant
.vt
= VT_I4
;
501 oleVariant
.lVal
= variant
.GetLong() ;
503 else if (type
== "double")
505 oleVariant
.vt
= VT_R8
;
506 oleVariant
.dblVal
= variant
.GetDouble();
508 else if (type
== "bool")
510 oleVariant
.vt
= VT_BOOL
;
512 oleVariant
.bool = variant
.GetBool();
514 oleVariant
.boolVal
= variant
.GetBool();
517 else if (type
== "string")
519 wxString
str( variant
.GetString() );
520 oleVariant
.vt
= VT_BSTR
;
521 oleVariant
.bstrVal
= ConvertStringToOle(str
);
523 else if (type
== "date")
525 wxDate
date( variant
.GetDate() );
526 oleVariant
.vt
= VT_DATE
;
528 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
529 0, 0, 0, oleVariant
.date
))
532 else if (type
== "time")
534 wxTime
time( variant
.GetTime() );
535 oleVariant
.vt
= VT_DATE
;
537 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
538 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
541 else if (type
== "void*")
543 oleVariant
.vt
= VT_DISPATCH
;
544 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
546 else if (type
== "list" || type
== "stringlist")
548 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
551 SAFEARRAYBOUND saBound
;
552 VARIANTARG
*pvargBase
;
556 int iCount
= variant
.GetCount();
559 saBound
.cElements
= iCount
;
561 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
565 SafeArrayAccessData(psa
, (void**)&pvargBase
);
568 for (i
= 0; i
< iCount
; i
++)
570 // copy each string in the list of strings
571 wxVariant
eachVariant(variant
[i
]);
572 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
574 // memory failure: back out and free strings alloc'ed up to
575 // now, and then the array itself.
577 for (j
= 0; j
< i
; j
++)
579 SysFreeString(pvarg
->bstrVal
);
582 SafeArrayDestroy(psa
);
588 SafeArrayUnaccessData(psa
);
590 oleVariant
.parray
= psa
;
594 oleVariant
.vt
= VT_NULL
;
601 #define VT_TYPEMASK 0xfff
604 bool wxAutomationObject::ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
606 switch (oleVariant
.vt
& VT_TYPEMASK
)
610 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
617 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
620 wxDate
date(tmTemp
.tm_yday
, tmTemp
.tm_mon
, tmTemp
.tm_year
);
621 wxTime
time(date
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
628 variant
= (long) oleVariant
.lVal
;
633 variant
= (long) oleVariant
.iVal
;
640 variant
= (bool) (oleVariant
.bool != 0);
642 variant
= (bool) (oleVariant
.boolVal
!= 0);
648 variant
= oleVariant
.dblVal
;
655 int cDims
, cElements
, i
;
658 // Iterate the dimensions: number of elements is x*y*z
659 for (cDims
= 0, cElements
= 1;
660 cDims
< oleVariant
.parray
->cDims
; cDims
++)
661 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
663 // Get a pointer to the data
664 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
668 for (i
= 0; i
< cElements
; i
++)
670 VARIANTARG
& oleElement
= pvdata
[i
];
672 if (!ConvertOleToVariant(oleElement
, vElement
))
675 variant
.Append(vElement
);
677 SafeArrayUnaccessData(oleVariant
.parray
);
682 variant
= (void*) oleVariant
.pdispVal
;
692 break; // Ignore Empty Variant, used only during destruction of objects
696 wxLogError("wxAutomationObject::ConvertOleToVariant: Unknown variant value type");
703 static BSTR
ConvertStringToOle(const wxString
& str
)
706 unsigned int len = strlen((const char*) str);
707 unsigned short* s = new unsigned short[len*2+2];
709 memset(s, 0, len*2+2);
710 for (i=0; i < len; i++)
713 BasicString
bstr((const char*) str
);
717 static wxString
ConvertStringFromOle(BSTR bStr
)
719 int len
= SysStringLen(bStr
) + 1;
720 char *buf
= new char[len
];
721 int i
= wcstombs( buf
, bStr
, len
);
728 // ----------------------------------------------------------------------------
730 // ----------------------------------------------------------------------------
732 // ctor takes an ANSI string and transforms it to Unicode
733 BasicString::BasicString(const char *sz
)
735 // get the size of required buffer
736 UINT lenAnsi
= strlen(sz
);
737 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
740 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
741 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
742 m_wzBuf
[lenWide
] = L
'\0';
750 BasicString::~BasicString()
755 /////////////////////////////////////////////////////////////////////////////
756 // COleDateTime class HELPERS - implementation
758 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
759 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
761 // Validate year and month (ignore day of week and milliseconds)
762 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
765 // Check for leap year and set the number of days in the month
766 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
767 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
770 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
771 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
773 // Finish validating the date
774 if (wDay
< 1 || wDay
> nDaysInMonth
||
775 wHour
> 23 || wMinute
> 59 ||
781 // Cache the date in days and time in fractional days
785 //It is a valid date; make Jan 1, 1AD be 1
786 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
787 rgMonthDays
[wMonth
-1] + wDay
;
789 // If leap year and it's before March, subtract 1:
790 if (wMonth
<= 2 && bLeapYear
)
793 // Offset so that 12/30/1899 is 0
796 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
797 ((long)wMinute
* 60L) + // mins in seconds
798 ((long)wSecond
)) / 86400.;
800 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
805 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
807 // The legal range does not actually span year 0 to 9999.
808 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
811 long nDays
; // Number of days since Dec. 30, 1899
812 long nDaysAbsolute
; // Number of days since 1/1/0
813 long nSecsInDay
; // Time in seconds since midnight
814 long nMinutesInDay
; // Minutes in day
816 long n400Years
; // Number of 400 year increments since 1/1/0
817 long n400Century
; // Century within 400 year block (0,1,2 or 3)
818 long n4Years
; // Number of 4 year increments since 1/1/0
819 long n4Day
; // Day within 4 year block
820 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
821 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
822 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
824 double dblDate
= dtSrc
; // tempory serial date
826 // If a valid date, then this conversion should not overflow
827 nDays
= (long)dblDate
;
829 // Round to the second
830 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
832 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
834 dblDate
= fabs(dblDate
);
835 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
837 // Calculate the day of week (sun=1, mon=2...)
838 // -1 because 1/1/0 is Sat. +1 because we want 1-based
839 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
841 // Leap years every 4 yrs except centuries not multiples of 400.
842 n400Years
= (long)(nDaysAbsolute
/ 146097L);
844 // Set nDaysAbsolute to day within 400-year block
845 nDaysAbsolute
%= 146097L;
847 // -1 because first century has extra day
848 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
851 if (n400Century
!= 0)
853 // Set nDaysAbsolute to day within century
854 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
856 // +1 because 1st 4 year increment has 1460 days
857 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
860 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
864 n4Day
= (long)nDaysAbsolute
;
869 // Leap century - not special case!
870 n4Years
= (long)(nDaysAbsolute
/ 1461L);
871 n4Day
= (long)(nDaysAbsolute
% 1461L);
876 // -1 because first year has 366 days
877 n4Yr
= (n4Day
- 1) / 365;
880 n4Day
= (n4Day
- 1) % 365;
888 // n4Day is now 0-based day of year. Save 1-based day of year, year number
889 tmDest
.tm_yday
= (int)n4Day
+ 1;
890 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
892 // Handle leap year: before, on, and after Feb. 29.
893 if (n4Yr
== 0 && bLeap4
)
904 // Pretend it's not a leap year for month/day comp.
909 // Make n4DaY a 1-based day of non-leap year and compute
910 // month/day for everything but Feb. 29.
913 // Month number always >= n/32, so save some loop time */
914 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
915 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
917 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
921 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
924 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
925 nMinutesInDay
= nSecsInDay
/ 60L;
926 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
927 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
933 void TmConvertToStandardFormat(struct tm
& tmSrc
)
935 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
936 tmSrc
.tm_year
-= 1900; // year is based on 1900
937 tmSrc
.tm_mon
-= 1; // month of year is 0-based
938 tmSrc
.tm_wday
-= 1; // day of week is 0-based
939 tmSrc
.tm_yday
-= 1; // day of year is 0-based
942 double DoubleFromDate(DATE dt
)
944 // No problem if positive
948 // If negative, must convert since negative dates not continuous
949 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
950 double temp
= ceil(dt
);
951 return temp
- (dt
- temp
);
954 DATE
DateFromDouble(double dbl
)
956 // No problem if positive
960 // If negative, must convert since negative dates not continuous
961 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
962 double temp
= floor(dbl
); // dbl is now whole part
963 return temp
+ (temp
- dbl
);
969 * Zeros a variant structure without regard to current contents
971 static void ClearVariant(VARIANTARG
*pvarg
)
973 pvarg
->vt
= VT_EMPTY
;
974 pvarg
->wReserved1
= 0;
975 pvarg
->wReserved2
= 0;
976 pvarg
->wReserved3
= 0;
983 * Clears a particular variant structure and releases any external objects
984 * or memory contained in the variant. Supports the data types listed above.
986 static void ReleaseVariant(VARIANTARG
*pvarg
)
989 VARIANTARG _huge
*pvargArray
;
990 long lLBound
, lUBound
, l
;
992 vt
= pvarg
->vt
& 0xfff; // mask off flags
994 // check if an array. If so, free its contents, then the array itself.
995 if (V_ISARRAY(pvarg
))
997 // variant arrays are all this routine currently knows about. Since a
998 // variant can contain anything (even other arrays), call ourselves
1000 if (vt
== VT_VARIANT
)
1002 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1003 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1005 if (lUBound
> lLBound
)
1009 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1011 for (l
= 0; l
< lUBound
; l
++)
1013 ReleaseVariant(pvargArray
);
1017 SafeArrayUnaccessData(pvarg
->parray
);
1022 wxLogWarning("ReleaseVariant: Array contains non-variant type");
1025 // Free the array itself.
1026 SafeArrayDestroy(pvarg
->parray
);
1033 if (pvarg
->pdispVal
)
1034 pvarg
->pdispVal
->Release();
1038 SysFreeString(pvarg
->bstrVal
);
1044 case VT_ERROR
: // to avoid erroring on an error return from Excel
1045 // no work for these types
1049 wxLogWarning("ReleaseVariant: Unknown type");
1054 ClearVariant(pvarg
);
1059 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1063 switch (GetScode(hr
))
1065 case DISP_E_UNKNOWNNAME
:
1066 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1069 case DISP_E_BADPARAMCOUNT
:
1070 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1073 case DISP_E_EXCEPTION
:
1074 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1075 if (pexcep
->bstrDescription
!= NULL
)
1076 lstrcat(szBuf
, pexcep
->bstrDescription
);
1078 lstrcat(szBuf
, L
"<<No Description>>");
1081 case DISP_E_MEMBERNOTFOUND
:
1082 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1085 case DISP_E_OVERFLOW
:
1086 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1089 case DISP_E_NONAMEDARGS
:
1090 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1094 case DISP_E_UNKNOWNLCID
:
1095 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1098 case DISP_E_PARAMNOTOPTIONAL
:
1099 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1102 case DISP_E_PARAMNOTFOUND
:
1103 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1106 case DISP_E_TYPEMISMATCH
:
1107 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1111 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1115 wxLogWarning(szBuf
);