]>
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/oleutils.h"
32 #include "wx/msw/ole/automtn.h"
33 #include "wx/msw/private.h"
45 // Verifies will fail if the needed buffer size is too large
46 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
47 #define MIN_DATE (-657434L) // about year 100
48 #define MAX_DATE 2958465L // about year 9999
50 // Half a second, expressed in days
51 #define HALF_SECOND (1.0/172800.0)
53 // One-based array of days in year at month start
54 static int rgMonthDays
[13] =
55 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
58 #include "wx/datetime.h"
60 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
61 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
62 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
63 #endif // wxUSE_TIMEDATE
65 static void ClearVariant(VARIANTARG
*pvarg
) ;
66 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
67 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
73 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
75 m_dispatchPtr
= dispatchPtr
;
78 wxAutomationObject::~wxAutomationObject()
82 ((IDispatch
*)m_dispatchPtr
)->Release();
87 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
89 // For Put/Get, no named arguments are allowed.
90 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
91 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
96 // nonConstMember is necessary because the wxString class doesn't have enough consts...
97 wxString
nonConstMember(member
);
99 int ch
= nonConstMember
.Find('.');
102 // Use dot notation to get the next object
103 wxString
member2(nonConstMember
.Left((size_t) ch
));
104 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
105 wxAutomationObject obj
;
106 if (!GetObject(obj
, member2
))
108 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
112 ClearVariant(& vReturn
);
114 VARIANTARG
* vReturnPtr
= & vReturn
;
116 // Find number of names args
117 int namedArgCount
= 0;
119 for (i
= 0; i
< noArgs
; i
++)
120 if (!INVOKEARG(i
).GetName().IsNull())
125 int namedArgStringCount
= namedArgCount
+ 1;
126 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
127 argNames
[0] = wxConvertStringToOle(member
);
129 // Note that arguments are specified in reverse order
130 // (all totally logical; hey, we're dealing with OLE here.)
133 for (i
= 0; i
< namedArgCount
; i
++)
135 if (!INVOKEARG(i
).GetName().IsNull())
137 argNames
[(namedArgCount
-j
)] = wxConvertStringToOle(INVOKEARG(i
).GetName());
142 // + 1 for the member name, + 1 again in case we're a 'put'
143 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
146 DISPPARAMS dispparams
;
147 unsigned int uiArgErr
;
150 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
151 // member name as the first name, followed by argument names (if any).
152 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
153 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
156 // ShowException(szMember, hr, NULL, 0);
162 // if doing a property put(ref), we need to adjust the first argument to have a
163 // named arg of DISPID_PROPERTYPUT.
164 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
167 dispIds
[1] = DISPID_PROPERTYPUT
;
168 vReturnPtr
= (VARIANTARG
*) NULL
;
171 // Convert the wxVariants to VARIANTARGs
172 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
173 for (i
= 0; i
< noArgs
; i
++)
175 // Again, reverse args
176 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
185 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
186 dispparams
.rgvarg
= oleArgs
;
187 dispparams
.cArgs
= noArgs
;
188 dispparams
.cNamedArgs
= namedArgCount
;
190 excep
.pfnDeferredFillIn
= NULL
;
192 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
193 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
195 for (i
= 0; i
< namedArgStringCount
; i
++)
197 SysFreeString(argNames
[i
]);
202 for (i
= 0; i
< noArgs
; i
++)
203 ReleaseVariant(& oleArgs
[i
]) ;
208 // display the exception information if appropriate:
209 // ShowException((const char*) member, hr, &excep, uiArgErr);
211 // free exception structure information
212 SysFreeString(excep
.bstrSource
);
213 SysFreeString(excep
.bstrDescription
);
214 SysFreeString(excep
.bstrHelpFile
);
217 ReleaseVariant(vReturnPtr
);
224 // Convert result to wxVariant form
225 wxConvertOleToVariant(vReturn
, retValue
);
226 // Mustn't release the dispatch pointer
227 if (vReturn
.vt
== VT_DISPATCH
)
229 vReturn
.pdispVal
= (IDispatch
*) NULL
;
231 ReleaseVariant(& vReturn
);
237 // Invoke a member function
238 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
240 wxVariant retVariant
;
241 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
243 retVariant
.MakeNull();
248 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
250 wxVariant retVariant
;
251 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
253 retVariant
.MakeNull();
258 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
259 const wxVariant
& arg1
, const wxVariant
& arg2
,
260 const wxVariant
& arg3
, const wxVariant
& arg4
,
261 const wxVariant
& arg5
, const wxVariant
& arg6
)
263 const wxVariant
** args
= new const wxVariant
*[6];
295 wxVariant retVariant
;
296 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
298 retVariant
.MakeNull();
305 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
307 wxVariant retVariant
;
308 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
310 retVariant
.MakeNull();
314 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
316 wxVariant retVariant
;
317 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
319 retVariant
.MakeNull();
324 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
325 const wxVariant
& arg1
, const wxVariant
& arg2
,
326 const wxVariant
& arg3
, const wxVariant
& arg4
,
327 const wxVariant
& arg5
, const wxVariant
& arg6
)
329 const wxVariant
** args
= new const wxVariant
*[6];
361 wxVariant retVariant
;
362 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
364 retVariant
.MakeNull();
370 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
372 wxVariant retVariant
;
373 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
380 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
382 wxVariant retVariant
;
383 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
390 bool wxAutomationObject::PutProperty(const wxString
& property
,
391 const wxVariant
& arg1
, const wxVariant
& arg2
,
392 const wxVariant
& arg3
, const wxVariant
& arg4
,
393 const wxVariant
& arg5
, const wxVariant
& arg6
)
395 const wxVariant
** args
= new const wxVariant
*[6];
427 wxVariant retVariant
;
428 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
434 // Uses DISPATCH_PROPERTYGET
435 // and returns a dispatch pointer. The calling code should call Release
436 // on the pointer, though this could be implicit by constructing an wxAutomationObject
437 // with it and letting the destructor call Release.
438 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
440 wxVariant retVariant
;
441 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
443 if (retVariant
.GetType() == wxT("void*"))
445 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
449 return (WXIDISPATCH
*) NULL
;
452 // Uses DISPATCH_PROPERTYGET
453 // and returns a dispatch pointer. The calling code should call Release
454 // on the pointer, though this could be implicit by constructing an wxAutomationObject
455 // with it and letting the destructor call Release.
456 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
458 wxVariant retVariant
;
459 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
461 if (retVariant
.GetType() == wxT("void*"))
463 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
467 return (WXIDISPATCH
*) NULL
;
471 // A way of initialising another wxAutomationObject with a dispatch object
472 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
474 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
477 obj
.SetDispatchPtr(dispatch
);
484 // A way of initialising another wxAutomationObject with a dispatch object
485 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
487 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
490 obj
.SetDispatchPtr(dispatch
);
497 // Get a dispatch pointer from the current object associated
499 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
505 IUnknown
* pUnk
= NULL
;
507 wxBasicString
unicodeName(classId
.mb_str());
509 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
511 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
515 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
517 wxLogWarning(wxT("Cannot find an active object"));
521 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
523 wxLogWarning(wxT("Cannot find IDispatch interface"));
530 // Get a dispatch pointer from a new object associated
531 // with the given class id
532 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
539 wxBasicString
unicodeName(classId
.mb_str());
541 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
543 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
547 // start a new copy of Excel, grab the IDispatch interface
548 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
550 wxLogWarning(wxT("Cannot start an instance of this class."));
558 bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
560 ClearVariant(&oleVariant
);
561 if (variant
.IsNull())
563 oleVariant
.vt
= VT_NULL
;
567 wxString
type(variant
.GetType());
570 if (type
== wxT("long"))
572 oleVariant
.vt
= VT_I4
;
573 oleVariant
.lVal
= variant
.GetLong() ;
575 // cVal not always present
577 else if (type
== wxT("char"))
579 oleVariant
.vt
=VT_I1
; // Signed Char
580 oleVariant
.cVal
=variant
.GetChar();
583 else if (type
== wxT("double"))
585 oleVariant
.vt
= VT_R8
;
586 oleVariant
.dblVal
= variant
.GetDouble();
588 else if (type
== wxT("bool"))
590 oleVariant
.vt
= VT_BOOL
;
591 // 'bool' required for VC++ 4 apparently
592 #if defined(__WATCOMC__) || (defined(__VISUALC__) && (__VISUALC__ <= 1000))
593 oleVariant
.bool = variant
.GetBool();
595 oleVariant
.boolVal
= variant
.GetBool();
598 else if (type
== wxT("string"))
600 wxString
str( variant
.GetString() );
601 oleVariant
.vt
= VT_BSTR
;
602 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
604 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
606 #if 0 // wxUSE_TIMEDATE && !defined(__WATCOMC__)
607 else if (type
== wxT("date"))
609 wxDate
date( variant
.GetDate() );
610 oleVariant
.vt
= VT_DATE
;
612 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
613 0, 0, 0, oleVariant
.date
))
616 else if (type
== wxT("time"))
618 wxTime
time( variant
.GetTime() );
619 oleVariant
.vt
= VT_DATE
;
621 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
622 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
627 else if (type
== wxT("datetime"))
629 wxDateTime
date( variant
.GetDateTime() );
630 oleVariant
.vt
= VT_DATE
;
632 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
633 date
.GetHour(), date
.GetMinute(), date
.GetSecond(), oleVariant
.date
))
637 else if (type
== wxT("void*"))
639 oleVariant
.vt
= VT_DISPATCH
;
640 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
642 else if (type
== wxT("list") || type
== wxT("stringlist"))
644 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
647 SAFEARRAYBOUND saBound
;
648 VARIANTARG
*pvargBase
;
652 int iCount
= variant
.GetCount();
655 saBound
.cElements
= iCount
;
657 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
661 SafeArrayAccessData(psa
, (void**)&pvargBase
);
664 for (i
= 0; i
< iCount
; i
++)
666 // copy each string in the list of strings
667 wxVariant
eachVariant(variant
[i
]);
668 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
670 // memory failure: back out and free strings alloc'ed up to
671 // now, and then the array itself.
673 for (j
= 0; j
< i
; j
++)
675 SysFreeString(pvarg
->bstrVal
);
678 SafeArrayDestroy(psa
);
684 SafeArrayUnaccessData(psa
);
686 oleVariant
.parray
= psa
;
690 oleVariant
.vt
= VT_NULL
;
697 #define VT_TYPEMASK 0xfff
700 bool wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
702 switch (oleVariant
.vt
& VT_TYPEMASK
)
706 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
714 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
717 wxDateTime
date(tmTemp
.tm_yday
, (wxDateTime::Month
) tmTemp
.tm_mon
, tmTemp
.tm_year
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
726 variant
= (long) oleVariant
.lVal
;
731 variant
= (long) oleVariant
.iVal
;
737 #if defined(__WATCOMC__) || (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
738 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
739 variant
= (long) (oleVariant
.bool != 0);
741 variant
= (bool) (oleVariant
.bool != 0);
744 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
745 variant
= (long) (oleVariant
.boolVal
!= 0);
747 variant
= (bool) (oleVariant
.boolVal
!= 0);
754 variant
= oleVariant
.dblVal
;
761 int cDims
, cElements
, i
;
764 // Iterate the dimensions: number of elements is x*y*z
765 for (cDims
= 0, cElements
= 1;
766 cDims
< oleVariant
.parray
->cDims
; cDims
++)
767 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
769 // Get a pointer to the data
770 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
774 for (i
= 0; i
< cElements
; i
++)
776 VARIANTARG
& oleElement
= pvdata
[i
];
778 if (!wxConvertOleToVariant(oleElement
, vElement
))
781 variant
.Append(vElement
);
783 SafeArrayUnaccessData(oleVariant
.parray
);
788 variant
= (void*) oleVariant
.pdispVal
;
798 break; // Ignore Empty Variant, used only during destruction of objects
802 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
809 BSTR
wxConvertStringToOle(const wxString
& str
)
812 unsigned int len = strlen((const char*) str);
813 unsigned short* s = new unsigned short[len*2+2];
815 memset(s, 0, len*2+2);
816 for (i=0; i < len; i++)
819 wxBasicString
bstr(str
.mb_str());
823 wxString
wxConvertStringFromOle(BSTR bStr
)
828 int len
= SysStringLen(bStr
) + 1;
829 char *buf
= new char[len
];
830 (void)wcstombs( buf
, bStr
, len
);
837 // ----------------------------------------------------------------------------
839 // ----------------------------------------------------------------------------
841 // ctor takes an ANSI string and transforms it to Unicode
842 wxBasicString::wxBasicString(const char *sz
)
847 // ctor takes an ANSI or Unicode string and transforms it to Unicode
848 wxBasicString::wxBasicString(const wxString
& str
)
851 m_wzBuf
= new OLECHAR
[str
.Length() + 1];
852 memcpy(m_wzBuf
, str
.c_str(), str
.Length()*2);
853 m_wzBuf
[str
.Length()] = L
'\0';
859 // Takes an ANSI string and transforms it to Unicode
860 void wxBasicString::Init(const char *sz
)
862 // get the size of required buffer
863 UINT lenAnsi
= strlen(sz
);
865 UINT lenWide
= lenAnsi
* 2 ;
867 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
871 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
872 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
873 m_wzBuf
[lenWide
] = L
'\0';
881 wxBasicString::~wxBasicString()
886 /////////////////////////////////////////////////////////////////////////////
887 // COleDateTime class HELPERS - implementation
889 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
890 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
892 // Validate year and month (ignore day of week and milliseconds)
893 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
896 // Check for leap year and set the number of days in the month
897 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
898 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
901 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
902 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
904 // Finish validating the date
905 if (wDay
< 1 || wDay
> nDaysInMonth
||
906 wHour
> 23 || wMinute
> 59 ||
912 // Cache the date in days and time in fractional days
916 //It is a valid date; make Jan 1, 1AD be 1
917 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
918 rgMonthDays
[wMonth
-1] + wDay
;
920 // If leap year and it's before March, subtract 1:
921 if (wMonth
<= 2 && bLeapYear
)
924 // Offset so that 12/30/1899 is 0
927 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
928 ((long)wMinute
* 60L) + // mins in seconds
929 ((long)wSecond
)) / 86400.;
931 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
936 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
938 // The legal range does not actually span year 0 to 9999.
939 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
942 long nDays
; // Number of days since Dec. 30, 1899
943 long nDaysAbsolute
; // Number of days since 1/1/0
944 long nSecsInDay
; // Time in seconds since midnight
945 long nMinutesInDay
; // Minutes in day
947 long n400Years
; // Number of 400 year increments since 1/1/0
948 long n400Century
; // Century within 400 year block (0,1,2 or 3)
949 long n4Years
; // Number of 4 year increments since 1/1/0
950 long n4Day
; // Day within 4 year block
951 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
952 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
953 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
955 double dblDate
= dtSrc
; // tempory serial date
957 // If a valid date, then this conversion should not overflow
958 nDays
= (long)dblDate
;
960 // Round to the second
961 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
963 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
965 dblDate
= fabs(dblDate
);
966 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
968 // Calculate the day of week (sun=1, mon=2...)
969 // -1 because 1/1/0 is Sat. +1 because we want 1-based
970 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
972 // Leap years every 4 yrs except centuries not multiples of 400.
973 n400Years
= (long)(nDaysAbsolute
/ 146097L);
975 // Set nDaysAbsolute to day within 400-year block
976 nDaysAbsolute
%= 146097L;
978 // -1 because first century has extra day
979 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
982 if (n400Century
!= 0)
984 // Set nDaysAbsolute to day within century
985 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
987 // +1 because 1st 4 year increment has 1460 days
988 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
991 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
995 n4Day
= (long)nDaysAbsolute
;
1000 // Leap century - not special case!
1001 n4Years
= (long)(nDaysAbsolute
/ 1461L);
1002 n4Day
= (long)(nDaysAbsolute
% 1461L);
1007 // -1 because first year has 366 days
1008 n4Yr
= (n4Day
- 1) / 365;
1011 n4Day
= (n4Day
- 1) % 365;
1019 // n4Day is now 0-based day of year. Save 1-based day of year, year number
1020 tmDest
.tm_yday
= (int)n4Day
+ 1;
1021 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
1023 // Handle leap year: before, on, and after Feb. 29.
1024 if (n4Yr
== 0 && bLeap4
)
1031 tmDest
.tm_mday
= 29;
1035 // Pretend it's not a leap year for month/day comp.
1040 // Make n4DaY a 1-based day of non-leap year and compute
1041 // month/day for everything but Feb. 29.
1044 // Month number always >= n/32, so save some loop time */
1045 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
1046 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
1048 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
1051 if (nSecsInDay
== 0)
1052 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
1055 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
1056 nMinutesInDay
= nSecsInDay
/ 60L;
1057 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
1058 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
1064 // this function is not used
1066 void TmConvertToStandardFormat(struct tm
& tmSrc
)
1068 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
1069 tmSrc
.tm_year
-= 1900; // year is based on 1900
1070 tmSrc
.tm_mon
-= 1; // month of year is 0-based
1071 tmSrc
.tm_wday
-= 1; // day of week is 0-based
1072 tmSrc
.tm_yday
-= 1; // day of year is 0-based
1075 double DoubleFromDate(DATE dt
)
1077 // No problem if positive
1081 // If negative, must convert since negative dates not continuous
1082 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
1083 double temp
= ceil(dt
);
1084 return temp
- (dt
- temp
);
1087 DATE
DateFromDouble(double dbl
)
1089 // No problem if positive
1093 // If negative, must convert since negative dates not continuous
1094 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1095 double temp
= floor(dbl
); // dbl is now whole part
1096 return temp
+ (temp
- dbl
);
1103 * Zeros a variant structure without regard to current contents
1105 static void ClearVariant(VARIANTARG
*pvarg
)
1107 pvarg
->vt
= VT_EMPTY
;
1108 pvarg
->wReserved1
= 0;
1109 pvarg
->wReserved2
= 0;
1110 pvarg
->wReserved3
= 0;
1117 * Clears a particular variant structure and releases any external objects
1118 * or memory contained in the variant. Supports the data types listed above.
1120 static void ReleaseVariant(VARIANTARG
*pvarg
)
1123 VARIANTARG _huge
*pvargArray
;
1124 long lLBound
, lUBound
, l
;
1126 vt
= pvarg
->vt
& 0xfff; // mask off flags
1128 // check if an array. If so, free its contents, then the array itself.
1129 if (V_ISARRAY(pvarg
))
1131 // variant arrays are all this routine currently knows about. Since a
1132 // variant can contain anything (even other arrays), call ourselves
1134 if (vt
== VT_VARIANT
)
1136 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1137 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1139 if (lUBound
> lLBound
)
1143 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1145 for (l
= 0; l
< lUBound
; l
++)
1147 ReleaseVariant(pvargArray
);
1151 SafeArrayUnaccessData(pvarg
->parray
);
1156 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1159 // Free the array itself.
1160 SafeArrayDestroy(pvarg
->parray
);
1167 if (pvarg
->pdispVal
)
1168 pvarg
->pdispVal
->Release();
1172 SysFreeString(pvarg
->bstrVal
);
1178 case VT_ERROR
: // to avoid erroring on an error return from Excel
1179 // no work for these types
1183 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1188 ClearVariant(pvarg
);
1193 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1197 switch (GetScode(hr
))
1199 case DISP_E_UNKNOWNNAME
:
1200 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1203 case DISP_E_BADPARAMCOUNT
:
1204 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1207 case DISP_E_EXCEPTION
:
1208 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1209 if (pexcep
->bstrDescription
!= NULL
)
1210 lstrcat(szBuf
, pexcep
->bstrDescription
);
1212 lstrcat(szBuf
, L
"<<No Description>>");
1215 case DISP_E_MEMBERNOTFOUND
:
1216 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1219 case DISP_E_OVERFLOW
:
1220 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1223 case DISP_E_NONAMEDARGS
:
1224 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1228 case DISP_E_UNKNOWNLCID
:
1229 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1232 case DISP_E_PARAMNOTOPTIONAL
:
1233 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1236 case DISP_E_PARAMNOTFOUND
:
1237 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1240 case DISP_E_TYPEMISMATCH
:
1241 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1245 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1249 wxLogWarning(szBuf
);
1254 #endif // __WATCOMC__