locked
how to call a function of a object in jscript usr vc++ RRS feed

  • Question

  • hi ,all,my english is not very good ,i hope i can descript my question clearly.

    recently ,i  m writing a project need to call some javascript function in my c++ project,i use the IScriptControl interface,and i solve the simple call of the javascript function,for example there is a function in my test.js:

    function test(){return "hello world";}

    in my project code like that :

                    CoInitialize(NULL);
    	
    		HRESULT hr = m_Script.CreateInstance(__uuidof(ScriptControl));
    		if (FAILED(hr))
    			return FALSE;
    		m_Script->PutLanguage(L"JScript");
    		m_Script->PutAllowUI(VARIANT_TRUE);
    	
    		m_Script->AddCode(_T("function test(){return \"hello world\"}"));
    		//..........
    
    		IScriptProcedureCollectionPtr m_collection = m_Script->GetProcedures();
    		LONG u = m_collection->GetCount();
    		IScriptProcedurePtr m_procedure = m_collection->GetItem(_variant_t(1));
    		_bstr_t funname = m_procedure->GetName();
    
    		m_Script->Run(//.....);
    

    this just a test ,that code can call the normal jscript function ,but if there is an object in a js file ,like:

    var TestObj = {};
    
    TestObj.func = function()
    {
          return "hello world";
    }
    now ,i dont know how to call this function. i search on google,and know i maybe need a IDispatch Object,because the IScriptControl::AddObject(),need a IDisPatch pointer be parameter,i dont know how to create that object to reference the object in my js file ,if u know that ,pls tell me ,thank you very much 


    Tuesday, October 20, 2015 4:22 PM

Answers

  • Zhou,

    Your second code snippet defines a function and adds that function as a property to your TestObj object.  There is nothing in there that calls a function.  If you need to invoke it, you can call TestObj.func().

    You can pass your TestObj object into your C++ code.  However, to do that, you will need to either define an external function for the container, or call an object that itself takes an object.  I find the second method easier, since I don't always need or want to set up and configure the container. 

    To follow that method, you will need some JScript code along these lines:

    function GetObject(caller)
    {
       caller.SetObject(TestObj);
    }
    

    where TestObj is your object from above.  The 'caller' parameter is your IDispatch*, pointing to a valid ActiveX object (meaning it needs to have a configured type library).  If you already know COM, the specifics of how to pass an object is fairly well documented in the Microsoft JScript engine documentation.  If not, I suggest you at least take a crash course.

    Tuesday, October 20, 2015 10:52 PM

All replies

  • Where do the jscript reside at?  I did some with jscript before, but it must run inside IE control.

    Steve

    Tuesday, October 20, 2015 4:45 PM
  • Zhou,

    Your second code snippet defines a function and adds that function as a property to your TestObj object.  There is nothing in there that calls a function.  If you need to invoke it, you can call TestObj.func().

    You can pass your TestObj object into your C++ code.  However, to do that, you will need to either define an external function for the container, or call an object that itself takes an object.  I find the second method easier, since I don't always need or want to set up and configure the container. 

    To follow that method, you will need some JScript code along these lines:

    function GetObject(caller)
    {
       caller.SetObject(TestObj);
    }
    

    where TestObj is your object from above.  The 'caller' parameter is your IDispatch*, pointing to a valid ActiveX object (meaning it needs to have a configured type library).  If you already know COM, the specifics of how to pass an object is fairly well documented in the Microsoft JScript engine documentation.  If not, I suggest you at least take a crash course.

    Tuesday, October 20, 2015 10:52 PM