Back to index

lightning-sunbird  0.9+nobinonly
nsXPCDispTestArrays.cpp
Go to the documentation of this file.
00001 // nsXPCDispTestArrays.cpp : Implementation of CXPCIDispatchTestApp and DLL registration.
00002 
00003 #include "stdafx.h"
00004 #include "XPCIDispatchTest.h"
00005 #include "nsXPCDispTestArrays.h"
00006 
00007 unsigned int zero = 0;
00008 
00010 //
00011 
00012 STDMETHODIMP nsXPCDispTestArrays::InterfaceSupportsErrorInfo(REFIID riid)
00013 {
00014     static const IID* arr[] = 
00015     {
00016         &IID_nsIXPCDispTestArrays,
00017     };
00018 
00019     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
00020     {
00021         if (InlineIsEqualGUID(*arr[i],riid))
00022             return S_OK;
00023     }
00024     return S_FALSE;
00025 }
00026 
00027 STDMETHODIMP nsXPCDispTestArrays::ReturnSafeArray(LPSAFEARRAY * result)
00028 {
00029     if (result == NULL)
00030         return E_POINTER;
00031 
00032        *result = SafeArrayCreateVector(VT_I4, 0, 3);
00033     for (long index = 0; index < 3; ++index)
00034     {
00035         SafeArrayPutElement(*result, &index, &index);
00036     }
00037     return S_OK;
00038 }
00039 
00040 STDMETHODIMP nsXPCDispTestArrays::ReturnSafeArrayBSTR(LPSAFEARRAY * result)
00041 {
00042     if (result == NULL)
00043         return E_POINTER;
00044 
00045        *result = SafeArrayCreateVector(VT_BSTR, 0, 3);
00046     for (long index = 0; index < 3; ++index)
00047     {
00048         _variant_t var(index);
00049         HRESULT hr = VariantChangeType(&var, &var, VARIANT_ALPHABOOL, VT_BSTR);
00050 
00051         if (FAILED(hr))
00052             return hr;
00053         SafeArrayPutElement(*result, &index, var.bstrVal);
00054     }
00055     return S_OK;
00056 }
00057 
00058 STDMETHODIMP nsXPCDispTestArrays::ReturnSafeArrayIDispatch(LPSAFEARRAY * result)
00059 {
00060     if (result == NULL)
00061         return E_POINTER;
00062 
00063        *result = SafeArrayCreateVector(VT_DISPATCH, 0, 3);
00064     for (long index = 0; index < 3; ++index)
00065     {
00066         CComPtr<nsIXPCDispSimple> ptr;
00067         ptr.CoCreateInstance(CLSID_nsXPCDispSimple);
00068         SafeArrayPutElement(*result, &index, ptr.p);
00069     }
00070     return S_OK;
00071 }
00072 
00073 #define RETURN_IF_FAIL(x) hr = x; if (FAILED(hr)) return hr;
00074 
00075 STDMETHODIMP nsXPCDispTestArrays::TakesSafeArray(LPSAFEARRAY array)
00076 {
00077     long lbound;
00078     long ubound;
00079     HRESULT hr;
00080 
00081     RETURN_IF_FAIL(SafeArrayGetLBound(array, 1, &lbound));
00082     if (lbound != 0)
00083         return E_FAIL;
00084     RETURN_IF_FAIL(SafeArrayGetUBound(array, 1, &ubound));
00085     if (ubound != 3)
00086         return E_FAIL;
00087     for (long index = lbound; index <= ubound; ++index)
00088     {
00089         _variant_t value;
00090         RETURN_IF_FAIL(SafeArrayGetElement(array, &index, &value));
00091         if (value != _variant_t(index))
00092             return E_FAIL;
00093     }
00094     return S_OK;
00095 }
00096 STDMETHODIMP nsXPCDispTestArrays::TakesSafeArrayBSTR(LPSAFEARRAY array)
00097 {
00098     long lbound;
00099     long ubound;
00100     HRESULT hr;
00101 
00102     RETURN_IF_FAIL(SafeArrayGetLBound(array, 1, &lbound));
00103     if (lbound != 0)
00104         return E_FAIL;
00105     RETURN_IF_FAIL(SafeArrayGetUBound(array, 1, &ubound));
00106     if (ubound != 3)
00107         return E_FAIL;
00108     for (long index = lbound; index <= ubound; ++index)
00109     {
00110         _variant_t value;
00111         RETURN_IF_FAIL(SafeArrayGetElement(array, &index, &value));
00112         _variant_t test(index);
00113         if (_bstr_t(value) != _bstr_t(test))
00114             return E_FAIL;
00115     }
00116     return S_OK;
00117 }
00118 STDMETHODIMP nsXPCDispTestArrays::TakesSafeArrayIDispatch(LPSAFEARRAY array)
00119 {
00120     long lbound;
00121     long ubound;
00122     HRESULT hr;
00123 
00124     RETURN_IF_FAIL(SafeArrayGetLBound(array, 0, &lbound));
00125     if (lbound != 0)
00126         return E_FAIL;
00127     RETURN_IF_FAIL(SafeArrayGetUBound(array, 0, &ubound));
00128     if (ubound != 3)
00129         return E_FAIL;
00130     for (long index = lbound; index <= ubound; ++index)
00131     {
00132         _variant_t value;
00133         RETURN_IF_FAIL(SafeArrayGetElement(array, &index, &value));
00134         // We need to do more here, but this is good enough for now
00135         if (!value.pdispVal)
00136             return E_FAIL;
00137     }
00138     return S_OK;
00139 }
00140 STDMETHODIMP nsXPCDispTestArrays::InOutSafeArray(LPSAFEARRAY * array)
00141 {
00142     if (array == NULL)
00143         return E_POINTER;
00144     long lbound;
00145     long ubound;
00146     HRESULT hr;
00147 
00148     RETURN_IF_FAIL(SafeArrayGetLBound(*array, 0, &lbound));
00149     if (lbound != 0)
00150         return E_FAIL;
00151     RETURN_IF_FAIL(SafeArrayGetUBound(*array, 0, &ubound));
00152     if (ubound != 3)
00153         return E_FAIL;
00154        LPSAFEARRAY newArray = SafeArrayCreateVector(VT_I4, lbound, ubound);
00155     for (long index = lbound; index <= ubound; ++index)
00156     {
00157         long value;
00158         RETURN_IF_FAIL(SafeArrayGetElement(*array, &index, &value));
00159         if (value != index)
00160             return E_FAIL;
00161         value += 42;
00162         RETURN_IF_FAIL(SafeArrayPutElement(newArray, &index, &value));
00163     }
00164     SafeArrayDestroy(*array);
00165     *array = newArray;
00166     return S_OK;
00167 }
00168 STDMETHODIMP nsXPCDispTestArrays::InOutSafeArrayBSTR(LPSAFEARRAY * array)
00169 {
00170     if (array == NULL)
00171         return E_POINTER;
00172         
00173     long lbound;
00174     long ubound;
00175     HRESULT hr;
00176 
00177     RETURN_IF_FAIL(SafeArrayGetLBound(*array, 0, &lbound));
00178     if (lbound != 0)
00179         return E_FAIL;
00180     RETURN_IF_FAIL(SafeArrayGetUBound(*array, 0, &ubound));
00181     if (ubound != 3)
00182         return E_FAIL;
00183        LPSAFEARRAY newArray = SafeArrayCreateVector(VT_BSTR, lbound, ubound);
00184     for (long index = lbound; index <= ubound; ++index)
00185     {
00186         BSTR value;
00187         RETURN_IF_FAIL(SafeArrayGetElement(*array, &index, &value));
00188         _bstr_t newValue(value, TRUE);
00189         newValue += L"Appended";
00190         RETURN_IF_FAIL(SafeArrayPutElement(newArray, &index, newValue.copy()));
00191     }
00192     SafeArrayDestroy(*array);
00193     *array = newArray;
00194     return S_OK;
00195 }
00196 STDMETHODIMP nsXPCDispTestArrays::InOutSafeArrayIDispatch(LPSAFEARRAY * array)
00197 {
00198     if (array == NULL)
00199         return E_POINTER;
00200         
00201     long lbound;
00202     long ubound;
00203     HRESULT hr;
00204 
00205     RETURN_IF_FAIL(SafeArrayGetLBound(*array, 0, &lbound));
00206     if (lbound != 0)
00207         return E_FAIL;
00208     RETURN_IF_FAIL(SafeArrayGetUBound(*array, 0, &ubound));
00209     if (ubound != 3)
00210         return E_FAIL;
00211        LPSAFEARRAY newArray = SafeArrayCreateVector(VT_DISPATCH, lbound, ubound);
00212     for (long index = lbound; index <= ubound; ++index)
00213     {
00214         IDispatch* value;
00215         RETURN_IF_FAIL(SafeArrayGetElement(*array, &index, &value));
00216         RETURN_IF_FAIL(SafeArrayPutElement(newArray, &index, &value));
00217     }
00218     SafeArrayDestroy(*array);
00219     *array = newArray;
00220     return S_OK;
00221 }