在C++中怎麼調用一個js中的方法

來源:互聯網
上載者:User

 

 

例如一個test.js內容如下:

function main( input )

{

   return input;

}

在C++中調用方法如下:

// vcJscript.cpp : 定義控制台應用程式的進入點。

//

#include "stdafx.h"

#import "C:/windows/system32/msscript.ocx" // msscript.ocx 

using namespace MSScriptControl; 

#include <fstream>

#include <string>

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

HRESULT hr = CoInitialize(NULL); 

IScriptControlPtr pScriptControl(__uuidof(ScriptControl)); 

pScriptControl->Language = "JavaScript"; 

//pScriptControl->AllowUI = TRUE;

fstream file;

file.open( "test.js" );

string   strFileContent, strTemp;

char szTemp[1024]="";

 

do

{

   file.read(szTemp, 1024);

   strFileContent.append( szTemp );

   memset( szTemp, 0, 1024 );

}

while ( !file.fail() );

file.close();

pScriptControl->AddCode(strFileContent.c_str()); 

VARIANT A = pScriptControl->Eval("main(4);"); 

int iRet = A.intVal;

return 0;

}

指令碼控制項有四種方法。其中之一是 Run(),運行子常式或函數。在調用此方法之前,指定的指令碼語言、 設定 AllowUI,並將下面的指令碼添加到指令碼控制項:

//---------------------- Begin ---------------------------

 

#include <stdio.h>

#import "C:/winnt/system32/msscript.ocx"  // msscript.ocx 

using namespace MSScriptControl;

 

int main(void)

{

 HRESULT hr = CoInitialize(NULL);

 

 IScriptControlPtr pScriptControl(__uuidof(ScriptControl));

 

 // Create a VARIANT array of VARIANTs which hold BSTRs

 LPSAFEARRAY psa;

 SAFEARRAYBOUND rgsabound[]  = { 3, 0 }; // 3 elements, 0-based

 int i;

 

 psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);

 if (!psa)

 {

  return E_OUTOFMEMORY;

 }

 

 VARIANT vFlavors[3];

 for (i = 0; i < 3; i++)

 {

  VariantInit(&vFlavors[i]);

  V_VT(&vFlavors[i]) = VT_BSTR;

 }

 

 V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla"));

 V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate"));

 V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip"));

 

 long lZero = 0;

 long lOne = 1;

 long lTwo = 2;

 

 // Put Elements to the SafeArray:

 hr = SafeArrayPutElement(psa, &lZero,&vFlavors[0]);

 hr = SafeArrayPutElement(psa, &lOne,&vFlavors[1]);

 hr = SafeArrayPutElement(psa, &lTwo,&vFlavors[2]);

 

 // Free Elements from the SafeArray:

 for(i=0;i<3;i++)

 {

  SysFreeString(vFlavors[i].bstrVal);

 }

 

 // Set up Script control properties

 pScriptControl->Language = "JScript";

 pScriptControl->AllowUI = TRUE;

 pScriptControl->AddCode(

  "function MyStringFunction(Argu1,Argu2,Argu3)/ 

  {  return /"hi there/" ;}" );

 

 //  Call MyStringFunction with the two args:

 _variant_t outpar = pScriptControl->Run("MyStringFunction", &psa);

 

 

 // Convert VARIANT to C string:

 _bstr_t bstrReturn = (_bstr_t)outpar;

 char *pResult = (char *)bstrReturn;

 

 // Print the result out:

 printf("func=%s/n",pResult);

 

 //  Clean up:

 SafeArrayDestroy(psa);

 

 CoUninitialize();

 return(0);

}

 

// --------------- End -----------------------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.