how to convert SafeArray to array ?

Discussion in 'Javascript' started by thinktwice, Jan 24, 2006.

  1. thinktwice

    thinktwice Guest

    in my script file , i need call a method of a atl com
    module(implemented in vc++), which returan an safearray. i don't know
    how to convert it into array in jscript. i have tried serveral ways to
    get each item but failed at last. could anyone help me?
    thinktwice, Jan 24, 2006
    #1
    1. Advertising

  2. thinktwice

    VK Guest

    thinktwice wrote:
    > in my script file , i need call a method of a atl com
    > module(implemented in vc++), which returan an safearray. i don't know
    > how to convert it into array in jscript. i have tried serveral ways to
    > get each item but failed at last. could anyone help me?


    SafeArray is Microsoft specific, so you cannot handle it by JavaScript,
    only by JScript (IE).

    JScript has VBArray object wrapper for safe arrays.

    var arr = (new VBArray(mySafeArray)).toArray();

    arr now contains mySefeArray converted into standard JavaScript jagged
    sparse array.
    VK, Jan 24, 2006
    #2
    1. Advertising

  3. thinktwice

    thinktwice Guest

    this is definition of the method
    [ propget...]
    get_mySafeArray(VARIANT *pval);

    i tried
    new VBArray(mySafeArray()) but failed
    thinktwice, Jan 24, 2006
    #3
  4. thinktwice

    VK Guest

    thinktwice wrote:
    > this is definition of the method
    > [ propget...]
    > get_mySafeArray(VARIANT *pval);
    >
    > i tried
    > new VBArray(mySafeArray()) but failed


    On what side (client or server) did you try? VBArray has sense only on
    client side and only in the context of JScript.

    Also it is IE-exclusive. Your server side should return instead
    standard JavaScript array or JSON object (if AJAX) to easy handle it on
    client side.
    VK, Jan 24, 2006
    #4
  5. thinktwice

    thinktwice Guest

    hi, VK. thanks for your reply.
    i do use Jscript now.

    try{
    var arr = (new VBArray(myobj.mySafeArray)).toArray();
    }
    catch(e)
    {
    e.errorno;//0x800A1395
    }
    thinktwice, Jan 24, 2006
    #5
  6. thinktwice

    thinktwice Guest

    here is the code in server side
    STDMETHOD(mySafeArray)(VARIANT *pVal)
    {
    CComSafeArray<long> arr;
    i = 5;
    while(i-- > 0)
    {
    hr = arr.Add(i);
    }
    CComVariant vtRet;
    LPSAFEARRAY pCopy;
    if (arr.m_psa != NULL)
    {
    HRESULT hRes = ::SafeArrayCopy(arr.m_psa, &pCopy);
    if (SUCCEEDED(hRes) && pCopy != NULL)
    {
    ::SafeArrayGetVartype(arr.m_psa, &(vtRet.vt));
    vtRet.vt |= VT_ARRAY;
    vtRet.parray = pCopy;
    }
    else
    {
    vtRet.vt = VT_ERROR;
    vtRet.scode = hRes;
    }
    }
    return vtRet.Detach(pVal);
    }
    thinktwice, Jan 24, 2006
    #6
  7. thinktwice

    VK Guest

    thinktwice wrote:
    > here is the code in server side
    > STDMETHOD(mySafeArray)(VARIANT *pVal)
    > {
    > CComSafeArray<long> arr;
    > i = 5;
    > while(i-- > 0)
    > {
    > hr = arr.Add(i);
    > }
    > CComVariant vtRet;
    > LPSAFEARRAY pCopy;
    > if (arr.m_psa != NULL)
    > {
    > HRESULT hRes = ::SafeArrayCopy(arr.m_psa, &pCopy);
    > if (SUCCEEDED(hRes) && pCopy != NULL)
    > {
    > ::SafeArrayGetVartype(arr.m_psa, &(vtRet.vt));
    > vtRet.vt |= VT_ARRAY;
    > vtRet.parray = pCopy;
    > }
    > else
    > {
    > vtRet.vt = VT_ERROR;
    > vtRet.scode = hRes;
    > }
    > }
    > return vtRet.Detach(pVal);
    > }


    Truthfully I am not a VBArray expert as I almost never had to use it.
    But in any case it's *VB*Array (Visual Basic Array)

    Therefore your only options are:
    1) Use VBScript on client side instead of JScript (this naturally
    exclude all browsers by IE)
    2) Use VBScript helper together with JScript - so safe array would come
    first to VBScript which understand safe array format - and convert it
    later to JavaScript array (this naturally exclude all browsers but IE)
    3) Change your server-side procedure so it would return JavaScript
    array. I don't see really any problems with the latter (?)
    VK, Jan 24, 2006
    #7
  8. thinktwice

    thinktwice Guest

    thanks VK. i just wonder why the debug window could show the array
    content correctly. if it could achieve this, i guess i would get the
    content out of the safearray too.
    again thank you for taking time to answer my question .:)
    thinktwice, Jan 24, 2006
    #8
  9. thinktwice

    VK Guest

    thinktwice wrote:
    > i just wonder why the debug window could show the array
    > content correctly.


    I guess because any data type (known or unknown) is still internally a
    sequence of characters / bytes. System may do not know how to handle
    it, but still can display it as some text. (?)
    VK, Jan 24, 2006
    #9
  10. thinktwice wrote:

    > i do use Jscript now.
    >
    > try{
    > var arr = (new VBArray(myobj.mySafeArray)).toArray();
    > }
    > catch(e)
    > {
    > e.errorno;//0x800A1395
    > }


    There is no `errorno' property, the Error object property indicating the
    numeric value associated with an error in JScript is `number'.[1] And
    using it in this way does exactly nothing but evaluating the value of the
    property. Furthermore, this will break in JScript/IE before version 5.

    Think twice.


    PointedEars
    ___________
    [1]
    <URL:http://msdn.microsoft.com/library/en-us/jscript7/html/jspronumber.asp>
    Thomas 'PointedEars' Lahn, Jan 24, 2006
    #10
  11. thinktwice

    Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 1/24/2006 6:49 AM:
    > Furthermore, this will break in JScript/IE before version 5.


    IE4, IE5.0 and IE5.5 (to me) have fallen into the same category as NN4.
    They are old enough that its time to move on and forget them.

    >
    > Think twice.
    >


    Three times :)

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jan 24, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sebastian Wain
    Replies:
    0
    Views:
    833
    Sebastian Wain
    Jul 13, 2004
  2. Python and SAFEARRAY

    , Aug 17, 2006, in forum: Python
    Replies:
    0
    Views:
    784
  3. Safearray question

    , Aug 18, 2006, in forum: Python
    Replies:
    0
    Views:
    349
  4. Simply_Red

    Variant, SafeArray.....etc

    Simply_Red, Jul 31, 2007, in forum: C++
    Replies:
    2
    Views:
    527
  5. George2

    safearray

    George2, Nov 21, 2007, in forum: C++
    Replies:
    2
    Views:
    481
    Ian Collins
    Nov 21, 2007
Loading...

Share This Page