]>
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"
37 #include "wx/msw/wince/time.h"
54 // Verifies will fail if the needed buffer size is too large
55 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
56 #define MIN_DATE (-657434L) // about year 100
57 #define MAX_DATE 2958465L // about year 9999
59 // Half a second, expressed in days
60 #define HALF_SECOND (1.0/172800.0)
62 // One-based array of days in year at month start
63 static int rgMonthDays
[13] =
64 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
67 #include "wx/datetime.h"
69 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
70 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
71 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
72 #endif // wxUSE_TIMEDATE
74 static void ClearVariant(VARIANTARG
*pvarg
) ;
75 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
76 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
82 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
84 m_dispatchPtr
= dispatchPtr
;
87 wxAutomationObject::~wxAutomationObject()
91 ((IDispatch
*)m_dispatchPtr
)->Release();
96 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
98 // For Put/Get, no named arguments are allowed.
99 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
100 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
105 // nonConstMember is necessary because the wxString class doesn't have enough consts...
106 wxString
nonConstMember(member
);
108 int ch
= nonConstMember
.Find('.');
111 // Use dot notation to get the next object
112 wxString
member2(nonConstMember
.Left((size_t) ch
));
113 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
114 wxAutomationObject obj
;
115 if (!GetObject(obj
, member2
))
117 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
121 ClearVariant(& vReturn
);
123 VARIANTARG
* vReturnPtr
= & vReturn
;
125 // Find number of names args
126 int namedArgCount
= 0;
128 for (i
= 0; i
< noArgs
; i
++)
129 if (!INVOKEARG(i
).GetName().IsNull())
134 int namedArgStringCount
= namedArgCount
+ 1;
135 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
136 argNames
[0] = wxConvertStringToOle(member
);
138 // Note that arguments are specified in reverse order
139 // (all totally logical; hey, we're dealing with OLE here.)
142 for (i
= 0; i
< namedArgCount
; i
++)
144 if (!INVOKEARG(i
).GetName().IsNull())
146 argNames
[(namedArgCount
-j
)] = wxConvertStringToOle(INVOKEARG(i
).GetName());
151 // + 1 for the member name, + 1 again in case we're a 'put'
152 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
155 DISPPARAMS dispparams
;
156 unsigned int uiArgErr
;
159 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
160 // member name as the first name, followed by argument names (if any).
161 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
162 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
165 // ShowException(szMember, hr, NULL, 0);
171 // if doing a property put(ref), we need to adjust the first argument to have a
172 // named arg of DISPID_PROPERTYPUT.
173 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
176 dispIds
[1] = DISPID_PROPERTYPUT
;
177 vReturnPtr
= (VARIANTARG
*) NULL
;
180 // Convert the wxVariants to VARIANTARGs
181 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
182 for (i
= 0; i
< noArgs
; i
++)
184 // Again, reverse args
185 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
194 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
195 dispparams
.rgvarg
= oleArgs
;
196 dispparams
.cArgs
= noArgs
;
197 dispparams
.cNamedArgs
= namedArgCount
;
199 excep
.pfnDeferredFillIn
= NULL
;
201 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
202 (WORD
)action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
204 for (i
= 0; i
< namedArgStringCount
; i
++)
206 SysFreeString(argNames
[i
]);
211 for (i
= 0; i
< noArgs
; i
++)
212 ReleaseVariant(& oleArgs
[i
]) ;
217 // display the exception information if appropriate:
218 // ShowException((const char*) member, hr, &excep, uiArgErr);
220 // free exception structure information
221 SysFreeString(excep
.bstrSource
);
222 SysFreeString(excep
.bstrDescription
);
223 SysFreeString(excep
.bstrHelpFile
);
226 ReleaseVariant(vReturnPtr
);
233 // Convert result to wxVariant form
234 wxConvertOleToVariant(vReturn
, retValue
);
235 // Mustn't release the dispatch pointer
236 if (vReturn
.vt
== VT_DISPATCH
)
238 vReturn
.pdispVal
= (IDispatch
*) NULL
;
240 ReleaseVariant(& vReturn
);
246 // Invoke a member function
247 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
249 wxVariant retVariant
;
250 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
252 retVariant
.MakeNull();
257 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
259 wxVariant retVariant
;
260 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
262 retVariant
.MakeNull();
267 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
268 const wxVariant
& arg1
, const wxVariant
& arg2
,
269 const wxVariant
& arg3
, const wxVariant
& arg4
,
270 const wxVariant
& arg5
, const wxVariant
& arg6
)
272 const wxVariant
** args
= new const wxVariant
*[6];
304 wxVariant retVariant
;
305 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
307 retVariant
.MakeNull();
314 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
316 wxVariant retVariant
;
317 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
319 retVariant
.MakeNull();
323 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
325 wxVariant retVariant
;
326 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
328 retVariant
.MakeNull();
333 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
334 const wxVariant
& arg1
, const wxVariant
& arg2
,
335 const wxVariant
& arg3
, const wxVariant
& arg4
,
336 const wxVariant
& arg5
, const wxVariant
& arg6
)
338 const wxVariant
** args
= new const wxVariant
*[6];
370 wxVariant retVariant
;
371 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
373 retVariant
.MakeNull();
379 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
381 wxVariant retVariant
;
382 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
389 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
391 wxVariant retVariant
;
392 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
399 bool wxAutomationObject::PutProperty(const wxString
& property
,
400 const wxVariant
& arg1
, const wxVariant
& arg2
,
401 const wxVariant
& arg3
, const wxVariant
& arg4
,
402 const wxVariant
& arg5
, const wxVariant
& arg6
)
404 const wxVariant
** args
= new const wxVariant
*[6];
436 wxVariant retVariant
;
437 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
443 // Uses DISPATCH_PROPERTYGET
444 // and returns a dispatch pointer. The calling code should call Release
445 // on the pointer, though this could be implicit by constructing an wxAutomationObject
446 // with it and letting the destructor call Release.
447 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
449 wxVariant retVariant
;
450 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
452 if (retVariant
.GetType() == wxT("void*"))
454 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
458 return (WXIDISPATCH
*) NULL
;
461 // Uses DISPATCH_PROPERTYGET
462 // and returns a dispatch pointer. The calling code should call Release
463 // on the pointer, though this could be implicit by constructing an wxAutomationObject
464 // with it and letting the destructor call Release.
465 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
467 wxVariant retVariant
;
468 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
470 if (retVariant
.GetType() == wxT("void*"))
472 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
476 return (WXIDISPATCH
*) NULL
;
480 // A way of initialising another wxAutomationObject with a dispatch object
481 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
483 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
486 obj
.SetDispatchPtr(dispatch
);
493 // A way of initialising another wxAutomationObject with a dispatch object
494 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
496 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
499 obj
.SetDispatchPtr(dispatch
);
506 // Get a dispatch pointer from the current object associated
508 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
514 IUnknown
* pUnk
= NULL
;
516 wxBasicString
unicodeName(classId
.mb_str());
518 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
520 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
524 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
526 wxLogWarning(wxT("Cannot find an active object"));
530 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
532 wxLogWarning(wxT("Cannot find IDispatch interface"));
539 // Get a dispatch pointer from a new object associated
540 // with the given class id
541 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
548 wxBasicString
unicodeName(classId
.mb_str());
550 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
552 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
556 // start a new copy of Excel, grab the IDispatch interface
557 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
559 wxLogWarning(wxT("Cannot start an instance of this class."));
567 bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
569 ClearVariant(&oleVariant
);
570 if (variant
.IsNull())
572 oleVariant
.vt
= VT_NULL
;
576 wxString
type(variant
.GetType());
579 if (type
== wxT("long"))
581 oleVariant
.vt
= VT_I4
;
582 oleVariant
.lVal
= variant
.GetLong() ;
584 // cVal not always present
586 else if (type
== wxT("char"))
588 oleVariant
.vt
=VT_I1
; // Signed Char
589 oleVariant
.cVal
=variant
.GetChar();
592 else if (type
== wxT("double"))
594 oleVariant
.vt
= VT_R8
;
595 oleVariant
.dblVal
= variant
.GetDouble();
597 else if (type
== wxT("bool"))
599 oleVariant
.vt
= VT_BOOL
;
600 // 'bool' required for VC++ 4 apparently
601 #if (defined(__VISUALC__) && (__VISUALC__ <= 1000))
602 oleVariant
.bool = variant
.GetBool();
604 oleVariant
.boolVal
= variant
.GetBool();
607 else if (type
== wxT("string"))
609 wxString
str( variant
.GetString() );
610 oleVariant
.vt
= VT_BSTR
;
611 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
614 else if (type
== wxT("datetime"))
616 wxDateTime
date( variant
.GetDateTime() );
617 oleVariant
.vt
= VT_DATE
;
619 if (!OleDateFromTm((WORD
)date
.GetYear(), date
.GetMonth(), date
.GetDay(),
620 date
.GetHour(), date
.GetMinute(), date
.GetSecond(), oleVariant
.date
))
624 else if (type
== wxT("void*"))
626 oleVariant
.vt
= VT_DISPATCH
;
627 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
629 else if (type
== wxT("list") || type
== wxT("stringlist"))
631 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
634 SAFEARRAYBOUND saBound
;
635 VARIANTARG
*pvargBase
;
639 int iCount
= variant
.GetCount();
642 saBound
.cElements
= iCount
;
644 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
648 SafeArrayAccessData(psa
, (void**)&pvargBase
);
651 for (i
= 0; i
< iCount
; i
++)
653 // copy each string in the list of strings
654 wxVariant
eachVariant(variant
[i
]);
655 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
657 // memory failure: back out and free strings alloc'ed up to
658 // now, and then the array itself.
660 for (j
= 0; j
< i
; j
++)
662 SysFreeString(pvarg
->bstrVal
);
665 SafeArrayDestroy(psa
);
671 SafeArrayUnaccessData(psa
);
673 oleVariant
.parray
= psa
;
677 oleVariant
.vt
= VT_NULL
;
684 #define VT_TYPEMASK 0xfff
687 bool wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
689 switch (oleVariant
.vt
& VT_TYPEMASK
)
693 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
701 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
704 wxDateTime
date((wxDateTime::wxDateTime_t
) tmTemp
.tm_yday
,
705 (wxDateTime::Month
) tmTemp
.tm_mon
,
707 (wxDateTime::wxDateTime_t
) tmTemp
.tm_hour
,
708 (wxDateTime::wxDateTime_t
) tmTemp
.tm_min
,
709 (wxDateTime::wxDateTime_t
) tmTemp
.tm_sec
);
718 variant
= (long) oleVariant
.lVal
;
723 variant
= (long) oleVariant
.iVal
;
729 #if (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
730 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
731 variant
= (long) (oleVariant
.bool != 0);
733 variant
= (bool) (oleVariant
.bool != 0);
736 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
737 variant
= (long) (oleVariant
.boolVal
!= 0);
739 variant
= (bool) (oleVariant
.boolVal
!= 0);
746 variant
= oleVariant
.dblVal
;
753 int cDims
, cElements
, i
;
756 // Iterate the dimensions: number of elements is x*y*z
757 for (cDims
= 0, cElements
= 1;
758 cDims
< oleVariant
.parray
->cDims
; cDims
++)
759 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
761 // Get a pointer to the data
762 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
766 for (i
= 0; i
< cElements
; i
++)
768 VARIANTARG
& oleElement
= pvdata
[i
];
770 if (!wxConvertOleToVariant(oleElement
, vElement
))
773 variant
.Append(vElement
);
775 SafeArrayUnaccessData(oleVariant
.parray
);
780 variant
= (void*) oleVariant
.pdispVal
;
790 break; // Ignore Empty Variant, used only during destruction of objects
794 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
801 BSTR
wxConvertStringToOle(const wxString
& str
)
804 unsigned int len = strlen((const char*) str);
805 unsigned short* s = new unsigned short[len*2+2];
807 memset(s, 0, len*2+2);
808 for (i=0; i < len; i++)
811 wxBasicString
bstr(str
.mb_str());
815 wxString
wxConvertStringFromOle(BSTR bStr
)
820 int len
= SysStringLen(bStr
) + 1;
821 char *buf
= new char[len
];
822 (void)wcstombs( buf
, bStr
, len
);
829 // ----------------------------------------------------------------------------
831 // ----------------------------------------------------------------------------
833 // ctor takes an ANSI string and transforms it to Unicode
834 wxBasicString::wxBasicString(const char *sz
)
839 // ctor takes an ANSI or Unicode string and transforms it to Unicode
840 wxBasicString::wxBasicString(const wxString
& str
)
843 m_wzBuf
= new OLECHAR
[str
.Length() + 1];
844 memcpy(m_wzBuf
, str
.c_str(), str
.Length()*2);
845 m_wzBuf
[str
.Length()] = L
'\0';
851 // Takes an ANSI string and transforms it to Unicode
852 void wxBasicString::Init(const char *sz
)
854 // get the size of required buffer
855 UINT lenAnsi
= strlen(sz
);
857 UINT lenWide
= lenAnsi
* 2 ;
859 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
863 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
864 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
865 m_wzBuf
[lenWide
] = L
'\0';
873 wxBasicString::~wxBasicString()
878 /////////////////////////////////////////////////////////////////////////////
879 // COleDateTime class HELPERS - implementation
881 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
882 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
884 // Validate year and month (ignore day of week and milliseconds)
885 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
888 // Check for leap year and set the number of days in the month
889 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
890 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
893 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
894 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
896 // Finish validating the date
897 if (wDay
< 1 || wDay
> nDaysInMonth
||
898 wHour
> 23 || wMinute
> 59 ||
904 // Cache the date in days and time in fractional days
908 //It is a valid date; make Jan 1, 1AD be 1
909 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
910 rgMonthDays
[wMonth
-1] + wDay
;
912 // If leap year and it's before March, subtract 1:
913 if (wMonth
<= 2 && bLeapYear
)
916 // Offset so that 12/30/1899 is 0
919 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
920 ((long)wMinute
* 60L) + // mins in seconds
921 ((long)wSecond
)) / 86400.;
923 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
928 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
930 // The legal range does not actually span year 0 to 9999.
931 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
934 long nDaysAbsolute
; // Number of days since 1/1/0
935 long nSecsInDay
; // Time in seconds since midnight
936 long nMinutesInDay
; // Minutes in day
938 long n400Years
; // Number of 400 year increments since 1/1/0
939 long n400Century
; // Century within 400 year block (0,1,2 or 3)
940 long n4Years
; // Number of 4 year increments since 1/1/0
941 long n4Day
; // Day within 4 year block
942 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
943 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
944 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
946 double dblDate
= dtSrc
; // tempory serial date
948 // Round to the second
949 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
951 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
953 dblDate
= fabs(dblDate
);
954 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
956 // Calculate the day of week (sun=1, mon=2...)
957 // -1 because 1/1/0 is Sat. +1 because we want 1-based
958 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
960 // Leap years every 4 yrs except centuries not multiples of 400.
961 n400Years
= (long)(nDaysAbsolute
/ 146097L);
963 // Set nDaysAbsolute to day within 400-year block
964 nDaysAbsolute
%= 146097L;
966 // -1 because first century has extra day
967 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
970 if (n400Century
!= 0)
972 // Set nDaysAbsolute to day within century
973 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
975 // +1 because 1st 4 year increment has 1460 days
976 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
979 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
983 n4Day
= (long)nDaysAbsolute
;
988 // Leap century - not special case!
989 n4Years
= (long)(nDaysAbsolute
/ 1461L);
990 n4Day
= (long)(nDaysAbsolute
% 1461L);
995 // -1 because first year has 366 days
996 n4Yr
= (n4Day
- 1) / 365;
999 n4Day
= (n4Day
- 1) % 365;
1007 // n4Day is now 0-based day of year. Save 1-based day of year, year number
1008 tmDest
.tm_yday
= (int)n4Day
+ 1;
1009 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
1011 // Handle leap year: before, on, and after Feb. 29.
1012 if (n4Yr
== 0 && bLeap4
)
1019 tmDest
.tm_mday
= 29;
1023 // Pretend it's not a leap year for month/day comp.
1028 // Make n4DaY a 1-based day of non-leap year and compute
1029 // month/day for everything but Feb. 29.
1032 // Month number always >= n/32, so save some loop time */
1033 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
1034 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++)
1037 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
1040 if (nSecsInDay
== 0)
1041 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
1044 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
1045 nMinutesInDay
= nSecsInDay
/ 60L;
1046 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
1047 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
1053 // this function is not used
1055 void TmConvertToStandardFormat(struct tm
& tmSrc
)
1057 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
1058 tmSrc
.tm_year
-= 1900; // year is based on 1900
1059 tmSrc
.tm_mon
-= 1; // month of year is 0-based
1060 tmSrc
.tm_wday
-= 1; // day of week is 0-based
1061 tmSrc
.tm_yday
-= 1; // day of year is 0-based
1064 double DoubleFromDate(DATE dt
)
1066 // No problem if positive
1070 // If negative, must convert since negative dates not continuous
1071 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
1072 double temp
= ceil(dt
);
1073 return temp
- (dt
- temp
);
1076 DATE
DateFromDouble(double dbl
)
1078 // No problem if positive
1082 // If negative, must convert since negative dates not continuous
1083 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1084 double temp
= floor(dbl
); // dbl is now whole part
1085 return temp
+ (temp
- dbl
);
1092 * Zeros a variant structure without regard to current contents
1094 static void ClearVariant(VARIANTARG
*pvarg
)
1096 pvarg
->vt
= VT_EMPTY
;
1097 pvarg
->wReserved1
= 0;
1098 pvarg
->wReserved2
= 0;
1099 pvarg
->wReserved3
= 0;
1106 * Clears a particular variant structure and releases any external objects
1107 * or memory contained in the variant. Supports the data types listed above.
1109 static void ReleaseVariant(VARIANTARG
*pvarg
)
1112 VARIANTARG _huge
*pvargArray
;
1113 long lLBound
, lUBound
, l
;
1115 vt
= (VARTYPE
)(pvarg
->vt
& 0xfff); // mask off flags
1117 // check if an array. If so, free its contents, then the array itself.
1118 if (V_ISARRAY(pvarg
))
1120 // variant arrays are all this routine currently knows about. Since a
1121 // variant can contain anything (even other arrays), call ourselves
1123 if (vt
== VT_VARIANT
)
1125 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1126 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1128 if (lUBound
> lLBound
)
1132 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1134 for (l
= 0; l
< lUBound
; l
++)
1136 ReleaseVariant(pvargArray
);
1140 SafeArrayUnaccessData(pvarg
->parray
);
1145 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1148 // Free the array itself.
1149 SafeArrayDestroy(pvarg
->parray
);
1156 if (pvarg
->pdispVal
)
1157 pvarg
->pdispVal
->Release();
1161 SysFreeString(pvarg
->bstrVal
);
1167 case VT_ERROR
: // to avoid erroring on an error return from Excel
1168 // no work for these types
1172 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1177 ClearVariant(pvarg
);
1182 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1186 switch (GetScode(hr
))
1188 case DISP_E_UNKNOWNNAME
:
1189 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1192 case DISP_E_BADPARAMCOUNT
:
1193 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1196 case DISP_E_EXCEPTION
:
1197 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1198 if (pexcep
->bstrDescription
!= NULL
)
1199 lstrcat(szBuf
, pexcep
->bstrDescription
);
1201 lstrcat(szBuf
, L
"<<No Description>>");
1204 case DISP_E_MEMBERNOTFOUND
:
1205 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1208 case DISP_E_OVERFLOW
:
1209 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1212 case DISP_E_NONAMEDARGS
:
1213 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1217 case DISP_E_UNKNOWNLCID
:
1218 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1221 case DISP_E_PARAMNOTOPTIONAL
:
1222 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1225 case DISP_E_PARAMNOTFOUND
:
1226 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1229 case DISP_E_TYPEMISMATCH
:
1230 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1234 wsprintf(szBuf
, L
"%s: Unknown error occurred.", szMember
);
1238 wxLogWarning(szBuf
);
1243 #endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)