code on passing array from MFC ActiveX to Jscript...

Discussion in 'MFC' started by 燕山剑, Jun 27, 2007.

  1. 燕山剑

    燕山剑 New Member

    Joined:
    May 31, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    爱国者同盟 Chinese Patriot League
    Home Page:
    http://projects.takingitglobal.org/tds
    Code:
    alst=document.getElementById("ListBox1");
    
    alst.addItem(alst,"yyyyyyyy",      "666");
    alst.removeItem(alst,2);
    
    asb =alst.getItems(alst);
    
    
    asj = new VBArray( asb ).toArray();
    
    window.alert(asj[0]);
    window.alert(asj[1]);
    window.alert(asj[2]);
    
    window.alert(asj[3]);
    window.alert(asj[4]);
    window.alert(asj[5]);
    
    window.alert(asj[6]);
    window.alert(asj[7]);
    window.alert(asj[8]);
    
    http://msdn2.microsoft.com/en-us/library/xc634c13.aspx
    The conversion translates the multidimensional VBArray into a single dimensional JScript array. 
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ^^in the idl^^
    Code:
    	[ uuid(CB35E5F2-...) ]
    	dispinterface IMyList
    	{
    ...
    methods:
    		[id(5), helpstring("方法getItem")] VARIANT getItem(IDispatch* jjj, LONG kkk);
    	};
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ^^in The CPP^^
    Code:
    BEGIN_DISPATCH_MAP(CUFList, CListBox)
    ...
    	DISP_FUNCTION_ID(CUFList, "getItem", dispidgetItem, getItem, VT_VARIANT, VTS_DISPATCH VTS_I4)
    END_DISPATCH_MAP()
    
    
    VARIANT CmYList::getItem(IDispatch* ooo, LONG iii)
    {
    	AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
        USES_CONVERSION;
    
        HRESULT hr= S_OK;
    
    	VARIANT * pGIV = new VARIANT;
    	VariantInit(pGIV); // WARNING You must initialize *pVal before calling Detach
    
        // Create SafeArray of VARIANT BSTRs
        SAFEARRAY *pSA;
        SAFEARRAYBOUND aDim[1];
        aDim[0].lLbound= 0;
        aDim[0].cElements= 3;
        pSA= SafeArrayCreate(VT_VARIANT,1,aDim);
    
        if (pSA != NULL) {
    		_variant_t vOut;
    		CString strIII;
    		long l =0;	
    
    			GetText(iii,strIII);	
                vOut= A2W(strIII);
                if (hr= SafeArrayPutElement(pSA, &l, &vOut)) {
                    SafeArrayDestroy(pSA); // does a deep destroy
                    return * pGIV;
                }
                l++;
    
    			strIII.Format("%d", (int)GetItemData(iii));
                vOut= A2W(strIII);
                if (hr= SafeArrayPutElement(pSA, &l, &vOut)) {
                    SafeArrayDestroy(pSA); // does a deep destroy
                    return * pGIV;
                }
                l++;
    					
    			strIII.Format("%d", (int)GetSel(iii));
                vOut= A2W(strIII);
                if (hr= SafeArrayPutElement(pSA, &l, &vOut)) {
                    SafeArrayDestroy(pSA); // does a deep destroy
                    return * pGIV;
                }
        }
    
    
    V_VT(pGIV)= VT_ARRAY | VT_VARIANT; //pVal->vt= VT_ARRAY | VT_VARIANT; // oleauto.h
    V_ARRAY(pGIV)= pSA; // (pSA may be null) //pVal->parray= pSA; // oleauto.h
    return *  pGIV;
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ^^IN THE H^^
    Code:
    	enum 
    	{
    		dispidgetItem = 5L,	...
    	};
    	VARIANT getItem(IDispatch* ooo, LONG iii);
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice