]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
21 // Watcom C++ gives a linker error if this is compiled in.
22 // With Borland C++, all samples crash if this is compiled in.
23 #if wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
25 #define _FORCENAMELESSUNION
27 #include "wx/msw/private.h"
28 #include "wx/msw/ole/oleutils.h"
29 #include "wx/msw/ole/automtn.h"
33 #include "wx/msw/wince/time.h"
50 // Verifies will fail if the needed buffer size is too large
51 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
52 #define MIN_DATE (-657434L) // about year 100
53 #define MAX_DATE 2958465L // about year 9999
55 // Half a second, expressed in days
56 #define HALF_SECOND (1.0/172800.0)
58 // One-based array of days in year at month start
59 static int rgMonthDays
[13] =
60 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
63 #include "wx/datetime.h"
65 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
66 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
67 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
68 #endif // wxUSE_TIMEDATE
70 static void ClearVariant(VARIANTARG
*pvarg
) ;
71 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
72 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
78 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
80 m_dispatchPtr
= dispatchPtr
;
83 wxAutomationObject::~wxAutomationObject()
87 ((IDispatch
*)m_dispatchPtr
)->Release();
92 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
94 // For Put/Get, no named arguments are allowed.
95 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
96 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
101 // nonConstMember is necessary because the wxString class doesn't have enough consts...
102 wxString
nonConstMember(member
);
104 int ch
= nonConstMember
.Find('.');
107 // Use dot notation to get the next object
108 wxString
member2(nonConstMember
.Left((size_t) ch
));
109 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
110 wxAutomationObject obj
;
111 if (!GetObject(obj
, member2
))
113 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
117 ClearVariant(& vReturn
);
119 VARIANTARG
* vReturnPtr
= & vReturn
;
121 // Find number of names args
122 int namedArgCount
= 0;
124 for (i
= 0; i
< noArgs
; i
++)
125 if (!INVOKEARG(i
).GetName().IsNull())
130 int namedArgStringCount
= namedArgCount
+ 1;
131 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
132 argNames
[0] = wxConvertStringToOle(member
);
134 // Note that arguments are specified in reverse order
135 // (all totally logical; hey, we're dealing with OLE here.)
138 for (i
= 0; i
< namedArgCount
; i
++)
140 if (!INVOKEARG(i
).GetName().IsNull())
142 argNames
[(namedArgCount
-j
)] = wxConvertStringToOle(INVOKEARG(i
).GetName());
147 // + 1 for the member name, + 1 again in case we're a 'put'
148 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
151 DISPPARAMS dispparams
;
152 unsigned int uiArgErr
;
155 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
156 // member name as the first name, followed by argument names (if any).
157 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
158 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
161 // ShowException(szMember, hr, NULL, 0);
167 // if doing a property put(ref), we need to adjust the first argument to have a
168 // named arg of DISPID_PROPERTYPUT.
169 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
172 dispIds
[1] = DISPID_PROPERTYPUT
;
173 vReturnPtr
= (VARIANTARG
*) NULL
;
176 // Convert the wxVariants to VARIANTARGs
177 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
178 for (i
= 0; i
< noArgs
; i
++)
180 // Again, reverse args
181 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
190 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
191 dispparams
.rgvarg
= oleArgs
;
192 dispparams
.cArgs
= noArgs
;
193 dispparams
.cNamedArgs
= namedArgCount
;
195 excep
.pfnDeferredFillIn
= NULL
;
197 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
198 (WORD
)action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
200 for (i
= 0; i
< namedArgStringCount
; i
++)
202 SysFreeString(argNames
[i
]);
207 for (i
= 0; i
< noArgs
; i
++)
208 ReleaseVariant(& oleArgs
[i
]) ;
213 // display the exception information if appropriate:
214 // ShowException((const char*) member, hr, &excep, uiArgErr);
216 // free exception structure information
217 SysFreeString(excep
.bstrSource
);
218 SysFreeString(excep
.bstrDescription
);
219 SysFreeString(excep
.bstrHelpFile
);
222 ReleaseVariant(vReturnPtr
);
229 // Convert result to wxVariant form
230 wxConvertOleToVariant(vReturn
, retValue
);
231 // Mustn't release the dispatch pointer
232 if (vReturn
.vt
== VT_DISPATCH
)
234 vReturn
.pdispVal
= (IDispatch
*) NULL
;
236 ReleaseVariant(& vReturn
);
242 // Invoke a member function
243 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
245 wxVariant retVariant
;
246 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
248 retVariant
.MakeNull();
253 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
255 wxVariant retVariant
;
256 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
258 retVariant
.MakeNull();
263 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
264 const wxVariant
& arg1
, const wxVariant
& arg2
,
265 const wxVariant
& arg3
, const wxVariant
& arg4
,
266 const wxVariant
& arg5
, const wxVariant
& arg6
)
268 const wxVariant
** args
= new const wxVariant
*[6];
300 wxVariant retVariant
;
301 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
303 retVariant
.MakeNull();
310 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
312 wxVariant retVariant
;
313 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
315 retVariant
.MakeNull();
319 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
321 wxVariant retVariant
;
322 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
324 retVariant
.MakeNull();
329 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
330 const wxVariant
& arg1
, const wxVariant
& arg2
,
331 const wxVariant
& arg3
, const wxVariant
& arg4
,
332 const wxVariant
& arg5
, const wxVariant
& arg6
)
334 const wxVariant
** args
= new const wxVariant
*[6];
366 wxVariant retVariant
;
367 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
369 retVariant
.MakeNull();
375 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
377 wxVariant retVariant
;
378 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
385 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
387 wxVariant retVariant
;
388 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
395 bool wxAutomationObject::PutProperty(const wxString
& property
,
396 const wxVariant
& arg1
, const wxVariant
& arg2
,
397 const wxVariant
& arg3
, const wxVariant
& arg4
,
398 const wxVariant
& arg5
, const wxVariant
& arg6
)
400 const wxVariant
** args
= new const wxVariant
*[6];
432 wxVariant retVariant
;
433 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
439 // Uses DISPATCH_PROPERTYGET
440 // and returns a dispatch pointer. The calling code should call Release
441 // on the pointer, though this could be implicit by constructing an wxAutomationObject
442 // with it and letting the destructor call Release.
443 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
445 wxVariant retVariant
;
446 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
448 if (retVariant
.GetType() == wxT("void*"))
450 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
454 return (WXIDISPATCH
*) NULL
;
457 // Uses DISPATCH_PROPERTYGET
458 // and returns a dispatch pointer. The calling code should call Release
459 // on the pointer, though this could be implicit by constructing an wxAutomationObject
460 // with it and letting the destructor call Release.
461 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
463 wxVariant retVariant
;
464 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
466 if (retVariant
.GetType() == wxT("void*"))
468 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
472 return (WXIDISPATCH
*) NULL
;
476 // A way of initialising another wxAutomationObject with a dispatch object
477 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
479 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
482 obj
.SetDispatchPtr(dispatch
);
489 // A way of initialising another wxAutomationObject with a dispatch object
490 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
492 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
495 obj
.SetDispatchPtr(dispatch
);
502 // Get a dispatch pointer from the current object associated
504 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
510 IUnknown
* pUnk
= NULL
;
512 wxBasicString
unicodeName(classId
.mb_str());
514 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
516 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
520 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
522 wxLogWarning(wxT("Cannot find an active object"));
526 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
528 wxLogWarning(wxT("Cannot find IDispatch interface"));
535 // Get a dispatch pointer from a new object associated
536 // with the given class id
537 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
544 wxBasicString
unicodeName(classId
.mb_str());
546 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
548 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
552 // start a new copy of Excel, grab the IDispatch interface
553 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
555 wxLogWarning(wxT("Cannot start an instance of this class."));
563 bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
565 ClearVariant(&oleVariant
);
566 if (variant
.IsNull())
568 oleVariant
.vt
= VT_NULL
;
572 wxString
type(variant
.GetType());
575 if (type
== wxT("long"))
577 oleVariant
.vt
= VT_I4
;
578 oleVariant
.lVal
= variant
.GetLong() ;
580 // cVal not always present
582 else if (type
== wxT("char"))
584 oleVariant
.vt
=VT_I1
; // Signed Char
585 oleVariant
.cVal
=variant
.GetChar();
588 else if (type
== wxT("double"))
590 oleVariant
.vt
= VT_R8
;
591 oleVariant
.dblVal
= variant
.GetDouble();
593 else if (type
== wxT("bool"))
595 oleVariant
.vt
= VT_BOOL
;
596 // 'bool' required for VC++ 4 apparently
597 #if (defined(__VISUALC__) && (__VISUALC__ <= 1000))
598 oleVariant
.bool = variant
.GetBool();
600 oleVariant
.boolVal
= variant
.GetBool();
603 else if (type
== wxT("string"))
605 wxString
str( variant
.GetString() );
606 oleVariant
.vt
= VT_BSTR
;
607 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
610 else if (type
== wxT("datetime"))
612 wxDateTime
date( variant
.GetDateTime() );
613 oleVariant
.vt
= VT_DATE
;
615 if (!OleDateFromTm((WORD
)date
.GetYear(), date
.GetMonth(), date
.GetDay(),
616 date
.GetHour(), date
.GetMinute(), date
.GetSecond(), oleVariant
.date
))
620 else if (type
== wxT("void*"))
622 oleVariant
.vt
= VT_DISPATCH
;
623 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
625 else if (type
== wxT("list") || type
== wxT("stringlist"))
627 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
630 SAFEARRAYBOUND saBound
;
631 VARIANTARG
*pvargBase
;
635 int iCount
= variant
.GetCount();
638 saBound
.cElements
= iCount
;
640 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
644 SafeArrayAccessData(psa
, (void**)&pvargBase
);
647 for (i
= 0; i
< iCount
; i
++)
649 // copy each string in the list of strings
650 wxVariant
eachVariant(variant
[i
]);
651 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
653 // memory failure: back out and free strings alloc'ed up to
654 // now, and then the array itself.
656 for (j
= 0; j
< i
; j
++)
658 SysFreeString(pvarg
->bstrVal
);
661 SafeArrayDestroy(psa
);
667 SafeArrayUnaccessData(psa
);
669 oleVariant
.parray
= psa
;
673 oleVariant
.vt
= VT_NULL
;
680 #define VT_TYPEMASK 0xfff
683 bool wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
685 switch (oleVariant
.vt
& VT_TYPEMASK
)
689 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
697 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
700 wxDateTime
date((wxDateTime::wxDateTime_t
) tmTemp
.tm_yday
,
701 (wxDateTime::Month
) tmTemp
.tm_mon
,
703 (wxDateTime::wxDateTime_t
) tmTemp
.tm_hour
,
704 (wxDateTime::wxDateTime_t
) tmTemp
.tm_min
,
705 (wxDateTime::wxDateTime_t
) 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
= (VARTYPE
)(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 occurred.", szMember
);
1234 wxLogWarning(szBuf
);
1239 #endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)