none
What is the proper way to return strings to javascript from the window.external object from your hosting application? RRS feed

  • Question

  • STDMETHODIMP CImpIDispatch::Invoke(
                /* [in] */ DISPID dispIdMember,
                /* [in] */ REFIID /*riid*/,
                /* [in] */ LCID /*lcid*/,
                /* [in] */ WORD wFlags,
                /* [out][in] */ DISPPARAMS* pDispParams,
                /* [out] */ VARIANT* pVarResult,
                /* [out] */ EXCEPINFO* /*pExcepInfo*/,
                /* [out] */ UINT* puArgErr)
    {
    
    	CCustomBrowserDlg* pDlg = (CCustomBrowserDlg*)AfxGetMainWnd();
    	if(dispIdMember == DISPID_CB_GetVersionString)
    	{
    		if(wFlags & DISPATCH_PROPERTYGET)
    		{
    			if(pVarResult != NULL)
    			{
    
    				VariantInit(pVarResult);
    				V_VT(pVarResult)=VT_BSTR;
    				
    				V_BSTR(pVarResult) = CComBSTR(pDlg->CB_GetVersionString());
    			}
    		}
    
    		if ( wFlags & DISPATCH_METHOD )
    		{
    			if(pVarResult != NULL)
    			{
    			VariantInit(pVarResult);
    			V_VT(pVarResult)=VT_BSTR;
    			V_BSTR(pVarResult) = CComBSTR(pDlg->CB_GetVersionString());
    			}
    			
    
    		}
    	}
    
    }
    The function CB_GetVersionString just returns a const CString that gets converted to a VT_BSTR and placed in the pVarResult I then call is from JavaScript like this.
    var myText = window.external.CB_GetVersionString();
    but i only sometimes get the correct result and when i shutdown the App, can crash on cleanup
    Is there any examples of the best way to set the return value of Invoke so i can return a string to Javascript?
    Monday, December 14, 2009 11:07 PM

All replies

  • CComBSTR releases the BSTR in its destructor. Make sure the BSTR is available to the script AFTER the function returns to avoid unexpected behavior. 




    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Tuesday, December 15, 2009 8:25 PM