]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/ole/automtn.cpp
Better fix
[wxWidgets.git] / src / msw / ole / automtn.cpp
index 548ac661a80955cc789268c98b7c2335a9d3a094..c39b3b2e4c164b5e0cc51d859ba8e8ba65ab504b 100644 (file)
     #pragma hdrstop
 #endif
 
-// Watcom C++ gives a linker error if this is compiled in.
 // With Borland C++, all samples crash if this is compiled in.
-#if wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
+#if (defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) || defined(__CYGWIN10__)
+    #undef wxUSE_OLE_AUTOMATION
+    #define wxUSE_OLE_AUTOMATION 0
+#endif
+
+#if wxUSE_OLE_AUTOMATION
+
+#ifndef WX_PRECOMP
+    #include "wx/log.h"
+    #include "wx/math.h"
+#endif
 
 #define _FORCENAMELESSUNION
-#include "wx/log.h"
 #include "wx/msw/private.h"
 #include "wx/msw/ole/oleutils.h"
 #include "wx/msw/ole/automtn.h"
-#include "wx/math.h"
 
 #ifdef __WXWINCE__
 #include "wx/msw/wince/time.h"
@@ -47,7 +54,7 @@
 
 #if wxUSE_DATETIME
 #include "wx/datetime.h"
-#endif // wxUSE_TIMEDATE
+#endif // wxUSE_DATETIME
 
 static void ClearVariant(VARIANTARG *pvarg) ;
 static void ReleaseVariant(VARIANTARG *pvarg) ;
@@ -88,7 +95,7 @@ bool wxAutomationObject::Invoke(const wxString& member, int action,
     {
         // Use dot notation to get the next object
         wxString member2(nonConstMember.Left((size_t) ch));
-        wxString rest(nonConstMember.Right(nonConstMember.Length() - ch - 1));
+        wxString rest(nonConstMember.Right(nonConstMember.length() - ch - 1));
         wxAutomationObject obj;
         if (!GetObject(obj, member2))
             return false;
@@ -491,7 +498,7 @@ bool wxAutomationObject::GetInstance(const wxString& classId) const
     CLSID clsId;
     IUnknown * pUnk = NULL;
 
-    wxBasicString unicodeName(classId.mb_str());
+    wxBasicString unicodeName(classId);
 
     if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId)))
     {
@@ -523,7 +530,7 @@ bool wxAutomationObject::CreateInstance(const wxString& classId) const
 
     CLSID clsId;
 
-    wxBasicString unicodeName(classId.mb_str());
+    wxBasicString unicodeName(classId);
 
     if (FAILED(CLSIDFromProgID((BSTR) unicodeName, &clsId)))
     {
@@ -664,112 +671,137 @@ WXDLLEXPORT bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& ole
 #define VT_TYPEMASK 0xfff
 #endif
 
-WXDLLEXPORT bool wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant)
+WXDLLEXPORT bool
+wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant)
 {
-    switch (oleVariant.vt & VT_TYPEMASK)
+    if ( oleVariant.vt & VT_ARRAY )
     {
-    case VT_BSTR:
-        {
-            wxString str(wxConvertStringFromOle(oleVariant.bstrVal));
-            variant = str;
-            break;
-        }
-    case VT_DATE:
+        variant.ClearList();
+
+        // Compute the total number of elements in all array dimensions
+        int cElements = 1;
+        for ( int cDims = 0; cDims < oleVariant.parray->cDims; cDims++ )
+            cElements *= oleVariant.parray->rgsabound[cDims].cElements;
+
+        // Get a pointer to the data
+        VARIANTARG* pvdata;
+        HRESULT hr = SafeArrayAccessData(oleVariant.parray, (void **)&pvdata);
+        if ( FAILED(hr) )
+            return false;
+
+        bool ok = true;
+        for ( int i = 0; i < cElements; i++ )
         {
-#if wxUSE_DATETIME
-            unsigned short dosDate = 0;
-            unsigned short dosTime = 0;
-            VariantTimeToDosDateTime(oleVariant.date, & dosDate, & dosTime);
-
-            long dosDateTime = (dosDate << 16) || dosTime;
-            wxDateTime date;
-            date.SetFromDOS(dosDateTime);
-            variant = date;
-#endif
-            break;
+            VARIANTARG& oleElement = pvdata[i];
+            wxVariant vElement;
+            if ( !wxConvertOleToVariant(oleElement, vElement) )
+            {
+                ok = false;
+                variant.ClearList();
+                break;
+            }
+
+            variant.Append(vElement);
         }
-    case VT_I4:
+
+        SafeArrayUnaccessData(oleVariant.parray);
+    }
+    else if ( oleVariant.vt & VT_BYREF )
+    {
+        switch ( oleVariant.vt & VT_TYPEMASK )
         {
-            variant = (long) oleVariant.lVal;
-            break;
+            case VT_VARIANT:
+                {
+                    VARIANTARG& oleReference = *((LPVARIANT)oleVariant.byref);
+                    if (!wxConvertOleToVariant(oleReference,variant))
+                        return false;
+                    break;
+                }
+
+            default:
+                wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),oleVariant.vt);
+                return false;
         }
