]>
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 wxVariant
& theVariant
= INVOKEARG((noArgs
-1) - i
);
167 if (!ConvertVariantToOle(theVariant
, oleArgs
[i
]))
168 return FALSE
; // TODO: clean up memory at this point
171 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
172 dispparams
.rgvarg
= oleArgs
;
173 dispparams
.cArgs
= noArgs
;
174 dispparams
.cNamedArgs
= namedArgCount
;
176 excep
.pfnDeferredFillIn
= NULL
;
178 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
179 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
181 for (i
= 0; i
< namedArgStringCount
; i
++)
183 SysFreeString(argNames
[i
]);
188 for (i
= 0; i
< noArgs
; i
++)
189 ReleaseVariant(& oleArgs
[i
]) ;
194 // display the exception information if appropriate:
195 // ShowException((const char*) member, hr, &excep, uiArgErr);
197 // free exception structure information
198 SysFreeString(excep
.bstrSource
);
199 SysFreeString(excep
.bstrDescription
);
200 SysFreeString(excep
.bstrHelpFile
);
203 ReleaseVariant(vReturnPtr
);
210 // Convert result to wxVariant form
211 ConvertOleToVariant(vReturn
, retValue
);
212 // Mustn't release the dispatch pointer
213 if (vReturn
.vt
== VT_DISPATCH
)
215 vReturn
.pdispVal
= (IDispatch
*) NULL
;
217 ReleaseVariant(& vReturn
);
223 // Invoke a member function
224 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
226 wxVariant retVariant
;
227 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
229 retVariant
.MakeNull();
234 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
235 const wxVariant
& arg1
, const wxVariant
& arg2
,
236 const wxVariant
& arg3
, const wxVariant
& arg4
,
237 const wxVariant
& arg5
, const wxVariant
& arg6
)
239 const wxVariant
** args
= new const wxVariant
*[6];
271 wxVariant retVariant
;
272 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
274 retVariant
.MakeNull();
281 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
283 wxVariant retVariant
;
284 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
286 retVariant
.MakeNull();
291 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
292 const wxVariant
& arg1
, const wxVariant
& arg2
,
293 const wxVariant
& arg3
, const wxVariant
& arg4
,
294 const wxVariant
& arg5
, const wxVariant
& arg6
)
296 const wxVariant
** args
= new const wxVariant
*[6];
328 wxVariant retVariant
;
329 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
331 retVariant
.MakeNull();
337 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
339 wxVariant retVariant
;
340 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
347 bool wxAutomationObject::PutProperty(const wxString
& property
,
348 const wxVariant
& arg1
, const wxVariant
& arg2
,
349 const wxVariant
& arg3
, const wxVariant
& arg4
,
350 const wxVariant
& arg5
, const wxVariant
& arg6
)
352 const wxVariant
** args
= new const wxVariant
*[6];
384 wxVariant retVariant
;
385 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
391 // Uses DISPATCH_PROPERTYGET
392 // and returns a dispatch pointer. The calling code should call Release
393 // on the pointer, though this could be implicit by constructing an wxAutomationObject
394 // with it and letting the destructor call Release.
395 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
397 wxVariant retVariant
;
398 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
400 if (retVariant
.GetType() == "void*")
402 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
406 return (WXIDISPATCH
*) NULL
;
410 return (WXIDISPATCH
*) NULL
;
413 // A way of initialising another wxAutomationObject with a dispatch object
414 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
416 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
419 obj
.SetDispatchPtr(dispatch
);
426 // Get a dispatch pointer from the current object associated
428 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
434 IUnknown
* pUnk
= NULL
;
436 BasicString
unicodeName((const char*) classId
);
438 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
440 wxLogWarning("Cannot obtain CLSID from ProgID");
444 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
446 wxLogWarning("Cannot find an active object");
450 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
452 wxLogWarning("Cannot find IDispatch interface");
459 // Get a dispatch pointer from a new object associated
460 // with the given class id
461 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
467 IUnknown
* pUnk
= NULL
;
469 BasicString
unicodeName((const char*) classId
);
471 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
473 wxLogWarning("Cannot obtain CLSID from ProgID");
477 // start a new copy of Excel, grab the IDispatch interface
478 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
480 wxLogWarning("Cannot start an instance of this class.");
488 bool wxAutomationObject::ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
490 ClearVariant(&oleVariant
);
491 if (variant
.IsNull())
493 oleVariant
.vt
= VT_NULL
;
497 wxString
type(variant
.GetType());
501 oleVariant
.vt
= VT_I4
;
502 oleVariant
.lVal
= variant
.GetLong() ;
504 else if (type
== "double")
506 oleVariant
.vt
= VT_R8
;
507 oleVariant
.dblVal
= variant
.GetDouble();
509 else if (type
== "bool")
511 oleVariant
.vt
= VT_BOOL
;
512 oleVariant
.boolVal
= variant
.GetBool();
514 else if (type
== "string")
516 wxString
str( variant
.GetString() );
517 oleVariant
.vt
= VT_BSTR
;
518 oleVariant
.bstrVal
= ConvertStringToOle(str
);
520 else if (type
== "date")
522 wxDate
date( variant
.GetDate() );
523 oleVariant
.vt
= VT_DATE
;
525 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
526 0, 0, 0, oleVariant
.date
))
529 else if (type
== "time")
531 wxTime
time( variant
.GetTime() );
532 oleVariant
.vt
= VT_DATE
;
534 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
535 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
538 else if (type
== "void*")
540 oleVariant
.vt
= VT_DISPATCH
;
541 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
543 else if (type
== "list" || type
== "stringlist")
545 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
548 SAFEARRAYBOUND saBound
;
549 VARIANTARG
*pvargBase
;
553 int iCount
= variant
.GetCount();
556 saBound
.cElements
= iCount
;
558 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
562 SafeArrayAccessData(psa
, (void**)&pvargBase
);
565 for (i
= 0; i
< iCount
; i
++)
567 // copy each string in the list of strings
568 wxVariant
eachVariant(variant
[i
]);
569 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
571 // memory failure: back out and free strings alloc'ed up to
572 // now, and then the array itself.
574 for (j
= 0; j
< i
; j
++)
576 SysFreeString(pvarg
->bstrVal
);
579 SafeArrayDestroy(psa
);
585 SafeArrayUnaccessData(psa
);
587 oleVariant
.parray
= psa
;
591 oleVariant
.vt
= VT_NULL
;
598 #define VT_TYPEMASK 0xfff
601 bool wxAutomationObject::ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
603 switch (oleVariant
.vt
& VT_TYPEMASK
)
607 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
614 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
617 wxDate
date(tmTemp
.tm_yday
, tmTemp
.tm_mon
, tmTemp
.tm_year
);
618 wxTime
time(date
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
625 variant
= (long) oleVariant
.lVal
;
630 variant
= (long) oleVariant
.iVal
;
636 variant
= (bool) (oleVariant
.boolVal
!= 0);
641 variant
= oleVariant
.dblVal
;
648 int cDims
, cElements
, i
;
651 // Iterate the dimensions: number of elements is x*y*z
652 for (cDims
= 0, cElements
= 1;
653 cDims
< oleVariant
.parray
->cDims
; cDims
++)
654 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
656 // Get a pointer to the data
657 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
661 for (i
= 0; i
< cElements
; i
++)
663 VARIANTARG
& oleElement
= pvdata
[i
];
665 if (!ConvertOleToVariant(oleElement
, vElement
))
668 variant
.Append(vElement
);
670 SafeArrayUnaccessData(oleVariant
.parray
);
675 variant
= (void*) oleVariant
.pdispVal
;
685 break; // Ignore Empty Variant, used only during destruction of objects
689 wxLogError("wxAutomationObject::ConvertOleToVariant: Unknown variant value type");
696 static BSTR
ConvertStringToOle(const wxString
& str
)
699 unsigned int len = strlen((const char*) str);
700 unsigned short* s = new unsigned short[len*2+2];
702 memset(s, 0, len*2+2);
703 for (i=0; i < len; i++)
706 BasicString
bstr((const char*) str
);
710 static wxString
ConvertStringFromOle(BSTR bStr
)
712 int len
= SysStringLen(bStr
) + 1;
713 char *buf
= new char[len
];
714 int i
= wcstombs( buf
, bStr
, len
);
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
725 // ctor takes an ANSI string and transforms it to Unicode
726 BasicString::BasicString(const char *sz
)
728 // get the size of required buffer
729 UINT lenAnsi
= strlen(sz
);
730 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
733 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
734 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
735 m_wzBuf
[lenWide
] = L
'\0';
743 BasicString::~BasicString()
748 /////////////////////////////////////////////////////////////////////////////
749 // COleDateTime class HELPERS - implementation
751 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
752 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
754 // Validate year and month (ignore day of week and milliseconds)
755 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
758 // Check for leap year and set the number of days in the month
759 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
760 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
763 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
764 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
766 // Finish validating the date
767 if (wDay
< 1 || wDay
> nDaysInMonth
||
768 wHour
> 23 || wMinute
> 59 ||
774 // Cache the date in days and time in fractional days
778 //It is a valid date; make Jan 1, 1AD be 1
779 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
780 rgMonthDays
[wMonth
-1] + wDay
;
782 // If leap year and it's before March, subtract 1:
783 if (wMonth
<= 2 && bLeapYear
)
786 // Offset so that 12/30/1899 is 0
789 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
790 ((long)wMinute
* 60L) + // mins in seconds
791 ((long)wSecond
)) / 86400.;
793 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
798 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
800 // The legal range does not actually span year 0 to 9999.
801 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
804 long nDays
; // Number of days since Dec. 30, 1899
805 long nDaysAbsolute
; // Number of days since 1/1/0
806 long nSecsInDay
; // Time in seconds since midnight
807 long nMinutesInDay
; // Minutes in day
809 long n400Years
; // Number of 400 year increments since 1/1/0
810 long n400Century
; // Century within 400 year block (0,1,2 or 3)
811 long n4Years
; // Number of 4 year increments since 1/1/0
812 long n4Day
; // Day within 4 year block
813 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
814 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
815 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
817 double dblDate
= dtSrc
; // tempory serial date
819 // If a valid date, then this conversion should not overflow
820 nDays
= (long)dblDate
;
822 // Round to the second
823 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
825 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
827 dblDate
= fabs(dblDate
);
828 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
830 // Calculate the day of week (sun=1, mon=2...)
831 // -1 because 1/1/0 is Sat. +1 because we want 1-based
832 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
834 // Leap years every 4 yrs except centuries not multiples of 400.
835 n400Years
= (long)(nDaysAbsolute
/ 146097L);
837 // Set nDaysAbsolute to day within 400-year block
838 nDaysAbsolute
%= 146097L;
840 // -1 because first century has extra day
841 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
844 if (n400Century
!= 0)
846 // Set nDaysAbsolute to day within century
847 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
849 // +1 because 1st 4 year increment has 1460 days
850 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
853 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
857 n4Day
= (long)nDaysAbsolute
;
862 // Leap century - not special case!
863 n4Years
= (long)(nDaysAbsolute
/ 1461L);
864 n4Day
= (long)(nDaysAbsolute
% 1461L);
869 // -1 because first year has 366 days
870 n4Yr
= (n4Day
- 1) / 365;
873 n4Day
= (n4Day
- 1) % 365;
881 // n4Day is now 0-based day of year. Save 1-based day of year, year number
882 tmDest
.tm_yday
= (int)n4Day
+ 1;
883 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
885 // Handle leap year: before, on, and after Feb. 29.
886 if (n4Yr
== 0 && bLeap4
)
897 // Pretend it's not a leap year for month/day comp.
902 // Make n4DaY a 1-based day of non-leap year and compute
903 // month/day for everything but Feb. 29.
906 // Month number always >= n/32, so save some loop time */
907 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
908 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
910 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
914 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
917 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
918 nMinutesInDay
= nSecsInDay
/ 60L;
919 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
920 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
926 void TmConvertToStandardFormat(struct tm
& tmSrc
)
928 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
929 tmSrc
.tm_year
-= 1900; // year is based on 1900
930 tmSrc
.tm_mon
-= 1; // month of year is 0-based
931 tmSrc
.tm_wday
-= 1; // day of week is 0-based
932 tmSrc
.tm_yday
-= 1; // day of year is 0-based
935 double DoubleFromDate(DATE dt
)
937 // No problem if positive
941 // If negative, must convert since negative dates not continuous
942 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
943 double temp
= ceil(dt
);
944 return temp
- (dt
- temp
);
947 DATE
DateFromDouble(double dbl
)
949 // No problem if positive
953 // If negative, must convert since negative dates not continuous
954 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
955 double temp
= floor(dbl
); // dbl is now whole part
956 return temp
+ (temp
- dbl
);
962 * Zeros a variant structure without regard to current contents
964 static void ClearVariant(VARIANTARG
*pvarg
)
966 pvarg
->vt
= VT_EMPTY
;
967 pvarg
->wReserved1
= 0;
968 pvarg
->wReserved2
= 0;
969 pvarg
->wReserved3
= 0;
976 * Clears a particular variant structure and releases any external objects
977 * or memory contained in the variant. Supports the data types listed above.
979 static void ReleaseVariant(VARIANTARG
*pvarg
)
982 VARIANTARG _huge
*pvargArray
;
983 long lLBound
, lUBound
, l
;
985 vt
= pvarg
->vt
& 0xfff; // mask off flags
987 // check if an array. If so, free its contents, then the array itself.
988 if (V_ISARRAY(pvarg
))
990 // variant arrays are all this routine currently knows about. Since a
991 // variant can contain anything (even other arrays), call ourselves
993 if (vt
== VT_VARIANT
)
995 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
996 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
998 if (lUBound
> lLBound
)
1002 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1004 for (l
= 0; l
< lUBound
; l
++)
1006 ReleaseVariant(pvargArray
);
1010 SafeArrayUnaccessData(pvarg
->parray
);
1015 wxLogWarning("ReleaseVariant: Array contains non-variant type");
1018 // Free the array itself.
1019 SafeArrayDestroy(pvarg
->parray
);
1026 if (pvarg
->pdispVal
)
1027 pvarg
->pdispVal
->Release();
1031 SysFreeString(pvarg
->bstrVal
);
1037 case VT_ERROR
: // to avoid erroring on an error return from Excel
1038 // no work for these types
1042 wxLogWarning("ReleaseVariant: Unknown type");
1047 ClearVariant(pvarg
);
1052 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1056 switch (GetScode(hr
))
1058 case DISP_E_UNKNOWNNAME
:
1059 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1062 case DISP_E_BADPARAMCOUNT
:
1063 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1066 case DISP_E_EXCEPTION
:
1067 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1068 if (pexcep
->bstrDescription
!= NULL
)
1069 lstrcat(szBuf
, pexcep
->bstrDescription
);
1071 lstrcat(szBuf
, L
"<<No Description>>");
1074 case DISP_E_MEMBERNOTFOUND
:
1075 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1078 case DISP_E_OVERFLOW
:
1079 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1082 case DISP_E_NONAMEDARGS
:
1083 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1087 case DISP_E_UNKNOWNLCID
:
1088 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1091 case DISP_E_PARAMNOTOPTIONAL
:
1092 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1095 case DISP_E_PARAMNOTFOUND
:
1096 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1099 case DISP_E_TYPEMISMATCH
:
1100 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1104 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1108 wxLogWarning(szBuf
);