]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/automtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE automation utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998, Julian Smart
9 // Licence: wxWindows Licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "automtn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
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__)
29 #define _FORCENAMELESSUNION
31 #include "wx/msw/ole/automtn.h"
32 #include "wx/msw/private.h"
44 // wrapper around BSTR type (by Vadim Zeitlin)
46 class WXDLLEXPORT BasicString
50 BasicString(const char *sz
);
54 // just get the string
55 operator BSTR() const { return m_wzBuf
; }
56 // retrieve a copy of our string - caller must SysFreeString() it later!
57 BSTR
Get() const { return SysAllocString(m_wzBuf
); }
60 // @@@ not implemented (but should be)
61 BasicString(const BasicString
&);
62 BasicString
& operator=(const BasicString
&);
64 OLECHAR
*m_wzBuf
; // actual string
68 static bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
) ;
69 static bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
) ;
71 // Convert string to Unicode
72 static BSTR
ConvertStringToOle(const wxString
& str
);
74 // Convert string from BSTR to wxString
75 static wxString
ConvertStringFromOle(BSTR bStr
);
77 // Verifies will fail if the needed buffer size is too large
78 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
79 #define MIN_DATE (-657434L) // about year 100
80 #define MAX_DATE 2958465L // about year 9999
82 // Half a second, expressed in days
83 #define HALF_SECOND (1.0/172800.0)
85 // One-based array of days in year at month start
86 static int rgMonthDays
[13] =
87 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
90 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
91 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
92 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
93 #endif // wxUSE_TIMEDATE
95 static void ClearVariant(VARIANTARG
*pvarg
) ;
96 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
97 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
103 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
105 m_dispatchPtr
= dispatchPtr
;
108 wxAutomationObject::~wxAutomationObject()
112 ((IDispatch
*)m_dispatchPtr
)->Release();
113 m_dispatchPtr
= NULL
;
117 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
119 // For Put/Get, no named arguments are allowed.
120 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
121 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
126 // nonConstMember is necessary because the wxString class doesn't have enough consts...
127 wxString
nonConstMember(member
);
129 int ch
= nonConstMember
.Find('.');
132 // Use dot notation to get the next object
133 wxString
member2(nonConstMember
.Left((size_t) ch
));
134 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
135 wxAutomationObject obj
;
136 if (!GetObject(obj
, member2
))
138 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
142 ClearVariant(& vReturn
);
144 VARIANTARG
* vReturnPtr
= & vReturn
;
146 // Find number of names args
147 int namedArgCount
= 0;
149 for (i
= 0; i
< noArgs
; i
++)
150 if (!INVOKEARG(i
).GetName().IsNull())
155 int namedArgStringCount
= namedArgCount
+ 1;
156 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
157 argNames
[0] = ConvertStringToOle(member
);
159 // Note that arguments are specified in reverse order
160 // (all totally logical; hey, we're dealing with OLE here.)
163 for (i
= 0; i
< namedArgCount
; i
++)
165 if (!INVOKEARG(i
).GetName().IsNull())
167 argNames
[(namedArgCount
-j
)] = ConvertStringToOle(INVOKEARG(i
).GetName());
172 // + 1 for the member name, + 1 again in case we're a 'put'
173 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
176 DISPPARAMS dispparams
;
177 unsigned int uiArgErr
;
180 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
181 // member name as the first name, followed by argument names (if any).
182 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
183 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
186 // ShowException(szMember, hr, NULL, 0);
192 // if doing a property put(ref), we need to adjust the first argument to have a
193 // named arg of DISPID_PROPERTYPUT.
194 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
197 dispIds
[1] = DISPID_PROPERTYPUT
;
198 vReturnPtr
= (VARIANTARG
*) NULL
;
201 // Convert the wxVariants to VARIANTARGs
202 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
203 for (i
= 0; i
< noArgs
; i
++)
205 // Again, reverse args
206 if (!ConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
215 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
216 dispparams
.rgvarg
= oleArgs
;
217 dispparams
.cArgs
= noArgs
;
218 dispparams
.cNamedArgs
= namedArgCount
;
220 excep
.pfnDeferredFillIn
= NULL
;
222 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
223 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
225 for (i
= 0; i
< namedArgStringCount
; i
++)
227 SysFreeString(argNames
[i
]);
232 for (i
= 0; i
< noArgs
; i
++)
233 ReleaseVariant(& oleArgs
[i
]) ;
238 // display the exception information if appropriate:
239 // ShowException((const char*) member, hr, &excep, uiArgErr);
241 // free exception structure information
242 SysFreeString(excep
.bstrSource
);
243 SysFreeString(excep
.bstrDescription
);
244 SysFreeString(excep
.bstrHelpFile
);
247 ReleaseVariant(vReturnPtr
);
254 // Convert result to wxVariant form
255 ConvertOleToVariant(vReturn
, retValue
);
256 // Mustn't release the dispatch pointer
257 if (vReturn
.vt
== VT_DISPATCH
)
259 vReturn
.pdispVal
= (IDispatch
*) NULL
;
261 ReleaseVariant(& vReturn
);
267 // Invoke a member function
268 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
270 wxVariant retVariant
;
271 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
273 retVariant
.MakeNull();
278 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
280 wxVariant retVariant
;
281 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
283 retVariant
.MakeNull();
288 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
289 const wxVariant
& arg1
, const wxVariant
& arg2
,
290 const wxVariant
& arg3
, const wxVariant
& arg4
,
291 const wxVariant
& arg5
, const wxVariant
& arg6
)
293 const wxVariant
** args
= new const wxVariant
*[6];
325 wxVariant retVariant
;
326 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
328 retVariant
.MakeNull();
335 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
337 wxVariant retVariant
;
338 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
340 retVariant
.MakeNull();
344 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
346 wxVariant retVariant
;
347 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
349 retVariant
.MakeNull();
354 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
355 const wxVariant
& arg1
, const wxVariant
& arg2
,
356 const wxVariant
& arg3
, const wxVariant
& arg4
,
357 const wxVariant
& arg5
, const wxVariant
& arg6
)
359 const wxVariant
** args
= new const wxVariant
*[6];
391 wxVariant retVariant
;
392 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
394 retVariant
.MakeNull();
400 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
402 wxVariant retVariant
;
403 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
410 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
412 wxVariant retVariant
;
413 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
420 bool wxAutomationObject::PutProperty(const wxString
& property
,
421 const wxVariant
& arg1
, const wxVariant
& arg2
,
422 const wxVariant
& arg3
, const wxVariant
& arg4
,
423 const wxVariant
& arg5
, const wxVariant
& arg6
)
425 const wxVariant
** args
= new const wxVariant
*[6];
457 wxVariant retVariant
;
458 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
464 // Uses DISPATCH_PROPERTYGET
465 // and returns a dispatch pointer. The calling code should call Release
466 // on the pointer, though this could be implicit by constructing an wxAutomationObject
467 // with it and letting the destructor call Release.
468 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
470 wxVariant retVariant
;
471 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
473 if (retVariant
.GetType() == wxT("void*"))
475 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
479 return (WXIDISPATCH
*) NULL
;
482 // Uses DISPATCH_PROPERTYGET
483 // and returns a dispatch pointer. The calling code should call Release
484 // on the pointer, though this could be implicit by constructing an wxAutomationObject
485 // with it and letting the destructor call Release.
486 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
488 wxVariant retVariant
;
489 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
491 if (retVariant
.GetType() == wxT("void*"))
493 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
497 return (WXIDISPATCH
*) NULL
;
501 // A way of initialising another wxAutomationObject with a dispatch object
502 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
504 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
507 obj
.SetDispatchPtr(dispatch
);
514 // A way of initialising another wxAutomationObject with a dispatch object
515 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
517 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
520 obj
.SetDispatchPtr(dispatch
);
527 // Get a dispatch pointer from the current object associated
529 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
535 IUnknown
* pUnk
= NULL
;
537 BasicString
unicodeName(classId
.mb_str());
539 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
541 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
545 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
547 wxLogWarning(wxT("Cannot find an active object"));
551 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
553 wxLogWarning(wxT("Cannot find IDispatch interface"));
560 // Get a dispatch pointer from a new object associated
561 // with the given class id
562 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
569 BasicString
unicodeName(classId
.mb_str());
571 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
573 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
577 // start a new copy of Excel, grab the IDispatch interface
578 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
580 wxLogWarning(wxT("Cannot start an instance of this class."));
588 bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
590 ClearVariant(&oleVariant
);
591 if (variant
.IsNull())
593 oleVariant
.vt
= VT_NULL
;
597 wxString
type(variant
.GetType());
600 if (type
== wxT("long"))
602 oleVariant
.vt
= VT_I4
;
603 oleVariant
.lVal
= variant
.GetLong() ;
605 // cVal not always present
607 else if (type
== wxT("char"))
609 oleVariant
.vt
=VT_I1
; // Signed Char
610 oleVariant
.cVal
=variant
.GetChar();
613 else if (type
== wxT("double"))
615 oleVariant
.vt
= VT_R8
;
616 oleVariant
.dblVal
= variant
.GetDouble();
618 else if (type
== wxT("bool"))
620 oleVariant
.vt
= VT_BOOL
;
621 // 'bool' required for VC++ 4 apparently
622 #if defined(__WATCOMC__) || (defined(__VISUALC__) && (__VISUALC__ <= 1000))
623 oleVariant
.bool = variant
.GetBool();
625 oleVariant
.boolVal
= variant
.GetBool();
628 else if (type
== wxT("string"))
630 wxString
str( variant
.GetString() );
631 oleVariant
.vt
= VT_BSTR
;
632 oleVariant
.bstrVal
= ConvertStringToOle(str
);
634 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
635 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
636 else if (type
== wxT("date"))
638 wxDate
date( variant
.GetDate() );
639 oleVariant
.vt
= VT_DATE
;
641 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
642 0, 0, 0, oleVariant
.date
))
645 else if (type
== wxT("time"))
647 wxTime
time( variant
.GetTime() );
648 oleVariant
.vt
= VT_DATE
;
650 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
651 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
655 else if (type
== wxT("void*"))
657 oleVariant
.vt
= VT_DISPATCH
;
658 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
660 else if (type
== wxT("list") || type
== wxT("stringlist"))
662 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
665 SAFEARRAYBOUND saBound
;
666 VARIANTARG
*pvargBase
;
670 int iCount
= variant
.GetCount();
673 saBound
.cElements
= iCount
;
675 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
679 SafeArrayAccessData(psa
, (void**)&pvargBase
);
682 for (i
= 0; i
< iCount
; i
++)
684 // copy each string in the list of strings
685 wxVariant
eachVariant(variant
[i
]);
686 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
688 // memory failure: back out and free strings alloc'ed up to
689 // now, and then the array itself.
691 for (j
= 0; j
< i
; j
++)
693 SysFreeString(pvarg
->bstrVal
);
696 SafeArrayDestroy(psa
);
702 SafeArrayUnaccessData(psa
);
704 oleVariant
.parray
= psa
;
708 oleVariant
.vt
= VT_NULL
;
715 #define VT_TYPEMASK 0xfff
718 bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
720 switch (oleVariant
.vt
& VT_TYPEMASK
)
724 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
732 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
735 wxDate
date(tmTemp
.tm_yday
, tmTemp
.tm_mon
, tmTemp
.tm_year
);
736 wxTime
time(date
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
745 variant
= (long) oleVariant
.lVal
;
750 variant
= (long) oleVariant
.iVal
;
756 #if defined(__WATCOMC__) || (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
757 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
758 variant
= (long) (oleVariant
.bool != 0);
760 variant
= (bool) (oleVariant
.bool != 0);
763 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
764 variant
= (long) (oleVariant
.boolVal
!= 0);
766 variant
= (bool) (oleVariant
.boolVal
!= 0);
773 variant
= oleVariant
.dblVal
;
780 int cDims
, cElements
, i
;
783 // Iterate the dimensions: number of elements is x*y*z
784 for (cDims
= 0, cElements
= 1;
785 cDims
< oleVariant
.parray
->cDims
; cDims
++)
786 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
788 // Get a pointer to the data
789 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
793 for (i
= 0; i
< cElements
; i
++)
795 VARIANTARG
& oleElement
= pvdata
[i
];
797 if (!ConvertOleToVariant(oleElement
, vElement
))
800 variant
.Append(vElement
);
802 SafeArrayUnaccessData(oleVariant
.parray
);
807 variant
= (void*) oleVariant
.pdispVal
;
817 break; // Ignore Empty Variant, used only during destruction of objects
821 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
828 static BSTR
ConvertStringToOle(const wxString
& str
)
831 unsigned int len = strlen((const char*) str);
832 unsigned short* s = new unsigned short[len*2+2];
834 memset(s, 0, len*2+2);
835 for (i=0; i < len; i++)
838 BasicString
bstr(str
.mb_str());
842 static wxString
ConvertStringFromOle(BSTR bStr
)
847 int len
= SysStringLen(bStr
) + 1;
848 char *buf
= new char[len
];
849 (void)wcstombs( buf
, bStr
, len
);
856 // ----------------------------------------------------------------------------
858 // ----------------------------------------------------------------------------
860 // ctor takes an ANSI string and transforms it to Unicode
861 BasicString::BasicString(const char *sz
)
863 // get the size of required buffer
864 UINT lenAnsi
= strlen(sz
);
866 UINT lenWide
= lenAnsi
* 2 ;
868 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
872 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
873 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
874 m_wzBuf
[lenWide
] = L
'\0';
882 BasicString::~BasicString()
887 /////////////////////////////////////////////////////////////////////////////
888 // COleDateTime class HELPERS - implementation
890 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
891 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
893 // Validate year and month (ignore day of week and milliseconds)
894 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
897 // Check for leap year and set the number of days in the month
898 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
899 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
902 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
903 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
905 // Finish validating the date
906 if (wDay
< 1 || wDay
> nDaysInMonth
||
907 wHour
> 23 || wMinute
> 59 ||
913 // Cache the date in days and time in fractional days
917 //It is a valid date; make Jan 1, 1AD be 1
918 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
919 rgMonthDays
[wMonth
-1] + wDay
;
921 // If leap year and it's before March, subtract 1:
922 if (wMonth
<= 2 && bLeapYear
)
925 // Offset so that 12/30/1899 is 0
928 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
929 ((long)wMinute
* 60L) + // mins in seconds
930 ((long)wSecond
)) / 86400.;
932 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
937 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
939 // The legal range does not actually span year 0 to 9999.
940 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
943 long nDays
; // Number of days since Dec. 30, 1899
944 long nDaysAbsolute
; // Number of days since 1/1/0
945 long nSecsInDay
; // Time in seconds since midnight
946 long nMinutesInDay
; // Minutes in day
948 long n400Years
; // Number of 400 year increments since 1/1/0
949 long n400Century
; // Century within 400 year block (0,1,2 or 3)
950 long n4Years
; // Number of 4 year increments since 1/1/0
951 long n4Day
; // Day within 4 year block
952 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
953 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
954 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
956 double dblDate
= dtSrc
; // tempory serial date
958 // If a valid date, then this conversion should not overflow
959 nDays
= (long)dblDate
;
961 // Round to the second
962 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
964 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
966 dblDate
= fabs(dblDate
);
967 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
969 // Calculate the day of week (sun=1, mon=2...)
970 // -1 because 1/1/0 is Sat. +1 because we want 1-based
971 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
973 // Leap years every 4 yrs except centuries not multiples of 400.
974 n400Years
= (long)(nDaysAbsolute
/ 146097L);
976 // Set nDaysAbsolute to day within 400-year block
977 nDaysAbsolute
%= 146097L;
979 // -1 because first century has extra day
980 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
983 if (n400Century
!= 0)
985 // Set nDaysAbsolute to day within century
986 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
988 // +1 because 1st 4 year increment has 1460 days
989 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
992 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
996 n4Day
= (long)nDaysAbsolute
;
1001 // Leap century - not special case!
1002 n4Years
= (long)(nDaysAbsolute
/ 1461L);
1003 n4Day
= (long)(nDaysAbsolute
% 1461L);
1008 // -1 because first year has 366 days
1009 n4Yr
= (n4Day
- 1) / 365;
1012 n4Day
= (n4Day
- 1) % 365;
1020 // n4Day is now 0-based day of year. Save 1-based day of year, year number
1021 tmDest
.tm_yday
= (int)n4Day
+ 1;
1022 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
1024 // Handle leap year: before, on, and after Feb. 29.
1025 if (n4Yr
== 0 && bLeap4
)
1032 tmDest
.tm_mday
= 29;
1036 // Pretend it's not a leap year for month/day comp.
1041 // Make n4DaY a 1-based day of non-leap year and compute
1042 // month/day for everything but Feb. 29.
1045 // Month number always >= n/32, so save some loop time */
1046 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
1047 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
1049 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
1052 if (nSecsInDay
== 0)
1053 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
1056 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
1057 nMinutesInDay
= nSecsInDay
/ 60L;
1058 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
1059 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
1065 // this function is not used
1067 void TmConvertToStandardFormat(struct tm
& tmSrc
)
1069 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
1070 tmSrc
.tm_year
-= 1900; // year is based on 1900
1071 tmSrc
.tm_mon
-= 1; // month of year is 0-based
1072 tmSrc
.tm_wday
-= 1; // day of week is 0-based
1073 tmSrc
.tm_yday
-= 1; // day of year is 0-based
1076 double DoubleFromDate(DATE dt
)
1078 // No problem if positive
1082 // If negative, must convert since negative dates not continuous
1083 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
1084 double temp
= ceil(dt
);
1085 return temp
- (dt
- temp
);
1088 DATE
DateFromDouble(double dbl
)
1090 // No problem if positive
1094 // If negative, must convert since negative dates not continuous
1095 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1096 double temp
= floor(dbl
); // dbl is now whole part
1097 return temp
+ (temp
- dbl
);
1104 * Zeros a variant structure without regard to current contents
1106 static void ClearVariant(VARIANTARG
*pvarg
)
1108 pvarg
->vt
= VT_EMPTY
;
1109 pvarg
->wReserved1
= 0;
1110 pvarg
->wReserved2
= 0;
1111 pvarg
->wReserved3
= 0;
1118 * Clears a particular variant structure and releases any external objects
1119 * or memory contained in the variant. Supports the data types listed above.
1121 static void ReleaseVariant(VARIANTARG
*pvarg
)
1124 VARIANTARG _huge
*pvargArray
;
1125 long lLBound
, lUBound
, l
;
1127 vt
= pvarg
->vt
& 0xfff; // mask off flags
1129 // check if an array. If so, free its contents, then the array itself.
1130 if (V_ISARRAY(pvarg
))
1132 // variant arrays are all this routine currently knows about. Since a
1133 // variant can contain anything (even other arrays), call ourselves
1135 if (vt
== VT_VARIANT
)
1137 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1138 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1140 if (lUBound
> lLBound
)
1144 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1146 for (l
= 0; l
< lUBound
; l
++)
1148 ReleaseVariant(pvargArray
);
1152 SafeArrayUnaccessData(pvarg
->parray
);
1157 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1160 // Free the array itself.
1161 SafeArrayDestroy(pvarg
->parray
);
1168 if (pvarg
->pdispVal
)
1169 pvarg
->pdispVal
->Release();
1173 SysFreeString(pvarg
->bstrVal
);
1179 case VT_ERROR
: // to avoid erroring on an error return from Excel
1180 // no work for these types
1184 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1189 ClearVariant(pvarg
);
1194 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1198 switch (GetScode(hr
))
1200 case DISP_E_UNKNOWNNAME
:
1201 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1204 case DISP_E_BADPARAMCOUNT
:
1205 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1208 case DISP_E_EXCEPTION
:
1209 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1210 if (pexcep
->bstrDescription
!= NULL
)
1211 lstrcat(szBuf
, pexcep
->bstrDescription
);
1213 lstrcat(szBuf
, L
"<<No Description>>");
1216 case DISP_E_MEMBERNOTFOUND
:
1217 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1220 case DISP_E_OVERFLOW
:
1221 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1224 case DISP_E_NONAMEDARGS
:
1225 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1229 case DISP_E_UNKNOWNLCID
:
1230 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1233 case DISP_E_PARAMNOTOPTIONAL
:
1234 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1237 case DISP_E_PARAMNOTFOUND
:
1238 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1241 case DISP_E_TYPEMISMATCH
:
1242 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1246 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1250 wxLogWarning(szBuf
);
1255 #endif // __WATCOMC__