]>
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 wxLogError("wxAutomationObject::ConvertOleToVariant: Unknown variant value type");
692 static BSTR
ConvertStringToOle(const wxString
& str
)
695 unsigned int len = strlen((const char*) str);
696 unsigned short* s = new unsigned short[len*2+2];
698 memset(s, 0, len*2+2);
699 for (i=0; i < len; i++)
702 BasicString
bstr((const char*) str
);
706 static wxString
ConvertStringFromOle(BSTR bStr
)
708 int len
= SysStringLen(bStr
) + 1;
709 char *buf
= new char[len
];
710 int i
= wcstombs( buf
, bStr
, len
);
717 // ----------------------------------------------------------------------------
719 // ----------------------------------------------------------------------------
721 // ctor takes an ANSI string and transforms it to Unicode
722 BasicString::BasicString(const char *sz
)
724 // get the size of required buffer
725 UINT lenAnsi
= strlen(sz
);
726 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
729 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
730 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
731 m_wzBuf
[lenWide
] = L
'\0';
739 BasicString::~BasicString()
744 /////////////////////////////////////////////////////////////////////////////
745 // COleDateTime class HELPERS - implementation
747 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
748 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
750 // Validate year and month (ignore day of week and milliseconds)
751 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
754 // Check for leap year and set the number of days in the month
755 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
756 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
759 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
760 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
762 // Finish validating the date
763 if (wDay
< 1 || wDay
> nDaysInMonth
||
764 wHour
> 23 || wMinute
> 59 ||
770 // Cache the date in days and time in fractional days
774 //It is a valid date; make Jan 1, 1AD be 1
775 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
776 rgMonthDays
[wMonth
-1] + wDay
;
778 // If leap year and it's before March, subtract 1:
779 if (wMonth
<= 2 && bLeapYear
)
782 // Offset so that 12/30/1899 is 0
785 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
786 ((long)wMinute
* 60L) + // mins in seconds
787 ((long)wSecond
)) / 86400.;
789 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
794 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
796 // The legal range does not actually span year 0 to 9999.
797 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
800 long nDays
; // Number of days since Dec. 30, 1899
801 long nDaysAbsolute
; // Number of days since 1/1/0
802 long nSecsInDay
; // Time in seconds since midnight
803 long nMinutesInDay
; // Minutes in day
805 long n400Years
; // Number of 400 year increments since 1/1/0
806 long n400Century
; // Century within 400 year block (0,1,2 or 3)
807 long n4Years
; // Number of 4 year increments since 1/1/0
808 long n4Day
; // Day within 4 year block
809 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
810 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
811 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
813 double dblDate
= dtSrc
; // tempory serial date
815 // If a valid date, then this conversion should not overflow
816 nDays
= (long)dblDate
;
818 // Round to the second
819 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
821 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
823 dblDate
= fabs(dblDate
);
824 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
826 // Calculate the day of week (sun=1, mon=2...)
827 // -1 because 1/1/0 is Sat. +1 because we want 1-based
828 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
830 // Leap years every 4 yrs except centuries not multiples of 400.
831 n400Years
= (long)(nDaysAbsolute
/ 146097L);
833 // Set nDaysAbsolute to day within 400-year block
834 nDaysAbsolute
%= 146097L;
836 // -1 because first century has extra day
837 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
840 if (n400Century
!= 0)
842 // Set nDaysAbsolute to day within century
843 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
845 // +1 because 1st 4 year increment has 1460 days
846 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
849 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
853 n4Day
= (long)nDaysAbsolute
;
858 // Leap century - not special case!
859 n4Years
= (long)(nDaysAbsolute
/ 1461L);
860 n4Day
= (long)(nDaysAbsolute
% 1461L);
865 // -1 because first year has 366 days
866 n4Yr
= (n4Day
- 1) / 365;
869 n4Day
= (n4Day
- 1) % 365;
877 // n4Day is now 0-based day of year. Save 1-based day of year, year number
878 tmDest
.tm_yday
= (int)n4Day
+ 1;
879 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
881 // Handle leap year: before, on, and after Feb. 29.
882 if (n4Yr
== 0 && bLeap4
)
893 // Pretend it's not a leap year for month/day comp.
898 // Make n4DaY a 1-based day of non-leap year and compute
899 // month/day for everything but Feb. 29.
902 // Month number always >= n/32, so save some loop time */
903 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
904 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
906 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
910 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
913 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
914 nMinutesInDay
= nSecsInDay
/ 60L;
915 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
916 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
922 void TmConvertToStandardFormat(struct tm
& tmSrc
)
924 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
925 tmSrc
.tm_year
-= 1900; // year is based on 1900
926 tmSrc
.tm_mon
-= 1; // month of year is 0-based
927 tmSrc
.tm_wday
-= 1; // day of week is 0-based
928 tmSrc
.tm_yday
-= 1; // day of year is 0-based
931 double DoubleFromDate(DATE dt
)
933 // No problem if positive
937 // If negative, must convert since negative dates not continuous
938 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
939 double temp
= ceil(dt
);
940 return temp
- (dt
- temp
);
943 DATE
DateFromDouble(double dbl
)
945 // No problem if positive
949 // If negative, must convert since negative dates not continuous
950 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
951 double temp
= floor(dbl
); // dbl is now whole part
952 return temp
+ (temp
- dbl
);
958 * Zeros a variant structure without regard to current contents
960 static void ClearVariant(VARIANTARG
*pvarg
)
962 pvarg
->vt
= VT_EMPTY
;
963 pvarg
->wReserved1
= 0;
964 pvarg
->wReserved2
= 0;
965 pvarg
->wReserved3
= 0;
972 * Clears a particular variant structure and releases any external objects
973 * or memory contained in the variant. Supports the data types listed above.
975 static void ReleaseVariant(VARIANTARG
*pvarg
)
978 VARIANTARG _huge
*pvargArray
;
979 long lLBound
, lUBound
, l
;
981 vt
= pvarg
->vt
& 0xfff; // mask off flags
983 // check if an array. If so, free its contents, then the array itself.
984 if (V_ISARRAY(pvarg
))
986 // variant arrays are all this routine currently knows about. Since a
987 // variant can contain anything (even other arrays), call ourselves
989 if (vt
== VT_VARIANT
)
991 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
992 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
994 if (lUBound
> lLBound
)
998 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1000 for (l
= 0; l
< lUBound
; l
++)
1002 ReleaseVariant(pvargArray
);
1006 SafeArrayUnaccessData(pvarg
->parray
);
1011 wxLogWarning("ReleaseVariant: Array contains non-variant type");
1014 // Free the array itself.
1015 SafeArrayDestroy(pvarg
->parray
);
1022 if (pvarg
->pdispVal
)
1023 pvarg
->pdispVal
->Release();
1027 SysFreeString(pvarg
->bstrVal
);
1033 case VT_ERROR
: // to avoid erroring on an error return from Excel
1034 // no work for these types
1038 wxLogWarning("ReleaseVariant: Unknown type");
1043 ClearVariant(pvarg
);
1048 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1052 switch (GetScode(hr
))
1054 case DISP_E_UNKNOWNNAME
:
1055 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1058 case DISP_E_BADPARAMCOUNT
:
1059 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1062 case DISP_E_EXCEPTION
:
1063 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1064 if (pexcep
->bstrDescription
!= NULL
)
1065 lstrcat(szBuf
, pexcep
->bstrDescription
);
1067 lstrcat(szBuf
, L
"<<No Description>>");
1070 case DISP_E_MEMBERNOTFOUND
:
1071 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1074 case DISP_E_OVERFLOW
:
1075 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1078 case DISP_E_NONAMEDARGS
:
1079 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1083 case DISP_E_UNKNOWNLCID
:
1084 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1087 case DISP_E_PARAMNOTOPTIONAL
:
1088 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1091 case DISP_E_PARAMNOTFOUND
:
1092 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1095 case DISP_E_TYPEMISMATCH
:
1096 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1100 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1104 wxLogWarning(szBuf
);