]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/automtn.cpp
826bfe2cb3cfbd246b98d59ac36d17394ca7761b
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__)
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(__WATCOMC__) && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__) && !defined(__WXWINE__)
30 #include "wx/msw/ole/automtn.h"
31 #include "wx/msw/private.h"
43 // wrapper around BSTR type (by Vadim Zeitlin)
45 class WXDLLEXPORT BasicString
49 BasicString(const char *sz
);
53 // just get the string
54 operator BSTR() const { return m_wzBuf
; }
55 // retrieve a copy of our string - caller must SysFreeString() it later!
56 BSTR
Get() const { return SysAllocString(m_wzBuf
); }
59 // @@@ not implemented (but should be)
60 BasicString(const BasicString
&);
61 BasicString
& operator=(const BasicString
&);
63 OLECHAR
*m_wzBuf
; // actual string
67 static bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
) ;
68 static bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
) ;
70 // Convert string to Unicode
71 static BSTR
ConvertStringToOle(const wxString
& str
);
73 // Convert string from BSTR to wxString
74 static wxString
ConvertStringFromOle(BSTR bStr
);
76 // Verifies will fail if the needed buffer size is too large
77 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
78 #define MIN_DATE (-657434L) // about year 100
79 #define MAX_DATE 2958465L // about year 9999
81 // Half a second, expressed in days
82 #define HALF_SECOND (1.0/172800.0)
84 // One-based array of days in year at month start
85 static int rgMonthDays
[13] =
86 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
89 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
90 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
91 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
92 #endif // wxUSE_TIMEDATE
94 static void ClearVariant(VARIANTARG
*pvarg
) ;
95 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
96 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
102 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
104 m_dispatchPtr
= dispatchPtr
;
107 wxAutomationObject::~wxAutomationObject()
111 ((IDispatch
*)m_dispatchPtr
)->Release();
112 m_dispatchPtr
= NULL
;
116 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
118 // For Put/Get, no named arguments are allowed.
119 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
120 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
125 // nonConstMember is necessary because the wxString class doesn't have enough consts...
126 wxString
nonConstMember(member
);
128 int ch
= nonConstMember
.Find('.');
131 // Use dot notation to get the next object
132 wxString
member2(nonConstMember
.Left((size_t) ch
));
133 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
134 wxAutomationObject obj
;
135 if (!GetObject(obj
, member2
))
137 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
141 ClearVariant(& vReturn
);
143 VARIANTARG
* vReturnPtr
= & vReturn
;
145 // Find number of names args
146 int namedArgCount
= 0;
148 for (i
= 0; i
< noArgs
; i
++)
149 if (!INVOKEARG(i
).GetName().IsNull())
154 int namedArgStringCount
= namedArgCount
+ 1;
155 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
156 argNames
[0] = ConvertStringToOle(member
);
158 // Note that arguments are specified in reverse order
159 // (all totally logical; hey, we're dealing with OLE here.)
162 for (i
= 0; i
< namedArgCount
; i
++)
164 if (!INVOKEARG(i
).GetName().IsNull())
166 argNames
[(namedArgCount
-j
)] = ConvertStringToOle(INVOKEARG(i
).GetName());
171 // + 1 for the member name, + 1 again in case we're a 'put'
172 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
175 DISPPARAMS dispparams
;
176 unsigned int uiArgErr
;
179 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
180 // member name as the first name, followed by argument names (if any).
181 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
182 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
185 // ShowException(szMember, hr, NULL, 0);
189 // if doing a property put(ref), we need to adjust the first argument to have a
190 // named arg of DISPID_PROPERTYPUT.
191 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
194 dispIds
[1] = DISPID_PROPERTYPUT
;
195 vReturnPtr
= (VARIANTARG
*) NULL
;
198 // Convert the wxVariants to VARIANTARGs
199 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
200 for (i
= 0; i
< noArgs
; i
++)
202 // Again, reverse args
203 if (!ConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
204 return FALSE
; // TODO: clean up memory at this point
207 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
208 dispparams
.rgvarg
= oleArgs
;
209 dispparams
.cArgs
= noArgs
;
210 dispparams
.cNamedArgs
= namedArgCount
;
212 excep
.pfnDeferredFillIn
= NULL
;
214 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
215 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
217 for (i
= 0; i
< namedArgStringCount
; i
++)
219 SysFreeString(argNames
[i
]);
224 for (i
= 0; i
< noArgs
; i
++)
225 ReleaseVariant(& oleArgs
[i
]) ;
230 // display the exception information if appropriate:
231 // ShowException((const char*) member, hr, &excep, uiArgErr);
233 // free exception structure information
234 SysFreeString(excep
.bstrSource
);
235 SysFreeString(excep
.bstrDescription
);
236 SysFreeString(excep
.bstrHelpFile
);
239 ReleaseVariant(vReturnPtr
);
246 // Convert result to wxVariant form
247 ConvertOleToVariant(vReturn
, retValue
);
248 // Mustn't release the dispatch pointer
249 if (vReturn
.vt
== VT_DISPATCH
)
251 vReturn
.pdispVal
= (IDispatch
*) NULL
;
253 ReleaseVariant(& vReturn
);
259 // Invoke a member function
260 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
262 wxVariant retVariant
;
263 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
265 retVariant
.MakeNull();
270 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
272 wxVariant retVariant
;
273 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
275 retVariant
.MakeNull();
280 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
281 const wxVariant
& arg1
, const wxVariant
& arg2
,
282 const wxVariant
& arg3
, const wxVariant
& arg4
,
283 const wxVariant
& arg5
, const wxVariant
& arg6
)
285 const wxVariant
** args
= new const wxVariant
*[6];
317 wxVariant retVariant
;
318 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
320 retVariant
.MakeNull();
327 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
329 wxVariant retVariant
;
330 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
332 retVariant
.MakeNull();
336 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
338 wxVariant retVariant
;
339 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
341 retVariant
.MakeNull();
346 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
347 const wxVariant
& arg1
, const wxVariant
& arg2
,
348 const wxVariant
& arg3
, const wxVariant
& arg4
,
349 const wxVariant
& arg5
, const wxVariant
& arg6
)
351 const wxVariant
** args
= new const wxVariant
*[6];
383 wxVariant retVariant
;
384 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
386 retVariant
.MakeNull();
392 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
394 wxVariant retVariant
;
395 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
402 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
404 wxVariant retVariant
;
405 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
412 bool wxAutomationObject::PutProperty(const wxString
& property
,
413 const wxVariant
& arg1
, const wxVariant
& arg2
,
414 const wxVariant
& arg3
, const wxVariant
& arg4
,
415 const wxVariant
& arg5
, const wxVariant
& arg6
)
417 const wxVariant
** args
= new const wxVariant
*[6];
449 wxVariant retVariant
;
450 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
456 // Uses DISPATCH_PROPERTYGET
457 // and returns a dispatch pointer. The calling code should call Release
458 // on the pointer, though this could be implicit by constructing an wxAutomationObject
459 // with it and letting the destructor call Release.
460 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
462 wxVariant retVariant
;
463 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
465 if (retVariant
.GetType() == wxT("void*"))
467 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
471 return (WXIDISPATCH
*) NULL
;
474 // Uses DISPATCH_PROPERTYGET
475 // and returns a dispatch pointer. The calling code should call Release
476 // on the pointer, though this could be implicit by constructing an wxAutomationObject
477 // with it and letting the destructor call Release.
478 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
480 wxVariant retVariant
;
481 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
483 if (retVariant
.GetType() == wxT("void*"))
485 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
489 return (WXIDISPATCH
*) NULL
;
493 // A way of initialising another wxAutomationObject with a dispatch object
494 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
496 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
499 obj
.SetDispatchPtr(dispatch
);
506 // A way of initialising another wxAutomationObject with a dispatch object
507 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
509 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
512 obj
.SetDispatchPtr(dispatch
);
519 // Get a dispatch pointer from the current object associated
521 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
527 IUnknown
* pUnk
= NULL
;
529 BasicString
unicodeName(classId
.mb_str());
531 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
533 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
537 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
539 wxLogWarning(wxT("Cannot find an active object"));
543 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
545 wxLogWarning(wxT("Cannot find IDispatch interface"));
552 // Get a dispatch pointer from a new object associated
553 // with the given class id
554 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
561 BasicString
unicodeName(classId
.mb_str());
563 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
565 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
569 // start a new copy of Excel, grab the IDispatch interface
570 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
572 wxLogWarning(wxT("Cannot start an instance of this class."));
580 bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
582 ClearVariant(&oleVariant
);
583 if (variant
.IsNull())
585 oleVariant
.vt
= VT_NULL
;
589 wxString
type(variant
.GetType());
592 if (type
== wxT("long"))
594 oleVariant
.vt
= VT_I4
;
595 oleVariant
.lVal
= variant
.GetLong() ;
597 // cVal not always present
599 else if (type
== wxT("char"))
601 oleVariant
.vt
=VT_I1
; // Signed Char
602 oleVariant
.cVal
=variant
.GetChar();
605 else if (type
== wxT("double"))
607 oleVariant
.vt
= VT_R8
;
608 oleVariant
.dblVal
= variant
.GetDouble();
610 else if (type
== wxT("bool"))
612 oleVariant
.vt
= VT_BOOL
;
613 // 'bool' required for VC++ 4 apparently
614 #if defined(__WATCOMC__) || (defined(__VISUALC__) && (__VISUALC__ <= 1000))
615 oleVariant
.bool = variant
.GetBool();
617 oleVariant
.boolVal
= variant
.GetBool();
620 else if (type
== wxT("string"))
622 wxString
str( variant
.GetString() );
623 oleVariant
.vt
= VT_BSTR
;
624 oleVariant
.bstrVal
= ConvertStringToOle(str
);
626 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
627 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
628 else if (type
== wxT("date"))
630 wxDate
date( variant
.GetDate() );
631 oleVariant
.vt
= VT_DATE
;
633 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
634 0, 0, 0, oleVariant
.date
))
637 else if (type
== wxT("time"))
639 wxTime
time( variant
.GetTime() );
640 oleVariant
.vt
= VT_DATE
;
642 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
643 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
647 else if (type
== wxT("void*"))
649 oleVariant
.vt
= VT_DISPATCH
;
650 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
652 else if (type
== wxT("list") || type
== wxT("stringlist"))
654 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
657 SAFEARRAYBOUND saBound
;
658 VARIANTARG
*pvargBase
;
662 int iCount
= variant
.GetCount();
665 saBound
.cElements
= iCount
;
667 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
671 SafeArrayAccessData(psa
, (void**)&pvargBase
);
674 for (i
= 0; i
< iCount
; i
++)
676 // copy each string in the list of strings
677 wxVariant
eachVariant(variant
[i
]);
678 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
680 // memory failure: back out and free strings alloc'ed up to
681 // now, and then the array itself.
683 for (j
= 0; j
< i
; j
++)
685 SysFreeString(pvarg
->bstrVal
);
688 SafeArrayDestroy(psa
);
694 SafeArrayUnaccessData(psa
);
696 oleVariant
.parray
= psa
;
700 oleVariant
.vt
= VT_NULL
;
707 #define VT_TYPEMASK 0xfff
710 bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
712 switch (oleVariant
.vt
& VT_TYPEMASK
)
716 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
724 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
727 wxDate
date(tmTemp
.tm_yday
, tmTemp
.tm_mon
, tmTemp
.tm_year
);
728 wxTime
time(date
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
737 variant
= (long) oleVariant
.lVal
;
742 variant
= (long) oleVariant
.iVal
;
748 #if defined(__WATCOMC__) || (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
749 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
750 variant
= (long) (oleVariant
.bool != 0);
752 variant
= (bool) (oleVariant
.bool != 0);
755 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
756 variant
= (long) (oleVariant
.boolVal
!= 0);
758 variant
= (bool) (oleVariant
.boolVal
!= 0);
765 variant
= oleVariant
.dblVal
;
772 int cDims
, cElements
, i
;
775 // Iterate the dimensions: number of elements is x*y*z
776 for (cDims
= 0, cElements
= 1;
777 cDims
< oleVariant
.parray
->cDims
; cDims
++)
778 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
780 // Get a pointer to the data
781 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
785 for (i
= 0; i
< cElements
; i
++)
787 VARIANTARG
& oleElement
= pvdata
[i
];
789 if (!ConvertOleToVariant(oleElement
, vElement
))
792 variant
.Append(vElement
);
794 SafeArrayUnaccessData(oleVariant
.parray
);
799 variant
= (void*) oleVariant
.pdispVal
;
809 break; // Ignore Empty Variant, used only during destruction of objects
813 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
820 static BSTR
ConvertStringToOle(const wxString
& str
)
823 unsigned int len = strlen((const char*) str);
824 unsigned short* s = new unsigned short[len*2+2];
826 memset(s, 0, len*2+2);
827 for (i=0; i < len; i++)
830 BasicString
bstr(str
.mb_str());
834 static wxString
ConvertStringFromOle(BSTR bStr
)
836 int len
= SysStringLen(bStr
) + 1;
837 char *buf
= new char[len
];
838 (void)wcstombs( buf
, bStr
, len
);
845 // ----------------------------------------------------------------------------
847 // ----------------------------------------------------------------------------
849 // ctor takes an ANSI string and transforms it to Unicode
850 BasicString::BasicString(const char *sz
)
852 // get the size of required buffer
853 UINT lenAnsi
= strlen(sz
);
855 UINT lenWide
= lenAnsi
* 2 ;
857 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
861 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
862 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
863 m_wzBuf
[lenWide
] = L
'\0';
871 BasicString::~BasicString()
876 /////////////////////////////////////////////////////////////////////////////
877 // COleDateTime class HELPERS - implementation
879 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
880 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
882 // Validate year and month (ignore day of week and milliseconds)
883 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
886 // Check for leap year and set the number of days in the month
887 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
888 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
891 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
892 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
894 // Finish validating the date
895 if (wDay
< 1 || wDay
> nDaysInMonth
||
896 wHour
> 23 || wMinute
> 59 ||
902 // Cache the date in days and time in fractional days
906 //It is a valid date; make Jan 1, 1AD be 1
907 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
908 rgMonthDays
[wMonth
-1] + wDay
;
910 // If leap year and it's before March, subtract 1:
911 if (wMonth
<= 2 && bLeapYear
)
914 // Offset so that 12/30/1899 is 0
917 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
918 ((long)wMinute
* 60L) + // mins in seconds
919 ((long)wSecond
)) / 86400.;
921 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
926 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
928 // The legal range does not actually span year 0 to 9999.
929 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
932 long nDays
; // Number of days since Dec. 30, 1899
933 long nDaysAbsolute
; // Number of days since 1/1/0
934 long nSecsInDay
; // Time in seconds since midnight
935 long nMinutesInDay
; // Minutes in day
937 long n400Years
; // Number of 400 year increments since 1/1/0
938 long n400Century
; // Century within 400 year block (0,1,2 or 3)
939 long n4Years
; // Number of 4 year increments since 1/1/0
940 long n4Day
; // Day within 4 year block
941 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
942 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
943 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
945 double dblDate
= dtSrc
; // tempory serial date
947 // If a valid date, then this conversion should not overflow
948 nDays
= (long)dblDate
;
950 // Round to the second
951 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
953 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
955 dblDate
= fabs(dblDate
);
956 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
958 // Calculate the day of week (sun=1, mon=2...)
959 // -1 because 1/1/0 is Sat. +1 because we want 1-based
960 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
962 // Leap years every 4 yrs except centuries not multiples of 400.
963 n400Years
= (long)(nDaysAbsolute
/ 146097L);
965 // Set nDaysAbsolute to day within 400-year block
966 nDaysAbsolute
%= 146097L;
968 // -1 because first century has extra day
969 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
972 if (n400Century
!= 0)
974 // Set nDaysAbsolute to day within century
975 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
977 // +1 because 1st 4 year increment has 1460 days
978 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
981 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
985 n4Day
= (long)nDaysAbsolute
;
990 // Leap century - not special case!
991 n4Years
= (long)(nDaysAbsolute
/ 1461L);
992 n4Day
= (long)(nDaysAbsolute
% 1461L);
997 // -1 because first year has 366 days
998 n4Yr
= (n4Day
- 1) / 365;
1001 n4Day
= (n4Day
- 1) % 365;
1009 // n4Day is now 0-based day of year. Save 1-based day of year, year number
1010 tmDest
.tm_yday
= (int)n4Day
+ 1;
1011 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
1013 // Handle leap year: before, on, and after Feb. 29.
1014 if (n4Yr
== 0 && bLeap4
)
1021 tmDest
.tm_mday
= 29;
1025 // Pretend it's not a leap year for month/day comp.
1030 // Make n4DaY a 1-based day of non-leap year and compute
1031 // month/day for everything but Feb. 29.
1034 // Month number always >= n/32, so save some loop time */
1035 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
1036 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
1038 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
1041 if (nSecsInDay
== 0)
1042 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
1045 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
1046 nMinutesInDay
= nSecsInDay
/ 60L;
1047 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
1048 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
1054 // this function is not used
1056 void TmConvertToStandardFormat(struct tm
& tmSrc
)
1058 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
1059 tmSrc
.tm_year
-= 1900; // year is based on 1900
1060 tmSrc
.tm_mon
-= 1; // month of year is 0-based
1061 tmSrc
.tm_wday
-= 1; // day of week is 0-based
1062 tmSrc
.tm_yday
-= 1; // day of year is 0-based
1065 double DoubleFromDate(DATE dt
)
1067 // No problem if positive
1071 // If negative, must convert since negative dates not continuous
1072 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
1073 double temp
= ceil(dt
);
1074 return temp
- (dt
- temp
);
1077 DATE
DateFromDouble(double dbl
)
1079 // No problem if positive
1083 // If negative, must convert since negative dates not continuous
1084 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1085 double temp
= floor(dbl
); // dbl is now whole part
1086 return temp
+ (temp
- dbl
);
1093 * Zeros a variant structure without regard to current contents
1095 static void ClearVariant(VARIANTARG
*pvarg
)
1097 pvarg
->vt
= VT_EMPTY
;
1098 pvarg
->wReserved1
= 0;
1099 pvarg
->wReserved2
= 0;
1100 pvarg
->wReserved3
= 0;
1107 * Clears a particular variant structure and releases any external objects
1108 * or memory contained in the variant. Supports the data types listed above.
1110 static void ReleaseVariant(VARIANTARG
*pvarg
)
1113 VARIANTARG _huge
*pvargArray
;
1114 long lLBound
, lUBound
, l
;
1116 vt
= pvarg
->vt
& 0xfff; // mask off flags
1118 // check if an array. If so, free its contents, then the array itself.
1119 if (V_ISARRAY(pvarg
))
1121 // variant arrays are all this routine currently knows about. Since a
1122 // variant can contain anything (even other arrays), call ourselves
1124 if (vt
== VT_VARIANT
)
1126 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1127 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1129 if (lUBound
> lLBound
)
1133 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1135 for (l
= 0; l
< lUBound
; l
++)
1137 ReleaseVariant(pvargArray
);
1141 SafeArrayUnaccessData(pvarg
->parray
);
1146 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1149 // Free the array itself.
1150 SafeArrayDestroy(pvarg
->parray
);
1157 if (pvarg
->pdispVal
)
1158 pvarg
->pdispVal
->Release();
1162 SysFreeString(pvarg
->bstrVal
);
1168 case VT_ERROR
: // to avoid erroring on an error return from Excel
1169 // no work for these types
1173 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1178 ClearVariant(pvarg
);
1183 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1187 switch (GetScode(hr
))
1189 case DISP_E_UNKNOWNNAME
:
1190 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1193 case DISP_E_BADPARAMCOUNT
:
1194 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1197 case DISP_E_EXCEPTION
:
1198 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1199 if (pexcep
->bstrDescription
!= NULL
)
1200 lstrcat(szBuf
, pexcep
->bstrDescription
);
1202 lstrcat(szBuf
, L
"<<No Description>>");
1205 case DISP_E_MEMBERNOTFOUND
:
1206 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1209 case DISP_E_OVERFLOW
:
1210 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1213 case DISP_E_NONAMEDARGS
:
1214 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1218 case DISP_E_UNKNOWNLCID
:
1219 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1222 case DISP_E_PARAMNOTOPTIONAL
:
1223 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1226 case DISP_E_PARAMNOTFOUND
:
1227 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1230 case DISP_E_TYPEMISMATCH
:
1231 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1235 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1239 wxLogWarning(szBuf
);
1244 #endif // __WATCOMC__