]>
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__)
28 // Watcom C++ gives a linker error if this is compiled in.
31 #include "wx/msw/ole/automtn.h"
33 #include "wx/msw/private.h"
42 // wrapper around BSTR type (by Vadim Zeitlin)
44 class WXDLLEXPORT BasicString
48 BasicString(const char *sz
);
52 // just get the string
53 operator BSTR() const { return m_wzBuf
; }
54 // retrieve a copy of our string - caller must SysFreeString() it later!
55 BSTR
Get() const { return SysAllocString(m_wzBuf
); }
58 // @@@ not implemented (but should be)
59 BasicString(const BasicString
&);
60 BasicString
& operator=(const BasicString
&);
62 OLECHAR
*m_wzBuf
; // actual string
66 static bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
) ;
67 static bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
) ;
69 // Convert string to Unicode
70 static BSTR
ConvertStringToOle(const wxString
& str
);
72 // Convert string from BSTR to wxString
73 static wxString
ConvertStringFromOle(BSTR bStr
);
75 // Verifies will fail if the needed buffer size is too large
76 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
77 #define MIN_DATE (-657434L) // about year 100
78 #define MAX_DATE 2958465L // about year 9999
80 // Half a second, expressed in days
81 #define HALF_SECOND (1.0/172800.0)
83 // One-based array of days in year at month start
84 static int rgMonthDays
[13] =
85 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
87 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
88 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
89 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
91 static void ClearVariant(VARIANTARG
*pvarg
) ;
92 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
93 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
99 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
101 m_dispatchPtr
= dispatchPtr
;
104 wxAutomationObject::~wxAutomationObject()
108 ((IDispatch
*)m_dispatchPtr
)->Release();
109 m_dispatchPtr
= NULL
;
113 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
115 // For Put/Get, no named arguments are allowed.
116 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
117 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
122 // nonConstMember is necessary because the wxString class doesn't have enough consts...
123 wxString
nonConstMember(member
);
125 int ch
= nonConstMember
.Find('.');
128 // Use dot notation to get the next object
129 wxString
member2(nonConstMember
.Left((size_t) ch
));
130 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
131 wxAutomationObject obj
;
132 if (!GetObject(obj
, member2
))
134 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
138 ClearVariant(& vReturn
);
140 VARIANTARG
* vReturnPtr
= & vReturn
;
142 // Find number of names args
143 int namedArgCount
= 0;
145 for (i
= 0; i
< noArgs
; i
++)
146 if (!INVOKEARG(i
).GetName().IsNull())
151 int namedArgStringCount
= namedArgCount
+ 1;
152 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
153 argNames
[0] = ConvertStringToOle(member
);
155 // Note that arguments are specified in reverse order
156 // (all totally logical; hey, we're dealing with OLE here.)
159 for (i
= 0; i
< namedArgCount
; i
++)
161 if (!INVOKEARG(i
).GetName().IsNull())
163 argNames
[(namedArgCount
-j
)] = ConvertStringToOle(INVOKEARG(i
).GetName());
168 // + 1 for the member name, + 1 again in case we're a 'put'
169 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
172 DISPPARAMS dispparams
;
173 unsigned int uiArgErr
;
176 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
177 // member name as the first name, followed by argument names (if any).
178 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
179 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
182 // ShowException(szMember, hr, NULL, 0);
186 // if doing a property put(ref), we need to adjust the first argument to have a
187 // named arg of DISPID_PROPERTYPUT.
188 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
191 dispIds
[1] = DISPID_PROPERTYPUT
;
192 vReturnPtr
= (VARIANTARG
*) NULL
;
195 // Convert the wxVariants to VARIANTARGs
196 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
197 for (i
= 0; i
< noArgs
; i
++)
199 // Again, reverse args
200 if (!ConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
201 return FALSE
; // TODO: clean up memory at this point
204 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
205 dispparams
.rgvarg
= oleArgs
;
206 dispparams
.cArgs
= noArgs
;
207 dispparams
.cNamedArgs
= namedArgCount
;
209 excep
.pfnDeferredFillIn
= NULL
;
211 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
212 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
214 for (i
= 0; i
< namedArgStringCount
; i
++)
216 SysFreeString(argNames
[i
]);
221 for (i
= 0; i
< noArgs
; i
++)
222 ReleaseVariant(& oleArgs
[i
]) ;
227 // display the exception information if appropriate:
228 // ShowException((const char*) member, hr, &excep, uiArgErr);
230 // free exception structure information
231 SysFreeString(excep
.bstrSource
);
232 SysFreeString(excep
.bstrDescription
);
233 SysFreeString(excep
.bstrHelpFile
);
236 ReleaseVariant(vReturnPtr
);
243 // Convert result to wxVariant form
244 ConvertOleToVariant(vReturn
, retValue
);
245 // Mustn't release the dispatch pointer
246 if (vReturn
.vt
== VT_DISPATCH
)
248 vReturn
.pdispVal
= (IDispatch
*) NULL
;
250 ReleaseVariant(& vReturn
);
256 // Invoke a member function
257 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
259 wxVariant retVariant
;
260 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
262 retVariant
.MakeNull();
267 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
268 const wxVariant
& arg1
, const wxVariant
& arg2
,
269 const wxVariant
& arg3
, const wxVariant
& arg4
,
270 const wxVariant
& arg5
, const wxVariant
& arg6
)
272 const wxVariant
** args
= new const wxVariant
*[6];
304 wxVariant retVariant
;
305 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
307 retVariant
.MakeNull();
314 wxVariant
wxAutomationObject::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::PutProperty(const wxString
& property
,
381 const wxVariant
& arg1
, const wxVariant
& arg2
,
382 const wxVariant
& arg3
, const wxVariant
& arg4
,
383 const wxVariant
& arg5
, const wxVariant
& arg6
)
385 const wxVariant
** args
= new const wxVariant
*[6];
417 wxVariant retVariant
;
418 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
424 // Uses DISPATCH_PROPERTYGET
425 // and returns a dispatch pointer. The calling code should call Release
426 // on the pointer, though this could be implicit by constructing an wxAutomationObject
427 // with it and letting the destructor call Release.
428 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
430 wxVariant retVariant
;
431 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
433 if (retVariant
.GetType() == wxT("void*"))
435 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
439 return (WXIDISPATCH
*) NULL
;
443 return (WXIDISPATCH
*) NULL
;
446 // A way of initialising another wxAutomationObject with a dispatch object
447 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
449 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
452 obj
.SetDispatchPtr(dispatch
);
459 // Get a dispatch pointer from the current object associated
461 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
467 IUnknown
* pUnk
= NULL
;
469 BasicString
unicodeName(classId
.mb_str());
471 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
473 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
477 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
479 wxLogWarning(wxT("Cannot find an active object"));
483 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
485 wxLogWarning(wxT("Cannot find IDispatch interface"));
492 // Get a dispatch pointer from a new object associated
493 // with the given class id
494 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
501 BasicString
unicodeName(classId
.mb_str());
503 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
505 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
509 // start a new copy of Excel, grab the IDispatch interface
510 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
512 wxLogWarning(wxT("Cannot start an instance of this class."));
520 bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
522 ClearVariant(&oleVariant
);
523 if (variant
.IsNull())
525 oleVariant
.vt
= VT_NULL
;
529 wxString
type(variant
.GetType());
531 if (type
== wxT("long"))
533 oleVariant
.vt
= VT_I4
;
534 oleVariant
.lVal
= variant
.GetLong() ;
536 else if (type
== wxT("double"))
538 oleVariant
.vt
= VT_R8
;
539 oleVariant
.dblVal
= variant
.GetDouble();
541 else if (type
== wxT("bool"))
543 oleVariant
.vt
= VT_BOOL
;
544 // 'bool' required for VC++ 4 apparently
545 #if defined(__WATCOMC__) || (defined(__VISUALC__) && (__VISUALC__ <= 1000))
546 oleVariant
.bool = variant
.GetBool();
548 oleVariant
.boolVal
= variant
.GetBool();
551 else if (type
== wxT("string"))
553 wxString
str( variant
.GetString() );
554 oleVariant
.vt
= VT_BSTR
;
555 oleVariant
.bstrVal
= ConvertStringToOle(str
);
557 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
558 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
559 else if (type
== wxT("date"))
561 wxDate
date( variant
.GetDate() );
562 oleVariant
.vt
= VT_DATE
;
564 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
565 0, 0, 0, oleVariant
.date
))
568 else if (type
== wxT("time"))
570 wxTime
time( variant
.GetTime() );
571 oleVariant
.vt
= VT_DATE
;
573 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
574 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
578 else if (type
== wxT("void*"))
580 oleVariant
.vt
= VT_DISPATCH
;
581 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
583 else if (type
== wxT("list") || type
== wxT("stringlist"))
585 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
588 SAFEARRAYBOUND saBound
;
589 VARIANTARG
*pvargBase
;
593 int iCount
= variant
.GetCount();
596 saBound
.cElements
= iCount
;
598 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
602 SafeArrayAccessData(psa
, (void**)&pvargBase
);
605 for (i
= 0; i
< iCount
; i
++)
607 // copy each string in the list of strings
608 wxVariant
eachVariant(variant
[i
]);
609 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
611 // memory failure: back out and free strings alloc'ed up to
612 // now, and then the array itself.
614 for (j
= 0; j
< i
; j
++)
616 SysFreeString(pvarg
->bstrVal
);
619 SafeArrayDestroy(psa
);
625 SafeArrayUnaccessData(psa
);
627 oleVariant
.parray
= psa
;
631 oleVariant
.vt
= VT_NULL
;
638 #define VT_TYPEMASK 0xfff
641 bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
643 switch (oleVariant
.vt
& VT_TYPEMASK
)
647 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
654 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
657 wxDate
date(tmTemp
.tm_yday
, tmTemp
.tm_mon
, tmTemp
.tm_year
);
658 wxTime
time(date
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
665 variant
= (long) oleVariant
.lVal
;
670 variant
= (long) oleVariant
.iVal
;
676 #if defined(__WATCOMC__) || (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
677 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
678 variant
= (long) (oleVariant
.bool != 0);
680 variant
= (bool) (oleVariant
.bool != 0);
683 variant
= (bool) (oleVariant
.boolVal
!= 0);
689 variant
= oleVariant
.dblVal
;
696 int cDims
, cElements
, i
;
699 // Iterate the dimensions: number of elements is x*y*z
700 for (cDims
= 0, cElements
= 1;
701 cDims
< oleVariant
.parray
->cDims
; cDims
++)
702 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
704 // Get a pointer to the data
705 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
709 for (i
= 0; i
< cElements
; i
++)
711 VARIANTARG
& oleElement
= pvdata
[i
];
713 if (!ConvertOleToVariant(oleElement
, vElement
))
716 variant
.Append(vElement
);
718 SafeArrayUnaccessData(oleVariant
.parray
);
723 variant
= (void*) oleVariant
.pdispVal
;
733 break; // Ignore Empty Variant, used only during destruction of objects
737 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
744 static BSTR
ConvertStringToOle(const wxString
& str
)
747 unsigned int len = strlen((const char*) str);
748 unsigned short* s = new unsigned short[len*2+2];
750 memset(s, 0, len*2+2);
751 for (i=0; i < len; i++)
754 BasicString
bstr(str
.mb_str());
758 static wxString
ConvertStringFromOle(BSTR bStr
)
760 int len
= SysStringLen(bStr
) + 1;
761 char *buf
= new char[len
];
762 (void)wcstombs( buf
, bStr
, len
);
769 // ----------------------------------------------------------------------------
771 // ----------------------------------------------------------------------------
773 // ctor takes an ANSI string and transforms it to Unicode
774 BasicString::BasicString(const char *sz
)
776 // get the size of required buffer
777 UINT lenAnsi
= strlen(sz
);
779 UINT lenWide
= lenAnsi
* 2 ;
781 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
785 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
786 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
787 m_wzBuf
[lenWide
] = L
'\0';
795 BasicString::~BasicString()
800 /////////////////////////////////////////////////////////////////////////////
801 // COleDateTime class HELPERS - implementation
803 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
804 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
806 // Validate year and month (ignore day of week and milliseconds)
807 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
810 // Check for leap year and set the number of days in the month
811 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
812 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
815 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
816 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
818 // Finish validating the date
819 if (wDay
< 1 || wDay
> nDaysInMonth
||
820 wHour
> 23 || wMinute
> 59 ||
826 // Cache the date in days and time in fractional days
830 //It is a valid date; make Jan 1, 1AD be 1
831 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
832 rgMonthDays
[wMonth
-1] + wDay
;
834 // If leap year and it's before March, subtract 1:
835 if (wMonth
<= 2 && bLeapYear
)
838 // Offset so that 12/30/1899 is 0
841 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
842 ((long)wMinute
* 60L) + // mins in seconds
843 ((long)wSecond
)) / 86400.;
845 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
850 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
852 // The legal range does not actually span year 0 to 9999.
853 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
856 long nDays
; // Number of days since Dec. 30, 1899
857 long nDaysAbsolute
; // Number of days since 1/1/0
858 long nSecsInDay
; // Time in seconds since midnight
859 long nMinutesInDay
; // Minutes in day
861 long n400Years
; // Number of 400 year increments since 1/1/0
862 long n400Century
; // Century within 400 year block (0,1,2 or 3)
863 long n4Years
; // Number of 4 year increments since 1/1/0
864 long n4Day
; // Day within 4 year block
865 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
866 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
867 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
869 double dblDate
= dtSrc
; // tempory serial date
871 // If a valid date, then this conversion should not overflow
872 nDays
= (long)dblDate
;
874 // Round to the second
875 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
877 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
879 dblDate
= fabs(dblDate
);
880 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
882 // Calculate the day of week (sun=1, mon=2...)
883 // -1 because 1/1/0 is Sat. +1 because we want 1-based
884 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
886 // Leap years every 4 yrs except centuries not multiples of 400.
887 n400Years
= (long)(nDaysAbsolute
/ 146097L);
889 // Set nDaysAbsolute to day within 400-year block
890 nDaysAbsolute
%= 146097L;
892 // -1 because first century has extra day
893 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
896 if (n400Century
!= 0)
898 // Set nDaysAbsolute to day within century
899 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
901 // +1 because 1st 4 year increment has 1460 days
902 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
905 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
909 n4Day
= (long)nDaysAbsolute
;
914 // Leap century - not special case!
915 n4Years
= (long)(nDaysAbsolute
/ 1461L);
916 n4Day
= (long)(nDaysAbsolute
% 1461L);
921 // -1 because first year has 366 days
922 n4Yr
= (n4Day
- 1) / 365;
925 n4Day
= (n4Day
- 1) % 365;
933 // n4Day is now 0-based day of year. Save 1-based day of year, year number
934 tmDest
.tm_yday
= (int)n4Day
+ 1;
935 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
937 // Handle leap year: before, on, and after Feb. 29.
938 if (n4Yr
== 0 && bLeap4
)
949 // Pretend it's not a leap year for month/day comp.
954 // Make n4DaY a 1-based day of non-leap year and compute
955 // month/day for everything but Feb. 29.
958 // Month number always >= n/32, so save some loop time */
959 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
960 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
962 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
966 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
969 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
970 nMinutesInDay
= nSecsInDay
/ 60L;
971 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
972 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
978 // this function is not used
980 void TmConvertToStandardFormat(struct tm
& tmSrc
)
982 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
983 tmSrc
.tm_year
-= 1900; // year is based on 1900
984 tmSrc
.tm_mon
-= 1; // month of year is 0-based
985 tmSrc
.tm_wday
-= 1; // day of week is 0-based
986 tmSrc
.tm_yday
-= 1; // day of year is 0-based
989 double DoubleFromDate(DATE dt
)
991 // No problem if positive
995 // If negative, must convert since negative dates not continuous
996 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
997 double temp
= ceil(dt
);
998 return temp
- (dt
- temp
);
1001 DATE
DateFromDouble(double dbl
)
1003 // No problem if positive
1007 // If negative, must convert since negative dates not continuous
1008 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1009 double temp
= floor(dbl
); // dbl is now whole part
1010 return temp
+ (temp
- dbl
);
1017 * Zeros a variant structure without regard to current contents
1019 static void ClearVariant(VARIANTARG
*pvarg
)
1021 pvarg
->vt
= VT_EMPTY
;
1022 pvarg
->wReserved1
= 0;
1023 pvarg
->wReserved2
= 0;
1024 pvarg
->wReserved3
= 0;
1031 * Clears a particular variant structure and releases any external objects
1032 * or memory contained in the variant. Supports the data types listed above.
1034 static void ReleaseVariant(VARIANTARG
*pvarg
)
1037 VARIANTARG _huge
*pvargArray
;
1038 long lLBound
, lUBound
, l
;
1040 vt
= pvarg
->vt
& 0xfff; // mask off flags
1042 // check if an array. If so, free its contents, then the array itself.
1043 if (V_ISARRAY(pvarg
))
1045 // variant arrays are all this routine currently knows about. Since a
1046 // variant can contain anything (even other arrays), call ourselves
1048 if (vt
== VT_VARIANT
)
1050 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1051 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1053 if (lUBound
> lLBound
)
1057 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1059 for (l
= 0; l
< lUBound
; l
++)
1061 ReleaseVariant(pvargArray
);
1065 SafeArrayUnaccessData(pvarg
->parray
);
1070 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
1073 // Free the array itself.
1074 SafeArrayDestroy(pvarg
->parray
);
1081 if (pvarg
->pdispVal
)
1082 pvarg
->pdispVal
->Release();
1086 SysFreeString(pvarg
->bstrVal
);
1092 case VT_ERROR
: // to avoid erroring on an error return from Excel
1093 // no work for these types
1097 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
1102 ClearVariant(pvarg
);
1107 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1111 switch (GetScode(hr
))
1113 case DISP_E_UNKNOWNNAME
:
1114 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1117 case DISP_E_BADPARAMCOUNT
:
1118 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1121 case DISP_E_EXCEPTION
:
1122 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1123 if (pexcep
->bstrDescription
!= NULL
)
1124 lstrcat(szBuf
, pexcep
->bstrDescription
);
1126 lstrcat(szBuf
, L
"<<No Description>>");
1129 case DISP_E_MEMBERNOTFOUND
:
1130 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1133 case DISP_E_OVERFLOW
:
1134 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1137 case DISP_E_NONAMEDARGS
:
1138 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1142 case DISP_E_UNKNOWNLCID
:
1143 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1146 case DISP_E_PARAMNOTOPTIONAL
:
1147 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1150 case DISP_E_PARAMNOTFOUND
:
1151 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1154 case DISP_E_TYPEMISMATCH
:
1155 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1159 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1163 wxLogWarning(szBuf
);
1168 #endif // __WATCOMC__