]>
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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "automtn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
25 // Watcom C++ gives a linker error if this is compiled in.
26 // With Borland C++, all samples crash if this is compiled in.
27 #if wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
29 #define _FORCENAMELESSUNION
31 #include "wx/msw/private.h"
32 #include "wx/msw/ole/oleutils.h"
33 #include "wx/msw/ole/automtn.h"
38 #include "wx/msw/wince/time.h"
55 // Verifies will fail if the needed buffer size is too large
56 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
57 #define MIN_DATE (-657434L) // about year 100
58 #define MAX_DATE 2958465L // about year 9999
60 // Half a second, expressed in days
61 #define HALF_SECOND (1.0/172800.0)
63 // One-based array of days in year at month start
64 static int rgMonthDays
[13] =
65 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
68 #include "wx/datetime.h"
70 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
71 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
72 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
73 #endif // wxUSE_TIMEDATE
75 static void ClearVariant(VARIANTARG
*pvarg
) ;
76 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
77 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
83 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
85 m_dispatchPtr
= dispatchPtr
;
88 wxAutomationObject::~wxAutomationObject()
92 ((IDispatch
*)m_dispatchPtr
)->Release();
97 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
99 // For Put/Get, no named arguments are allowed.
100 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
101 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
106 // nonConstMember is necessary because the wxString class doesn't have enough consts...
107 wxString
nonConstMember(member
);
109 int ch
= nonConstMember
.Find('.');
112 // Use dot notation to get the next object
113 wxString
member2(nonConstMember
.Left((size_t) ch
));
114 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
115 wxAutomationObject obj
;
116 if (!GetObject(obj
, member2
))
118 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
122 ClearVariant(& vReturn
);
124 VARIANTARG
* vReturnPtr
= & vReturn
;
126 // Find number of names args
127 int namedArgCount
= 0;
129 for (i
= 0; i
< noArgs
; i
++)
130 if (!INVOKEARG(i
).GetName().IsNull())
135 int namedArgStringCount
= namedArgCount
+ 1;
136 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
137 argNames
[0] = wxConvertStringToOle(member
);
139 // Note that arguments are specified in reverse order
140 // (all totally logical; hey, we're dealing with OLE here.)
143 for (i
= 0; i
< namedArgCount
; i
++)
145 if (!INVOKEARG(i
).GetName().IsNull())
147 argNames
[(namedArgCount
-j
)] = wxConvertStringToOle(INVOKEARG(i
).GetName());
152 // + 1 for the member name, + 1 again in case we're a 'put'
153 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
156 DISPPARAMS dispparams
;
157 unsigned int uiArgErr
;
160 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
161 // member name as the first name, followed by argument names (if any).
162 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
163 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
166 // ShowException(szMember, hr, NULL, 0);
172 // if doing a property put(ref), we need to adjust the first argument to have a
173 // named arg of DISPID_PROPERTYPUT.
174 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
177 dispIds
[1] = DISPID_PROPERTYPUT
;
178 vReturnPtr
= (VARIANTARG
*) NULL
;
181 // Convert the wxVariants to VARIANTARGs
182 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
183 for (i
= 0; i
< noArgs
; i
++)
185 // Again, reverse args
186 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
195 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
196 dispparams
.rgvarg
= oleArgs
;
197 dispparams
.cArgs
= noArgs
;
198 dispparams
.cNamedArgs
= namedArgCount
;
200 excep
.pfnDeferredFillIn
= NULL
;
202 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
203 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
205 for (i
= 0; i
< namedArgStringCount
; i
++)
207 SysFreeString(argNames
[i
]);
212 for (i
= 0; i
< noArgs
; i
++)
213 ReleaseVariant(& oleArgs
[i
]) ;
218 // display the exception information if appropriate:
219 // ShowException((const char*) member, hr, &excep, uiArgErr);
221 // free exception structure information
222 SysFreeString(excep
.bstrSource
);
223 SysFreeString(excep
.bstrDescription
);
224 SysFreeString(excep
.bstrHelpFile
);
227 ReleaseVariant(vReturnPtr
);
234 // Convert result to wxVariant form
235 wxConvertOleToVariant(vReturn
, retValue
);
236 // Mustn't release the dispatch pointer
237 if (vReturn
.vt
== VT_DISPATCH
)
239 vReturn
.pdispVal
= (IDispatch
*) NULL
;
241 ReleaseVariant(& vReturn
);
247 // Invoke a member function
248 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
250 wxVariant retVariant
;
251 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
253 retVariant
.MakeNull();
258 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
260 wxVariant retVariant
;
261 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
263 retVariant
.MakeNull();
268 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
269 const wxVariant
& arg1
, const wxVariant
& arg2
,
270 const wxVariant
& arg3
, const wxVariant
& arg4
,
271 const wxVariant
& arg5
, const wxVariant
& arg6
)
273 const wxVariant
** args
= new const wxVariant
*[6];
305 wxVariant retVariant
;
306 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
308 retVariant
.MakeNull();
315 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
317 wxVariant retVariant
;
318 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
320 retVariant
.MakeNull();
324 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
326 wxVariant retVariant
;
327 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
329 retVariant
.MakeNull();
334 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
335 const wxVariant
& arg1
, const wxVariant
& arg2
,
336 const wxVariant
& arg3
, const wxVariant
& arg4
,
337 const wxVariant
& arg5
, const wxVariant
& arg6
)
339 const wxVariant
** args
= new const wxVariant
*[6];
371 wxVariant retVariant
;
372 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
374 retVariant
.MakeNull();
380 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
382 wxVariant retVariant
;
383 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
390 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
392 wxVariant retVariant
;
393 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
400 bool wxAutomationObject::PutProperty(const wxString
& property
,
401 const wxVariant
& arg1
, const wxVariant
& arg2
,
402 const wxVariant
& arg3
, const wxVariant
& arg4
,
403 const wxVariant
& arg5
, const wxVariant
& arg6
)
405 const wxVariant
** args
= new const wxVariant
*[6];
437 wxVariant retVariant
;
438 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
444 // Uses DISPATCH_PROPERTYGET
445 // and returns a dispatch pointer. The calling code should call Release
446 // on the pointer, though this could be implicit by constructing an wxAutomationObject
447 // with it and letting the destructor call Release.
448 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
450 wxVariant retVariant
;
451 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
453 if (retVariant
.GetType() == wxT("void*"))
455 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
459 return (WXIDISPATCH
*) NULL
;
462 // Uses DISPATCH_PROPERTYGET
463 // and returns a dispatch pointer. The calling code should call Release
464 // on the pointer, though this could be implicit by constructing an wxAutomationObject
465 // with it and letting the destructor call Release.
466 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
468 wxVariant retVariant
;
469 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
471 if (retVariant
.GetType() == wxT("void*"))
473 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
477 return (WXIDISPATCH
*) NULL
;
481 // A way of initialising another wxAutomationObject with a dispatch object
482 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
484 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
487 obj
.SetDispatchPtr(dispatch
);
494 // A way of initialising another wxAutomationObject with a dispatch object
495 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
497 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
500 obj
.SetDispatchPtr(dispatch
);
507 // Get a dispatch pointer from the current object associated
509 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
515 IUnknown
* pUnk
= NULL
;
517 wxBasicString
unicodeName(classId
.mb_str());
519 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
521 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
525 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
527 wxLogWarning(wxT("Cannot find an active object"));
531 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
533 wxLogWarning(wxT("Cannot find IDispatch interface"));
540 // Get a dispatch pointer from a new object associated
541 // with the given class id
542 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
549 wxBasicString
unicodeName(classId
.mb_str());
551 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
553 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
557 // start a new copy of Excel, grab the IDispatch interface
558 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
560 wxLogWarning(wxT("Cannot start an instance of this class."));
568 bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
570 ClearVariant(&oleVariant
);
571 if (variant
.IsNull())
573 oleVariant
.vt
= VT_NULL
;
577 wxString
type(variant
.GetType());
580 if (type
== wxT("long"))
582 oleVariant
.vt
= VT_I4
;
583 oleVariant
.lVal
= variant
.GetLong() ;
585 // cVal not always present
587 else if (type
== wxT("char"))
589 oleVariant
.vt
=VT_I1
; // Signed Char
590 oleVariant
.cVal
=variant
.GetChar();
593 else if (type
== wxT("double"))
595 oleVariant
.vt
= VT_R8
;
596 oleVariant
.dblVal
= variant
.GetDouble();
598 else if (type
== wxT("bool"))
600 oleVariant
.vt
= VT_BOOL
;
601 // 'bool' required for VC++ 4 apparently
602 #if (defined(__VISUALC__) && (__VISUALC__ <= 1000))
603 oleVariant
.bool = variant
.GetBool();
605 oleVariant
.boolVal
= variant
.GetBool();
608 else if (type
== wxT("string"))
610 wxString
str( variant
.GetString() );
611 oleVariant
.vt
= VT_BSTR
;
612 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
615 else if (type
== wxT("datetime"))
617 wxDateTime
date( variant
.GetDateTime() );
618 oleVariant
.vt
= VT_DATE
;
620 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
621 date
.GetHour(), date
.GetMinute(), date
.GetSecond(), oleVariant
.date
))
625 else if (type
== wxT("void*"))
627 oleVariant
.vt
= VT_DISPATCH
;
628 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
630 else if (type
== wxT("list") || type
== wxT("stringlist"))
632 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
635 SAFEARRAYBOUND saBound
;
636 VARIANTARG
*pvargBase
;
640 int iCount
= variant
.GetCount();
643 saBound
.cElements
= iCount
;
645 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
649 SafeArrayAccessData(psa
, (void**)&pvargBase
);
652 for (i
= 0; i
< iCount
; i
++)
654 // copy each string in the list of strings
655 wxVariant
eachVariant(variant
[i
]);
656 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
658 // memory failure: back out and free strings alloc'ed up to
659 // now, and then the array itself.
661 for (j
= 0; j
< i
; j
++)
663 SysFreeString(pvarg
->bstrVal
);
666 SafeArrayDestroy(psa
);
672 SafeArrayUnaccessData(psa
);
674 oleVariant
.parray
= psa
;
678 oleVariant
.vt
= VT_NULL
;
685 #define VT_TYPEMASK 0xfff
688 bool wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
690 switch (oleVariant
.vt
& VT_TYPEMASK
)
694 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
702 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
705 wxDateTime
date(tmTemp
.tm_yday
, (wxDateTime::Month
) tmTemp
.tm_mon
, tmTemp
.tm_year
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
714 variant
= (long) oleVariant
.lVal
;
719 variant
= (long) oleVariant
.iVal
;
725 #if (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
726 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
727 variant
= (long) (oleVariant
.bool != 0);
729 variant
= (bool) (oleVariant
.bool != 0);
732 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
733 variant
= (long) (oleVariant
.boolVal
!= 0);
735 variant
= (bool) (oleVariant
.boolVal
!= 0);
742 variant
= oleVariant
.dblVal
;
749 int cDims
, cElements
, i
;
752 // Iterate the dimensions: number of elements is x*y*z
753 for (cDims
= 0, cElements
= 1;
754 cDims
< oleVariant
.parray
->cDims
; cDims
++)
755 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
757 // Get a pointer to the data
758 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
762 for (i
= 0; i
< cElements
; i
++)
764 VARIANTARG
& oleElement
= pvdata
[i
];
766 if (!wxConvertOleToVariant(oleElement
, vElement
))
769 variant
.Append(vElement
);
771 SafeArrayUnaccessData(oleVariant
.parray
);
776 variant
= (void*) oleVariant
.pdispVal
;
786 break; // Ignore Empty Variant, used only during destruction of objects
790 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
797 BSTR
wxConvertStringToOle(const wxString
& str
)
800 unsigned int len = strlen((const char*) str);
801 unsigned short* s = new unsigned short[len*2+2];
803 memset(s, 0, len*2+2);
804 for (i=0; i < len; i++)
807 wxBasicString
bstr(str
.mb_str());
811 wxString
wxConvertStringFromOle(BSTR bStr
)
816 int len
= SysStringLen(bStr
) + 1;
817 char *buf
= new char[len
];
818 (void)wcstombs( buf
, bStr
, len
);
825 // ----------------------------------------------------------------------------
827 // ----------------------------------------------------------------------------
829 // ctor takes an ANSI string and transforms it to Unicode
830 wxBasicString::wxBasicString(const char *sz
)
835 // ctor takes an ANSI or Unicode string and transforms it to Unicode
836 wxBasicString::wxBasicString(const wxString
& str
)
839 m_wzBuf
= new OLECHAR
[str
.Length() + 1];
840 memcpy(m_wzBuf
, str
.c_str(), str
.Length()*2);
841 m_wzBuf
[str
.Length()] = L
'\0';
847 // Takes an ANSI string and transforms it to Unicode
848 void wxBasicString::Init(const char *sz
)
850 // get the size of required buffer
851 UINT lenAnsi
= strlen(sz
);
853 UINT lenWide
= lenAnsi
* 2 ;
855 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
859 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
860 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
861 m_wzBuf
[lenWide
] = L
'\0';
869 wxBasicString::~wxBasicString()
874 /////////////////////////////////////////////////////////////////////////////
875 // COleDateTime class HELPERS - implementation
877 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
878 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
880 // Validate year and month (ignore day of week and milliseconds)
881 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
884 // Check for leap year and set the number of days in the month
885 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
886 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
889 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
890 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
892 // Finish validating the date
893 if (wDay
< 1 || wDay
> nDaysInMonth
||
894 wHour
> 23 || wMinute
> 59 ||
900 // Cache the date in days and time in fractional days
904 //It is a valid date; make Jan 1, 1AD be 1
905 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
906 rgMonthDays
[wMonth
-1] + wDay
;
908 // If leap year and it's before March, subtract 1:
909 if (wMonth
<= 2 && bLeapYear
)
912 // Offset so that 12/30/1899 is 0
915 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
916 ((long)wMinute
* 60L) + // mins in seconds
917 ((long)wSecond
)) / 86400.;
919 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
924 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
926 // The legal range does not actually span year 0 to 9999.
927 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
930 long nDaysAbsolute
; // Number of days since 1/1/0
931 long nSecsInDay
; // Time in seconds since midnight
932 long nMinutesInDay
; // Minutes in day
934 long n400Years
; // Number of 400 year increments since 1/1/0
935 long n400Century
; // Century within 400 year block (0,1,2 or 3)
936 long n4Years
; // Number of 4 year increments since 1/1/0
937 long n4Day
; // Day within 4 year block
938 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
939 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
940 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
942 double dblDate
= dtSrc
; // tempory serial date
944 // Round to the second
945 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
947 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
949 dblDate
= fabs(dblDate
);
950 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
952 // Calculate the day of week (sun=1, mon=2...)
953 // -1 because 1/1/0 is Sat. +1 because we want 1-based
954 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
956 // Leap years every 4 yrs except centuries not multiples of 400.
957 n400Years
= (long)(nDaysAbsolute
/ 146097L);
959 // Set nDaysAbsolute to day within 400-year block
960 nDaysAbsolute
%= 146097L;
962 // -1 because first century has extra day
963 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
966 if (n400Century
!= 0)
968 // Set nDaysAbsolute to day within century
969 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
971 // +1 because 1st 4 year increment has 1460 days
972 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
975 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
979 n4Day
= (long)nDaysAbsolute
;
984 // Leap century - not special case!
985 n4Years
= (long)(nDaysAbsolute
/ 1461L);
986 n4Day
= (long)(nDaysAbsolute
% 1461L);
991 // -1 because first year has 366 days
992 n4Yr
= (n4Day
- 1) / 365;
995 n4Day
= (n4Day
- 1) % 365;
1003 // n4Day is now 0-based day of year. Save 1-based day of year, year number
1004 tmDest
.tm_yday
= (int)n4Day
+ 1;
1005 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
1007 // Handle leap year: before, on, and after Feb. 29.
1008 if (n4Yr
== 0 && bLeap4
)
1015 tmDest
.tm_mday
= 29;
1019 // Pretend it's not a leap year for month/day comp.
1024 // Make n4DaY a 1-based day of non-leap year and compute
1025 // month/day for everything but Feb. 29.
1028 // Month number always >= n/32, so save some loop time */
1029 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
1030 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++)
1033 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
1036 if (nSecsInDay
== 0)
1037 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
1040 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
1041 nMinutesInDay
= nSecsInDay
/ 60L;
1042 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
1043 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
1049 // this function is not used
1051 void TmConvertToStandardFormat(struct tm
& tmSrc
)
1053 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
1054 tmSrc
.tm_year
-= 1900; // year is based on 1900
1055 tmSrc
.tm_mon
-= 1; // month of year is 0-based
1056 tmSrc
.tm_wday
-= 1; // day of week is 0-based
1057 tmSrc
.tm_yday
-= 1; // day of year is 0-based
1060 double DoubleFromDate(DATE dt
)
1062 // No problem if positive
1066 // If negative, must convert since negative dates not continuous
1067 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
1068 double temp
= ceil(dt
);
1069 return temp
- (dt
- temp
);
1072 DATE
DateFromDouble(double dbl
)
1074 // No problem if positive
1078 // If negative, must convert since negative dates not continuous
1079 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1080 double temp
= floor(dbl
); // dbl is now whole part
1081 return temp
+ (temp
- dbl
);
1088 * Zeros a variant structure without regard to current contents
1090 static void ClearVariant(VARIANTARG
*pvarg
)
1092 pvarg
->vt
= VT_EMPTY
;
1093 pvarg
->wReserved1
= 0;
1094 pvarg
->wReserved2
= 0;
1095 pvarg
->wReserved3
= 0;
1102 * Clears a particular variant structure and releases any external objects
1103 * or memory contained in the variant. Supports the data types listed above.
1105 static void ReleaseVariant(VARIANTARG
*pvarg
)
1108 VARIANTARG _huge
*pvargArray
;
1109 long lLBound
, lUBound
, l
;
1111 vt
= pvarg
->vt
& 0xfff; // mask off flags
1113 // check if an array. If so, free its contents, then the array itself.
1114 if (V_ISARRAY(pvarg
))
1116 // variant arrays are all this routine currently knows about. Since a
1117 // variant can contain anything (even other arrays), call ourselves
1119 if (vt
== VT_VARIANT
)
1121 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1122 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1124 if (lUBound
> lLBound
)
1128 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1130 for (l
= 0; l
< lUBound
; l
++)
1132 ReleaseVariant(pvargArray
);
1136 SafeArrayUnaccessData(pvarg
->parray
);
1141 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1144 // Free the array itself.
1145 SafeArrayDestroy(pvarg
->parray
);
1152 if (pvarg
->pdispVal
)
1153 pvarg
->pdispVal
->Release();
1157 SysFreeString(pvarg
->bstrVal
);
1163 case VT_ERROR
: // to avoid erroring on an error return from Excel
1164 // no work for these types
1168 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1173 ClearVariant(pvarg
);
1178 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1182 switch (GetScode(hr
))
1184 case DISP_E_UNKNOWNNAME
:
1185 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1188 case DISP_E_BADPARAMCOUNT
:
1189 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1192 case DISP_E_EXCEPTION
:
1193 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1194 if (pexcep
->bstrDescription
!= NULL
)
1195 lstrcat(szBuf
, pexcep
->bstrDescription
);
1197 lstrcat(szBuf
, L
"<<No Description>>");
1200 case DISP_E_MEMBERNOTFOUND
:
1201 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1204 case DISP_E_OVERFLOW
:
1205 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1208 case DISP_E_NONAMEDARGS
:
1209 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1213 case DISP_E_UNKNOWNLCID
:
1214 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1217 case DISP_E_PARAMNOTOPTIONAL
:
1218 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1221 case DISP_E_PARAMNOTFOUND
:
1222 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1225 case DISP_E_TYPEMISMATCH
:
1226 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1230 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1234 wxLogWarning(szBuf
);
1239 #endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)