]>
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"
36 // wrapper around BSTR type (by Vadim Zeitlin)
38 class WXDLLEXPORT BasicString
42 BasicString(const char *sz
);
46 // just get the string
47 operator BSTR() const { return m_wzBuf
; }
48 // retrieve a copy of our string - caller must SysFreeString() it later!
49 BSTR
Get() const { return SysAllocString(m_wzBuf
); }
52 // @@@ not implemented (but should be)
53 BasicString(const BasicString
&);
54 BasicString
& operator=(const BasicString
&);
56 OLECHAR
*m_wzBuf
; // actual string
60 static bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
) ;
61 static bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
) ;
63 // Convert string to Unicode
64 static BSTR
ConvertStringToOle(const wxString
& str
);
66 // Convert string from BSTR to wxString
67 static wxString
ConvertStringFromOle(BSTR bStr
);
69 // Verifies will fail if the needed buffer size is too large
70 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
71 #define MIN_DATE (-657434L) // about year 100
72 #define MAX_DATE 2958465L // about year 9999
74 // Half a second, expressed in days
75 #define HALF_SECOND (1.0/172800.0)
77 // One-based array of days in year at month start
78 static int rgMonthDays
[13] =
79 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
81 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
82 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
83 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
84 static void TmConvertToStandardFormat(struct tm
& tmSrc
);
85 static double DoubleFromDate(DATE dt
);
86 static DATE
DateFromDouble(double dbl
);
88 static void ClearVariant(VARIANTARG
*pvarg
) ;
89 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
90 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
96 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
98 m_dispatchPtr
= dispatchPtr
;
101 wxAutomationObject::~wxAutomationObject()
105 ((IDispatch
*)m_dispatchPtr
)->Release();
106 m_dispatchPtr
= NULL
;
110 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
112 // For Put/Get, no named arguments are allowed.
113 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
114 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
119 // nonConstMember is necessary because the wxString class doesn't have enough consts...
120 wxString
nonConstMember(member
);
122 int ch
= nonConstMember
.Find('.');
125 // Use dot notation to get the next object
126 wxString
member2(nonConstMember
.Left((size_t) ch
));
127 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
128 wxAutomationObject obj
;
129 if (!GetObject(obj
, member2
))
131 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
135 ClearVariant(& vReturn
);
137 VARIANTARG
* vReturnPtr
= & vReturn
;
139 // Find number of names args
140 int namedArgCount
= 0;
142 for (i
= 0; i
< noArgs
; i
++)
143 if (!INVOKEARG(i
).GetName().IsNull())
148 int namedArgStringCount
= namedArgCount
+ 1;
149 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
150 argNames
[0] = ConvertStringToOle(member
);
152 // Note that arguments are specified in reverse order
153 // (all totally logical; hey, we're dealing with OLE here.)
156 for (i
= 0; i
< namedArgCount
; i
++)
158 if (!INVOKEARG(i
).GetName().IsNull())
160 argNames
[(namedArgCount
-j
)] = ConvertStringToOle(INVOKEARG(i
).GetName());
165 // + 1 for the member name, + 1 again in case we're a 'put'
166 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
169 DISPPARAMS dispparams
;
170 unsigned int uiArgErr
;
173 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
174 // member name as the first name, followed by argument names (if any).
175 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
176 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
179 // ShowException(szMember, hr, NULL, 0);
183 // if doing a property put(ref), we need to adjust the first argument to have a
184 // named arg of DISPID_PROPERTYPUT.
185 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
188 dispIds
[1] = DISPID_PROPERTYPUT
;
189 vReturnPtr
= (VARIANTARG
*) NULL
;
192 // Convert the wxVariants to VARIANTARGs
193 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
194 for (i
= 0; i
< noArgs
; i
++)
196 // Again, reverse args
197 if (!ConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
198 return FALSE
; // TODO: clean up memory at this point
201 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
202 dispparams
.rgvarg
= oleArgs
;
203 dispparams
.cArgs
= noArgs
;
204 dispparams
.cNamedArgs
= namedArgCount
;
206 excep
.pfnDeferredFillIn
= NULL
;
208 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
209 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
211 for (i
= 0; i
< namedArgStringCount
; i
++)
213 SysFreeString(argNames
[i
]);
218 for (i
= 0; i
< noArgs
; i
++)
219 ReleaseVariant(& oleArgs
[i
]) ;
224 // display the exception information if appropriate:
225 // ShowException((const char*) member, hr, &excep, uiArgErr);
227 // free exception structure information
228 SysFreeString(excep
.bstrSource
);
229 SysFreeString(excep
.bstrDescription
);
230 SysFreeString(excep
.bstrHelpFile
);
233 ReleaseVariant(vReturnPtr
);
240 // Convert result to wxVariant form
241 ConvertOleToVariant(vReturn
, retValue
);
242 // Mustn't release the dispatch pointer
243 if (vReturn
.vt
== VT_DISPATCH
)
245 vReturn
.pdispVal
= (IDispatch
*) NULL
;
247 ReleaseVariant(& vReturn
);
253 // Invoke a member function
254 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
256 wxVariant retVariant
;
257 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
259 retVariant
.MakeNull();
264 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
265 const wxVariant
& arg1
, const wxVariant
& arg2
,
266 const wxVariant
& arg3
, const wxVariant
& arg4
,
267 const wxVariant
& arg5
, const wxVariant
& arg6
)
269 const wxVariant
** args
= new const wxVariant
*[6];
301 wxVariant retVariant
;
302 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
304 retVariant
.MakeNull();
311 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
313 wxVariant retVariant
;
314 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
316 retVariant
.MakeNull();
321 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
322 const wxVariant
& arg1
, const wxVariant
& arg2
,
323 const wxVariant
& arg3
, const wxVariant
& arg4
,
324 const wxVariant
& arg5
, const wxVariant
& arg6
)
326 const wxVariant
** args
= new const wxVariant
*[6];
358 wxVariant retVariant
;
359 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
361 retVariant
.MakeNull();
367 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
369 wxVariant retVariant
;
370 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
377 bool wxAutomationObject::PutProperty(const wxString
& property
,
378 const wxVariant
& arg1
, const wxVariant
& arg2
,
379 const wxVariant
& arg3
, const wxVariant
& arg4
,
380 const wxVariant
& arg5
, const wxVariant
& arg6
)
382 const wxVariant
** args
= new const wxVariant
*[6];
414 wxVariant retVariant
;
415 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
421 // Uses DISPATCH_PROPERTYGET
422 // and returns a dispatch pointer. The calling code should call Release
423 // on the pointer, though this could be implicit by constructing an wxAutomationObject
424 // with it and letting the destructor call Release.
425 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
427 wxVariant retVariant
;
428 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
430 if (retVariant
.GetType() == "void*")
432 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
436 return (WXIDISPATCH
*) NULL
;
440 return (WXIDISPATCH
*) NULL
;
443 // A way of initialising another wxAutomationObject with a dispatch object
444 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
446 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
449 obj
.SetDispatchPtr(dispatch
);
456 // Get a dispatch pointer from the current object associated
458 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
464 IUnknown
* pUnk
= NULL
;
466 BasicString
unicodeName((const char*) classId
);
468 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
470 wxLogWarning("Cannot obtain CLSID from ProgID");
474 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
476 wxLogWarning("Cannot find an active object");
480 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
482 wxLogWarning("Cannot find IDispatch interface");
489 // Get a dispatch pointer from a new object associated
490 // with the given class id
491 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
497 IUnknown
* pUnk
= NULL
;
499 BasicString
unicodeName((const char*) classId
);
501 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
503 wxLogWarning("Cannot obtain CLSID from ProgID");
507 // start a new copy of Excel, grab the IDispatch interface
508 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
510 wxLogWarning("Cannot start an instance of this class.");
518 bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
520 ClearVariant(&oleVariant
);
521 if (variant
.IsNull())
523 oleVariant
.vt
= VT_NULL
;
527 wxString
type(variant
.GetType());
531 oleVariant
.vt
= VT_I4
;
532 oleVariant
.lVal
= variant
.GetLong() ;
534 else if (type
== "double")
536 oleVariant
.vt
= VT_R8
;
537 oleVariant
.dblVal
= variant
.GetDouble();
539 else if (type
== "bool")
541 oleVariant
.vt
= VT_BOOL
;
543 oleVariant
.bool = variant
.GetBool();
545 oleVariant
.boolVal
= variant
.GetBool();
548 else if (type
== "string")
550 wxString
str( variant
.GetString() );
551 oleVariant
.vt
= VT_BSTR
;
552 oleVariant
.bstrVal
= ConvertStringToOle(str
);
554 else if (type
== "date")
556 wxDate
date( variant
.GetDate() );
557 oleVariant
.vt
= VT_DATE
;
559 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
560 0, 0, 0, oleVariant
.date
))
563 else if (type
== "time")
565 wxTime
time( variant
.GetTime() );
566 oleVariant
.vt
= VT_DATE
;
568 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
569 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
572 else if (type
== "void*")
574 oleVariant
.vt
= VT_DISPATCH
;
575 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
577 else if (type
== "list" || type
== "stringlist")
579 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
582 SAFEARRAYBOUND saBound
;
583 VARIANTARG
*pvargBase
;
587 int iCount
= variant
.GetCount();
590 saBound
.cElements
= iCount
;
592 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
596 SafeArrayAccessData(psa
, (void**)&pvargBase
);
599 for (i
= 0; i
< iCount
; i
++)
601 // copy each string in the list of strings
602 wxVariant
eachVariant(variant
[i
]);
603 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
605 // memory failure: back out and free strings alloc'ed up to
606 // now, and then the array itself.
608 for (j
= 0; j
< i
; j
++)
610 SysFreeString(pvarg
->bstrVal
);
613 SafeArrayDestroy(psa
);
619 SafeArrayUnaccessData(psa
);
621 oleVariant
.parray
= psa
;
625 oleVariant
.vt
= VT_NULL
;
632 #define VT_TYPEMASK 0xfff
635 bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
637 switch (oleVariant
.vt
& VT_TYPEMASK
)
641 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
648 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
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
);
659 variant
= (long) oleVariant
.lVal
;
664 variant
= (long) oleVariant
.iVal
;
671 variant
= (bool) (oleVariant
.bool != 0);
673 variant
= (bool) (oleVariant
.boolVal
!= 0);
679 variant
= oleVariant
.dblVal
;
686 int cDims
, cElements
, i
;
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
;
694 // Get a pointer to the data
695 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
699 for (i
= 0; i
< cElements
; i
++)
701 VARIANTARG
& oleElement
= pvdata
[i
];
703 if (!ConvertOleToVariant(oleElement
, vElement
))
706 variant
.Append(vElement
);
708 SafeArrayUnaccessData(oleVariant
.parray
);
713 variant
= (void*) oleVariant
.pdispVal
;
723 break; // Ignore Empty Variant, used only during destruction of objects
727 wxLogError("wxAutomationObject::ConvertOleToVariant: Unknown variant value type");
734 static BSTR
ConvertStringToOle(const wxString
& str
)
737 unsigned int len = strlen((const char*) str);
738 unsigned short* s = new unsigned short[len*2+2];
740 memset(s, 0, len*2+2);
741 for (i=0; i < len; i++)
744 BasicString
bstr((const char*) str
);
748 static wxString
ConvertStringFromOle(BSTR bStr
)
750 int len
= SysStringLen(bStr
) + 1;
751 char *buf
= new char[len
];
752 int i
= wcstombs( buf
, bStr
, len
);
759 // ----------------------------------------------------------------------------
761 // ----------------------------------------------------------------------------
763 // ctor takes an ANSI string and transforms it to Unicode
764 BasicString::BasicString(const char *sz
)
766 // get the size of required buffer
767 UINT lenAnsi
= strlen(sz
);
768 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
771 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
772 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
773 m_wzBuf
[lenWide
] = L
'\0';
781 BasicString::~BasicString()
786 /////////////////////////////////////////////////////////////////////////////
787 // COleDateTime class HELPERS - implementation
789 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
790 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
792 // Validate year and month (ignore day of week and milliseconds)
793 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
796 // Check for leap year and set the number of days in the month
797 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
798 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
801 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
802 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
804 // Finish validating the date
805 if (wDay
< 1 || wDay
> nDaysInMonth
||
806 wHour
> 23 || wMinute
> 59 ||
812 // Cache the date in days and time in fractional days
816 //It is a valid date; make Jan 1, 1AD be 1
817 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
818 rgMonthDays
[wMonth
-1] + wDay
;
820 // If leap year and it's before March, subtract 1:
821 if (wMonth
<= 2 && bLeapYear
)
824 // Offset so that 12/30/1899 is 0
827 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
828 ((long)wMinute
* 60L) + // mins in seconds
829 ((long)wSecond
)) / 86400.;
831 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
836 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
838 // The legal range does not actually span year 0 to 9999.
839 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
842 long nDays
; // Number of days since Dec. 30, 1899
843 long nDaysAbsolute
; // Number of days since 1/1/0
844 long nSecsInDay
; // Time in seconds since midnight
845 long nMinutesInDay
; // Minutes in day
847 long n400Years
; // Number of 400 year increments since 1/1/0
848 long n400Century
; // Century within 400 year block (0,1,2 or 3)
849 long n4Years
; // Number of 4 year increments since 1/1/0
850 long n4Day
; // Day within 4 year block
851 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
852 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
853 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
855 double dblDate
= dtSrc
; // tempory serial date
857 // If a valid date, then this conversion should not overflow
858 nDays
= (long)dblDate
;
860 // Round to the second
861 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
863 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
865 dblDate
= fabs(dblDate
);
866 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
868 // Calculate the day of week (sun=1, mon=2...)
869 // -1 because 1/1/0 is Sat. +1 because we want 1-based
870 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
872 // Leap years every 4 yrs except centuries not multiples of 400.
873 n400Years
= (long)(nDaysAbsolute
/ 146097L);
875 // Set nDaysAbsolute to day within 400-year block
876 nDaysAbsolute
%= 146097L;
878 // -1 because first century has extra day
879 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
882 if (n400Century
!= 0)
884 // Set nDaysAbsolute to day within century
885 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
887 // +1 because 1st 4 year increment has 1460 days
888 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
891 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
895 n4Day
= (long)nDaysAbsolute
;
900 // Leap century - not special case!
901 n4Years
= (long)(nDaysAbsolute
/ 1461L);
902 n4Day
= (long)(nDaysAbsolute
% 1461L);
907 // -1 because first year has 366 days
908 n4Yr
= (n4Day
- 1) / 365;
911 n4Day
= (n4Day
- 1) % 365;
919 // n4Day is now 0-based day of year. Save 1-based day of year, year number
920 tmDest
.tm_yday
= (int)n4Day
+ 1;
921 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
923 // Handle leap year: before, on, and after Feb. 29.
924 if (n4Yr
== 0 && bLeap4
)
935 // Pretend it's not a leap year for month/day comp.
940 // Make n4DaY a 1-based day of non-leap year and compute
941 // month/day for everything but Feb. 29.
944 // Month number always >= n/32, so save some loop time */
945 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
946 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
948 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
952 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
955 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
956 nMinutesInDay
= nSecsInDay
/ 60L;
957 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
958 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
964 void TmConvertToStandardFormat(struct tm
& tmSrc
)
966 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
967 tmSrc
.tm_year
-= 1900; // year is based on 1900
968 tmSrc
.tm_mon
-= 1; // month of year is 0-based
969 tmSrc
.tm_wday
-= 1; // day of week is 0-based
970 tmSrc
.tm_yday
-= 1; // day of year is 0-based
973 double DoubleFromDate(DATE dt
)
975 // No problem if positive
979 // If negative, must convert since negative dates not continuous
980 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
981 double temp
= ceil(dt
);
982 return temp
- (dt
- temp
);
985 DATE
DateFromDouble(double dbl
)
987 // No problem if positive
991 // If negative, must convert since negative dates not continuous
992 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
993 double temp
= floor(dbl
); // dbl is now whole part
994 return temp
+ (temp
- dbl
);
1000 * Zeros a variant structure without regard to current contents
1002 static void ClearVariant(VARIANTARG
*pvarg
)
1004 pvarg
->vt
= VT_EMPTY
;
1005 pvarg
->wReserved1
= 0;
1006 pvarg
->wReserved2
= 0;
1007 pvarg
->wReserved3
= 0;
1014 * Clears a particular variant structure and releases any external objects
1015 * or memory contained in the variant. Supports the data types listed above.
1017 static void ReleaseVariant(VARIANTARG
*pvarg
)
1020 VARIANTARG _huge
*pvargArray
;
1021 long lLBound
, lUBound
, l
;
1023 vt
= pvarg
->vt
& 0xfff; // mask off flags
1025 // check if an array. If so, free its contents, then the array itself.
1026 if (V_ISARRAY(pvarg
))
1028 // variant arrays are all this routine currently knows about. Since a
1029 // variant can contain anything (even other arrays), call ourselves
1031 if (vt
== VT_VARIANT
)
1033 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1034 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1036 if (lUBound
> lLBound
)
1040 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1042 for (l
= 0; l
< lUBound
; l
++)
1044 ReleaseVariant(pvargArray
);
1048 SafeArrayUnaccessData(pvarg
->parray
);
1053 wxLogWarning("ReleaseVariant: Array contains non-variant type");
1056 // Free the array itself.
1057 SafeArrayDestroy(pvarg
->parray
);
1064 if (pvarg
->pdispVal
)
1065 pvarg
->pdispVal
->Release();
1069 SysFreeString(pvarg
->bstrVal
);
1075 case VT_ERROR
: // to avoid erroring on an error return from Excel
1076 // no work for these types
1080 wxLogWarning("ReleaseVariant: Unknown type");
1085 ClearVariant(pvarg
);
1090 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1094 switch (GetScode(hr
))
1096 case DISP_E_UNKNOWNNAME
:
1097 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1100 case DISP_E_BADPARAMCOUNT
:
1101 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1104 case DISP_E_EXCEPTION
:
1105 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1106 if (pexcep
->bstrDescription
!= NULL
)
1107 lstrcat(szBuf
, pexcep
->bstrDescription
);
1109 lstrcat(szBuf
, L
"<<No Description>>");
1112 case DISP_E_MEMBERNOTFOUND
:
1113 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1116 case DISP_E_OVERFLOW
:
1117 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1120 case DISP_E_NONAMEDARGS
:
1121 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1125 case DISP_E_UNKNOWNLCID
:
1126 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1129 case DISP_E_PARAMNOTOPTIONAL
:
1130 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1133 case DISP_E_PARAMNOTFOUND
:
1134 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1137 case DISP_E_TYPEMISMATCH
:
1138 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1142 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1146 wxLogWarning(szBuf
);