Results 1 to 2 of 2

Thread: Sending an array from VC++ to a VB OCX

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    3

    Question

    Hello all,

    I have a problem, which is bugging me for a few weeks now and I hope I will find the answer here.

    I wrote an activex control in visual basic. The activex control has a method that one of the arguments is a “Variant” that expects an array.
    Now I’m trying to use the control from VC++. The problem is how should I send the array from VC++ to the control.

    For example, I attached a demo OCX (proTest.ocx) that have only one method (I had some problems attaching the file so it can be found here
    http://www.optiwater.com/proTest.zip):

    Public Function TEST(varArray As Variant) As Integer



    The method expects varArray to be an array (no specific size). The array’s elements are shown in a message box – so use small arrays.

    I would be very grateful for any ideas,

    Elad Salomons

  2. #2
    New Member
    Join Date
    Feb 2001
    Location
    Kent
    Posts
    3

    How to ....

    Here is a C++ code snippet....

    Create an ATL DLL, then a simple object add a method to return a VARIANT ([out, retval] VARIANT* pVal) then

    #include <comdef.h>

    STDMETHODIMP CTSA::GetSafeArray(VARIANT *pVal)
    {
    SAFEARRAYBOUND rgsabound[1];
    HRESULT hr = S_OK;

    _variant_t var[5];
    var[0].vt = VT_I4;
    var[0].lVal = 500;
    var[1] = L"This is String 1";
    var[2] = L"This is String 2";
    var[3].vt = VT_I4;
    var[3].lVal = 324412;
    var[4] = L"This is String 3";
    const int varsize = sizeof var / sizeof var[0];

    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = varsize;

    pVal->vt = (VT_ARRAY | VT_VARIANT);
    pVal->parray = SafeArrayCreate(VT_VARIANT, 1, rgsabound);

    for (long x = 0; x < varsize && SUCCEEDED(hr); x++)
    {
    hr = SafeArrayPutElement(pVal->parray, &x, &var[x]);
    }

    return hr;
    }


    so From VB ...

    Dim X As TESTSAFEARRAYLib.TSA
    Set X = New TESTSAFEARRAYLib.TSA

    Dim Y As Variant
    Y = X.GetSafeArray

    MsgBox (Y(3))


    Hope this helps ....

    Steve Conner

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width