-    case VT_I2:
+    }
+    else // simply type (not array or reference)
+    {
+        switch (oleVariant.vt & VT_TYPEMASK)
         {
-            variant = (long) oleVariant.iVal;
-            break;
-        }
+            case VT_BSTR:
+                {
+                    wxString str(wxConvertStringFromOle(oleVariant.bstrVal));
+                    variant = str;
+                    break;
+                }
+            case VT_DATE:
+                {
+#if wxUSE_DATETIME
+                    unsigned short dosDate = 0;
+                    unsigned short dosTime = 0;
+                    VariantTimeToDosDateTime(oleVariant.date, & dosDate, & dosTime);
+
+                    long dosDateTime = (dosDate << 16) | dosTime;
+                    wxDateTime date;
+                    date.SetFromDOS(dosDateTime);
+                    variant = date;
+#endif
+                    break;
+                }
+            case VT_I4:
+                {
+                    variant = (long) oleVariant.lVal;
+                    break;
+                }
+            case VT_I2:
+                {
+                    variant = (long) oleVariant.iVal;
+                    break;
+                }
 
-    case VT_BOOL:
-        {
+            case VT_BOOL:
+                {
 #if (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
-            variant = (long) (oleVariant.bool != 0);
+                    variant = (long) (oleVariant.bool != 0);
 #else
-            variant = (bool) (oleVariant.bool != 0);
+                    variant = (bool) (oleVariant.bool != 0);
 #endif
 #else
 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
-            variant = (long) (oleVariant.boolVal != 0);
+                    variant = (long) (oleVariant.boolVal != 0);
 #else
-            variant = (bool) (oleVariant.boolVal != 0);
+                    variant = (bool) (oleVariant.boolVal != 0);
 #endif
 #endif
-            break;
-        }
-    case VT_R8:
-        {
-            variant = oleVariant.dblVal;
-            break;
-        }
-    case VT_ARRAY:
-        {
-            variant.ClearList();
-
-            int cDims, cElements, i;
-            VARIANTARG* pvdata;
-
-            // Iterate the dimensions: number of elements is x*y*z
-            for (cDims = 0, cElements = 1;
-                cDims < oleVariant.parray->cDims; cDims ++)
-                    cElements *= oleVariant.parray->rgsabound[cDims].cElements;
-
-            // Get a pointer to the data
-            HRESULT hr = SafeArrayAccessData(oleVariant.parray, (void HUGEP* FAR*) & pvdata);
-            if (hr != NOERROR)
-                return false;
-            // Iterate the data.
-            for (i = 0; i < cElements; i++)
-            {
-                VARIANTARG& oleElement = pvdata[i];
-                wxVariant vElement;
-                if (!wxConvertOleToVariant(oleElement, vElement))
+                    break;
+                }
+            case VT_R8:
+                {
+                    variant = oleVariant.dblVal;
+                    break;
+                }
+            case VT_DISPATCH:
+                {
+                    variant = (void*) oleVariant.pdispVal;
+                    break;
+                }
+            case VT_NULL:
+                {
+                    variant.MakeNull();
+                    break;
+                }
+            case VT_EMPTY:
+                {
+                    break;    // Ignore Empty Variant, used only during destruction of objects
+                }
+            default:
+                {
+                    wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),oleVariant.vt,oleVariant.vt&VT_TYPEMASK);
                     return false;
-
-                variant.Append(vElement);
-            }
-            SafeArrayUnaccessData(oleVariant.parray);
-            break;
-        }
-    case VT_DISPATCH:
-        {
-            variant = (void*) oleVariant.pdispVal;
-            break;
-        }
-    case VT_NULL:
-        {
-            variant.MakeNull();
-            break;
-        }
-    case VT_EMPTY:
-        {
-            break;    // Ignore Empty Variant, used only during destruction of objects
-        }
-    default:
-        {
-            wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
-            return false;
+                }
         }
     }
+
     return true;
 }
 
@@ -797,7 +829,7 @@ static void ReleaseVariant(VARIANTARG *pvarg)
 {
     VARTYPE vt;
     VARIANTARG _huge *pvargArray;
-    long lLBound, lUBound, l;
+    LONG lLBound, lUBound, l;
 
     vt = (VARTYPE)(pvarg->vt & 0xfff);        // mask off flags
 
@@ -929,4 +961,4 @@ void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned in
 
 #endif
 
-#endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
+#endif // wxUSE_OLE_AUTOMATION