]>
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 #include "wx/msw/ole/automtn.h"
30 #include "wx/msw/private.h"
35 // wrapper around BSTR type (by Vadim Zeitlin)
37 class WXDLLEXPORT BasicString
41 BasicString(const char *sz
);
45 // just get the string
46 operator BSTR() const { return m_wzBuf
; }
47 // retrieve a copy of our string - caller must SysFreeString() it later!
48 BSTR
Get() const { return SysAllocString(m_wzBuf
); }
51 // @@@ not implemented (but should be)
52 BasicString(const BasicString
&);
53 BasicString
& operator=(const BasicString
&);
55 OLECHAR
*m_wzBuf
; // actual string
59 static bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
) ;
60 static bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
) ;
62 // Convert string to Unicode
63 static BSTR
ConvertStringToOle(const wxString
& str
);
65 // Convert string from BSTR to wxString
66 static wxString
ConvertStringFromOle(BSTR bStr
);
68 // Verifies will fail if the needed buffer size is too large
69 #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
70 #define MIN_DATE (-657434L) // about year 100
71 #define MAX_DATE 2958465L // about year 9999
73 // Half a second, expressed in days
74 #define HALF_SECOND (1.0/172800.0)
76 // One-based array of days in year at month start
77 static int rgMonthDays
[13] =
78 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
80 static BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
81 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
);
82 static BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
);
83 static void TmConvertToStandardFormat(struct tm
& tmSrc
);
84 static double DoubleFromDate(DATE dt
);
85 static DATE
DateFromDouble(double dbl
);
87 static void ClearVariant(VARIANTARG
*pvarg
) ;
88 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
89 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
95 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
97 m_dispatchPtr
= dispatchPtr
;
100 wxAutomationObject::~wxAutomationObject()
104 ((IDispatch
*)m_dispatchPtr
)->Release();
105 m_dispatchPtr
= NULL
;
109 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
111 // For Put/Get, no named arguments are allowed.
112 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
113 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
118 // nonConstMember is necessary because the wxString class doesn't have enough consts...
119 wxString
nonConstMember(member
);
121 int ch
= nonConstMember
.Find('.');
124 // Use dot notation to get the next object
125 wxString
member2(nonConstMember
.Left((size_t) ch
));
126 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
127 wxAutomationObject obj
;
128 if (!GetObject(obj
, member2
))
130 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
134 ClearVariant(& vReturn
);
136 VARIANTARG
* vReturnPtr
= & vReturn
;
138 // Find number of names args
139 int namedArgCount
= 0;
141 for (i
= 0; i
< noArgs
; i
++)
142 if (!INVOKEARG(i
).GetName().IsNull())
147 int namedArgStringCount
= namedArgCount
+ 1;
148 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
149 argNames
[0] = ConvertStringToOle(member
);
151 // Note that arguments are specified in reverse order
152 // (all totally logical; hey, we're dealing with OLE here.)
155 for (i
= 0; i
< namedArgCount
; i
++)
157 if (!INVOKEARG(i
).GetName().IsNull())
159 argNames
[(namedArgCount
-j
)] = ConvertStringToOle(INVOKEARG(i
).GetName());
164 // + 1 for the member name, + 1 again in case we're a 'put'
165 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
168 DISPPARAMS dispparams
;
169 unsigned int uiArgErr
;
172 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
173 // member name as the first name, followed by argument names (if any).
174 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
175 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
178 // ShowException(szMember, hr, NULL, 0);
182 // if doing a property put(ref), we need to adjust the first argument to have a
183 // named arg of DISPID_PROPERTYPUT.
184 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
187 dispIds
[1] = DISPID_PROPERTYPUT
;
188 vReturnPtr
= (VARIANTARG
*) NULL
;
191 // Convert the wxVariants to VARIANTARGs
192 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
193 for (i
= 0; i
< noArgs
; i
++)
195 // Again, reverse args
196 if (!ConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
197 return FALSE
; // TODO: clean up memory at this point
200 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
201 dispparams
.rgvarg
= oleArgs
;
202 dispparams
.cArgs
= noArgs
;
203 dispparams
.cNamedArgs
= namedArgCount
;
205 excep
.pfnDeferredFillIn
= NULL
;
207 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
208 action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
210 for (i
= 0; i
< namedArgStringCount
; i
++)
212 SysFreeString(argNames
[i
]);
217 for (i
= 0; i
< noArgs
; i
++)
218 ReleaseVariant(& oleArgs
[i
]) ;
223 // display the exception information if appropriate:
224 // ShowException((const char*) member, hr, &excep, uiArgErr);
226 // free exception structure information
227 SysFreeString(excep
.bstrSource
);
228 SysFreeString(excep
.bstrDescription
);
229 SysFreeString(excep
.bstrHelpFile
);
232 ReleaseVariant(vReturnPtr
);
239 // Convert result to wxVariant form
240 ConvertOleToVariant(vReturn
, retValue
);
241 // Mustn't release the dispatch pointer
242 if (vReturn
.vt
== VT_DISPATCH
)
244 vReturn
.pdispVal
= (IDispatch
*) NULL
;
246 ReleaseVariant(& vReturn
);
252 // Invoke a member function
253 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
255 wxVariant retVariant
;
256 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
258 retVariant
.MakeNull();
263 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
264 const wxVariant
& arg1
, const wxVariant
& arg2
,
265 const wxVariant
& arg3
, const wxVariant
& arg4
,
266 const wxVariant
& arg5
, const wxVariant
& arg6
)
268 const wxVariant
** args
= new const wxVariant
*[6];
300 wxVariant retVariant
;
301 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
303 retVariant
.MakeNull();
310 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
312 wxVariant retVariant
;
313 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
315 retVariant
.MakeNull();
320 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
321 const wxVariant
& arg1
, const wxVariant
& arg2
,
322 const wxVariant
& arg3
, const wxVariant
& arg4
,
323 const wxVariant
& arg5
, const wxVariant
& arg6
)
325 const wxVariant
** args
= new const wxVariant
*[6];
357 wxVariant retVariant
;
358 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
360 retVariant
.MakeNull();
366 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
368 wxVariant retVariant
;
369 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
376 bool wxAutomationObject::PutProperty(const wxString
& property
,
377 const wxVariant
& arg1
, const wxVariant
& arg2
,
378 const wxVariant
& arg3
, const wxVariant
& arg4
,
379 const wxVariant
& arg5
, const wxVariant
& arg6
)
381 const wxVariant
** args
= new const wxVariant
*[6];
413 wxVariant retVariant
;
414 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
420 // Uses DISPATCH_PROPERTYGET
421 // and returns a dispatch pointer. The calling code should call Release
422 // on the pointer, though this could be implicit by constructing an wxAutomationObject
423 // with it and letting the destructor call Release.
424 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
426 wxVariant retVariant
;
427 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
429 if (retVariant
.GetType() == "void*")
431 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
435 return (WXIDISPATCH
*) NULL
;
439 return (WXIDISPATCH
*) NULL
;
442 // A way of initialising another wxAutomationObject with a dispatch object
443 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
445 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
448 obj
.SetDispatchPtr(dispatch
);
455 // Get a dispatch pointer from the current object associated
457 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
463 IUnknown
* pUnk
= NULL
;
465 BasicString
unicodeName((const char*) classId
);
467 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
469 wxLogWarning("Cannot obtain CLSID from ProgID");
473 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
475 wxLogWarning("Cannot find an active object");
479 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
481 wxLogWarning("Cannot find IDispatch interface");
488 // Get a dispatch pointer from a new object associated
489 // with the given class id
490 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
496 IUnknown
* pUnk
= NULL
;
498 BasicString
unicodeName((const char*) classId
);
500 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
502 wxLogWarning("Cannot obtain CLSID from ProgID");
506 // start a new copy of Excel, grab the IDispatch interface
507 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
509 wxLogWarning("Cannot start an instance of this class.");
517 bool ConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
519 ClearVariant(&oleVariant
);
520 if (variant
.IsNull())
522 oleVariant
.vt
= VT_NULL
;
526 wxString
type(variant
.GetType());
530 oleVariant
.vt
= VT_I4
;
531 oleVariant
.lVal
= variant
.GetLong() ;
533 else if (type
== "double")
535 oleVariant
.vt
= VT_R8
;
536 oleVariant
.dblVal
= variant
.GetDouble();
538 else if (type
== "bool")
540 oleVariant
.vt
= VT_BOOL
;
541 // 'bool' required for VC++ 4 apparently
542 #if defined(__WATCOMC__) || (defined(__VISUALC__) && (__VISUALC__ <= 1000))
543 oleVariant
.bool = variant
.GetBool();
545 oleVariant
.boolVal
= variant
.GetBool();
548 else if (type
== "string")
550 wxString
str( variant
.GetString() );
551 oleVariant
.vt
= VT_BSTR
;
552 oleVariant
.bstrVal
= ConvertStringToOle(str
);
554 else if (type
== "date")
556 wxDate
date( variant
.GetDate() );
557 oleVariant
.vt
= VT_DATE
;
559 if (!OleDateFromTm(date
.GetYear(), date
.GetMonth(), date
.GetDay(),
560 0, 0, 0, oleVariant
.date
))
563 else if (type
== "time")
565 wxTime
time( variant
.GetTime() );
566 oleVariant
.vt
= VT_DATE
;
568 if (!OleDateFromTm(time
.GetYear(), time
.GetMonth(), time
.GetDay(),
569 time
.GetHour(), time
.GetMinute(), time
.GetSecond(), oleVariant
.date
))
572 else if (type
== "void*")
574 oleVariant
.vt
= VT_DISPATCH
;
575 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
577 else if (type
== "list" || type
== "stringlist")
579 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
582 SAFEARRAYBOUND saBound
;
583 VARIANTARG
*pvargBase
;
587 int iCount
= variant
.GetCount();
590 saBound
.cElements
= iCount
;
592 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
596 SafeArrayAccessData(psa
, (void**)&pvargBase
);
599 for (i
= 0; i
< iCount
; i
++)
601 // copy each string in the list of strings
602 wxVariant
eachVariant(variant
[i
]);
603 if (!ConvertVariantToOle(eachVariant
, * pvarg
))
605 // memory failure: back out and free strings alloc'ed up to
606 // now, and then the array itself.
608 for (j
= 0; j
< i
; j
++)
610 SysFreeString(pvarg
->bstrVal
);
613 SafeArrayDestroy(psa
);
619 SafeArrayUnaccessData(psa
);
621 oleVariant
.parray
= psa
;
625 oleVariant
.vt
= VT_NULL
;
632 #define VT_TYPEMASK 0xfff
635 bool ConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
637 switch (oleVariant
.vt
& VT_TYPEMASK
)
641 wxString
str(ConvertStringFromOle(oleVariant
.bstrVal
));
648 if (!TmFromOleDate(oleVariant
.date
, tmTemp
))
651 wxDate
date(tmTemp
.tm_yday
, tmTemp
.tm_mon
, tmTemp
.tm_year
);
652 wxTime
time(date
, tmTemp
.tm_hour
, tmTemp
.tm_min
, tmTemp
.tm_sec
);
659 variant
= (long) oleVariant
.lVal
;
664 variant
= (long) oleVariant
.iVal
;
670 #if defined(__WATCOMC__) || (defined(_MSC_VER) && (_MSC_VER <= 1000)) //GC
671 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
672 variant
= (long) (oleVariant
.bool != 0);
674 variant
= (bool) (oleVariant
.bool != 0);
677 variant
= (bool) (oleVariant
.boolVal
!= 0);
683 variant
= oleVariant
.dblVal
;
690 int cDims
, cElements
, i
;
693 // Iterate the dimensions: number of elements is x*y*z
694 for (cDims
= 0, cElements
= 1;
695 cDims
< oleVariant
.parray
->cDims
; cDims
++)
696 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
698 // Get a pointer to the data
699 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
703 for (i
= 0; i
< cElements
; i
++)
705 VARIANTARG
& oleElement
= pvdata
[i
];
707 if (!ConvertOleToVariant(oleElement
, vElement
))
710 variant
.Append(vElement
);
712 SafeArrayUnaccessData(oleVariant
.parray
);
717 variant
= (void*) oleVariant
.pdispVal
;
727 break; // Ignore Empty Variant, used only during destruction of objects
731 wxLogError("wxAutomationObject::ConvertOleToVariant: Unknown variant value type");
738 static BSTR
ConvertStringToOle(const wxString
& str
)
741 unsigned int len = strlen((const char*) str);
742 unsigned short* s = new unsigned short[len*2+2];
744 memset(s, 0, len*2+2);
745 for (i=0; i < len; i++)
748 BasicString
bstr((const char*) str
);
752 static wxString
ConvertStringFromOle(BSTR bStr
)
754 int len
= SysStringLen(bStr
) + 1;
755 char *buf
= new char[len
];
756 int i
= wcstombs( buf
, bStr
, len
);
763 // ----------------------------------------------------------------------------
765 // ----------------------------------------------------------------------------
767 // ctor takes an ANSI string and transforms it to Unicode
768 BasicString::BasicString(const char *sz
)
770 // get the size of required buffer
771 UINT lenAnsi
= strlen(sz
);
773 UINT lenWide
= lenAnsi
* 2 ;
775 UINT lenWide
= mbstowcs(NULL
, sz
, lenAnsi
);
779 m_wzBuf
= new OLECHAR
[lenWide
+ 1];
780 mbstowcs(m_wzBuf
, sz
, lenAnsi
);
781 m_wzBuf
[lenWide
] = L
'\0';
789 BasicString::~BasicString()
794 /////////////////////////////////////////////////////////////////////////////
795 // COleDateTime class HELPERS - implementation
797 BOOL
OleDateFromTm(WORD wYear
, WORD wMonth
, WORD wDay
,
798 WORD wHour
, WORD wMinute
, WORD wSecond
, DATE
& dtDest
)
800 // Validate year and month (ignore day of week and milliseconds)
801 if (wYear
> 9999 || wMonth
< 1 || wMonth
> 12)
804 // Check for leap year and set the number of days in the month
805 BOOL bLeapYear
= ((wYear
& 3) == 0) &&
806 ((wYear
% 100) != 0 || (wYear
% 400) == 0);
809 rgMonthDays
[wMonth
] - rgMonthDays
[wMonth
-1] +
810 ((bLeapYear
&& wDay
== 29 && wMonth
== 2) ? 1 : 0);
812 // Finish validating the date
813 if (wDay
< 1 || wDay
> nDaysInMonth
||
814 wHour
> 23 || wMinute
> 59 ||
820 // Cache the date in days and time in fractional days
824 //It is a valid date; make Jan 1, 1AD be 1
825 nDate
= wYear
*365L + wYear
/4 - wYear
/100 + wYear
/400 +
826 rgMonthDays
[wMonth
-1] + wDay
;
828 // If leap year and it's before March, subtract 1:
829 if (wMonth
<= 2 && bLeapYear
)
832 // Offset so that 12/30/1899 is 0
835 dblTime
= (((long)wHour
* 3600L) + // hrs in seconds
836 ((long)wMinute
* 60L) + // mins in seconds
837 ((long)wSecond
)) / 86400.;
839 dtDest
= (double) nDate
+ ((nDate
>= 0) ? dblTime
: -dblTime
);
844 BOOL
TmFromOleDate(DATE dtSrc
, struct tm
& tmDest
)
846 // The legal range does not actually span year 0 to 9999.
847 if (dtSrc
> MAX_DATE
|| dtSrc
< MIN_DATE
) // about year 100 to about 9999
850 long nDays
; // Number of days since Dec. 30, 1899
851 long nDaysAbsolute
; // Number of days since 1/1/0
852 long nSecsInDay
; // Time in seconds since midnight
853 long nMinutesInDay
; // Minutes in day
855 long n400Years
; // Number of 400 year increments since 1/1/0
856 long n400Century
; // Century within 400 year block (0,1,2 or 3)
857 long n4Years
; // Number of 4 year increments since 1/1/0
858 long n4Day
; // Day within 4 year block
859 // (0 is 1/1/yr1, 1460 is 12/31/yr4)
860 long n4Yr
; // Year within 4 year block (0,1,2 or 3)
861 BOOL bLeap4
= TRUE
; // TRUE if 4 year block includes leap year
863 double dblDate
= dtSrc
; // tempory serial date
865 // If a valid date, then this conversion should not overflow
866 nDays
= (long)dblDate
;
868 // Round to the second
869 dblDate
+= ((dtSrc
> 0.0) ? HALF_SECOND
: -HALF_SECOND
);
871 nDaysAbsolute
= (long)dblDate
+ 693959L; // Add days from 1/1/0 to 12/30/1899
873 dblDate
= fabs(dblDate
);
874 nSecsInDay
= (long)((dblDate
- floor(dblDate
)) * 86400.);
876 // Calculate the day of week (sun=1, mon=2...)
877 // -1 because 1/1/0 is Sat. +1 because we want 1-based
878 tmDest
.tm_wday
= (int)((nDaysAbsolute
- 1) % 7L) + 1;
880 // Leap years every 4 yrs except centuries not multiples of 400.
881 n400Years
= (long)(nDaysAbsolute
/ 146097L);
883 // Set nDaysAbsolute to day within 400-year block
884 nDaysAbsolute
%= 146097L;
886 // -1 because first century has extra day
887 n400Century
= (long)((nDaysAbsolute
- 1) / 36524L);
890 if (n400Century
!= 0)
892 // Set nDaysAbsolute to day within century
893 nDaysAbsolute
= (nDaysAbsolute
- 1) % 36524L;
895 // +1 because 1st 4 year increment has 1460 days
896 n4Years
= (long)((nDaysAbsolute
+ 1) / 1461L);
899 n4Day
= (long)((nDaysAbsolute
+ 1) % 1461L);
903 n4Day
= (long)nDaysAbsolute
;
908 // Leap century - not special case!
909 n4Years
= (long)(nDaysAbsolute
/ 1461L);
910 n4Day
= (long)(nDaysAbsolute
% 1461L);
915 // -1 because first year has 366 days
916 n4Yr
= (n4Day
- 1) / 365;
919 n4Day
= (n4Day
- 1) % 365;
927 // n4Day is now 0-based day of year. Save 1-based day of year, year number
928 tmDest
.tm_yday
= (int)n4Day
+ 1;
929 tmDest
.tm_year
= n400Years
* 400 + n400Century
* 100 + n4Years
* 4 + n4Yr
;
931 // Handle leap year: before, on, and after Feb. 29.
932 if (n4Yr
== 0 && bLeap4
)
943 // Pretend it's not a leap year for month/day comp.
948 // Make n4DaY a 1-based day of non-leap year and compute
949 // month/day for everything but Feb. 29.
952 // Month number always >= n/32, so save some loop time */
953 for (tmDest
.tm_mon
= (n4Day
>> 5) + 1;
954 n4Day
> rgMonthDays
[tmDest
.tm_mon
]; tmDest
.tm_mon
++);
956 tmDest
.tm_mday
= (int)(n4Day
- rgMonthDays
[tmDest
.tm_mon
-1]);
960 tmDest
.tm_hour
= tmDest
.tm_min
= tmDest
.tm_sec
= 0;
963 tmDest
.tm_sec
= (int)nSecsInDay
% 60L;
964 nMinutesInDay
= nSecsInDay
/ 60L;
965 tmDest
.tm_min
= (int)nMinutesInDay
% 60;
966 tmDest
.tm_hour
= (int)nMinutesInDay
/ 60;
972 void TmConvertToStandardFormat(struct tm
& tmSrc
)
974 // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
975 tmSrc
.tm_year
-= 1900; // year is based on 1900
976 tmSrc
.tm_mon
-= 1; // month of year is 0-based
977 tmSrc
.tm_wday
-= 1; // day of week is 0-based
978 tmSrc
.tm_yday
-= 1; // day of year is 0-based
981 double DoubleFromDate(DATE dt
)
983 // No problem if positive
987 // If negative, must convert since negative dates not continuous
988 // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
989 double temp
= ceil(dt
);
990 return temp
- (dt
- temp
);
993 DATE
DateFromDouble(double dbl
)
995 // No problem if positive
999 // If negative, must convert since negative dates not continuous
1000 // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
1001 double temp
= floor(dbl
); // dbl is now whole part
1002 return temp
+ (temp
- dbl
);
1008 * Zeros a variant structure without regard to current contents
1010 static void ClearVariant(VARIANTARG
*pvarg
)
1012 pvarg
->vt
= VT_EMPTY
;
1013 pvarg
->wReserved1
= 0;
1014 pvarg
->wReserved2
= 0;
1015 pvarg
->wReserved3
= 0;
1022 * Clears a particular variant structure and releases any external objects
1023 * or memory contained in the variant. Supports the data types listed above.
1025 static void ReleaseVariant(VARIANTARG
*pvarg
)
1028 VARIANTARG _huge
*pvargArray
;
1029 long lLBound
, lUBound
, l
;
1031 vt
= pvarg
->vt
& 0xfff; // mask off flags
1033 // check if an array. If so, free its contents, then the array itself.
1034 if (V_ISARRAY(pvarg
))
1036 // variant arrays are all this routine currently knows about. Since a
1037 // variant can contain anything (even other arrays), call ourselves
1039 if (vt
== VT_VARIANT
)
1041 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
1042 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
1044 if (lUBound
> lLBound
)
1048 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
1050 for (l
= 0; l
< lUBound
; l
++)
1052 ReleaseVariant(pvargArray
);
1056 SafeArrayUnaccessData(pvarg
->parray
);
1061 wxLogWarning("ReleaseVariant: Array contains non-variant type");
1064 // Free the array itself.
1065 SafeArrayDestroy(pvarg
->parray
);
1072 if (pvarg
->pdispVal
)
1073 pvarg
->pdispVal
->Release();
1077 SysFreeString(pvarg
->bstrVal
);
1083 case VT_ERROR
: // to avoid erroring on an error return from Excel
1084 // no work for these types
1088 wxLogWarning("ReleaseVariant: Unknown type");
1093 ClearVariant(pvarg
);
1098 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
1102 switch (GetScode(hr
))
1104 case DISP_E_UNKNOWNNAME
:
1105 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
1108 case DISP_E_BADPARAMCOUNT
:
1109 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
1112 case DISP_E_EXCEPTION
:
1113 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
1114 if (pexcep
->bstrDescription
!= NULL
)
1115 lstrcat(szBuf
, pexcep
->bstrDescription
);
1117 lstrcat(szBuf
, L
"<<No Description>>");
1120 case DISP_E_MEMBERNOTFOUND
:
1121 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
1124 case DISP_E_OVERFLOW
:
1125 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
1128 case DISP_E_NONAMEDARGS
:
1129 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
1133 case DISP_E_UNKNOWNLCID
:
1134 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
1137 case DISP_E_PARAMNOTOPTIONAL
:
1138 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
1141 case DISP_E_PARAMNOTFOUND
:
1142 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
1145 case DISP_E_TYPEMISMATCH
:
1146 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
1150 wsprintf(szBuf
, L
"%s: Unknown error occured.", szMember
);
1154 wxLogWarning(szBuf
);