![在C++中怎么调用一个js中的方法,第1张 在C++中怎么调用一个js中的方法,第1张](/aiimages/%E5%9C%A8C%2B%2B%E4%B8%AD%E6%80%8E%E4%B9%88%E8%B0%83%E7%94%A8%E4%B8%80%E4%B8%AAjs%E4%B8%AD%E7%9A%84%E6%96%B9%E6%B3%95.png)
例如一个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 stdint _tmain(int argc, _TCHAR* argv[]){HRESULT hr = CoInitialize(NULL)IScriptControlPtr pScriptControl(__uuidof(ScriptControl))pScriptControl->Language = "JavaScript"//pScriptControl->AllowUI = TRUEfstream filefile.open( "test.js" )string strFileContent, strTempchar 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.intValreturn 0}脚本控件有四种方法。其中之一是 Run(),运行子例程或函数。在调用此方法之前,指定的脚本语言、 设置 AllowUI,并将下面的脚本添加到脚本控件://---------------------- Begin --------------------------- #include <stdio.h>#import "C:/winnt/system32/msscript.ocx" // msscript.ocx using namespace MSScriptControlint main(void){ HRESULT hr = CoInitialize(NULL) IScriptControlPtr pScriptControl(__uuidof(ScriptControl)) // Create a VARIANT array of VARIANTs which hold BSTRs LPSAFEARRAY psaSAFEARRAYBOUND 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 = 0i <3i++) { 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 = 0long lOne = 1long 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=0i<3i++) { SysFreeString(vFlavors[i].bstrVal)} // Set up Script control properties pScriptControl->Language = "JScript"pScriptControl->AllowUI = TRUEpScriptControl->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)outparchar *pResult = (char *)bstrReturn // Print the result out: printf("func=%s/n",pResult) // Clean up: SafeArrayDestroy(psa) CoUninitialize()return(0)}脚本执行的基础是WEB控件,
那么C++在WINDOWS下运行可以装入WEB控件实现脚本调用。
下面是一段引用某位博主的代码可以参考一下。
// 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) //使用一个变量初始化COM设置,单线程运行
IScriptControlPtr pScriptControl(__uuidof(ScriptControl))//初始化脚本控件
pScriptControl->Language = "JavaScript"//设置语言
pScriptControl->AllowUI = TRUE//设置显示
fstream file//文件流用于读出JS代码
file.open( "test.js" )
string strFileContent, strTemp
char szTemp[1024]=""
do
{
file.read(szTemp, 1024)
strFileContent.append( szTemp )//拼接JS文件
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
}
你是说把C#可以使用js方法,或者说是把js的方法转成C#的方法么?
首先你要新建一个js,把js方法拷贝过来
然后利用.net自带的编译程序。给你举个例子,加入我C盘下有个文件,内容是
public class JS
{
public static function Test(a)
{
return a+1
}
}
然后选择开始->程序->Visiual Stdio->Visiual Stdio Tool->命令提示符工具
然后输入(以我的为例)
C:\Program Files\Microsoft Visual Studio 10.0\VC>jsc /t:library c:\js.js
这句就是把C盘下的js.js编译成.net的dll,
生成的目录在(以我的为例)
C:\Program Files\Microsoft Visual Studio 10.0\VC下面。
你在程序里引用一下该dll,同时引用一下MicroSoft.JScript。
然后在程序里,你应该可以有这样一个方法
JS.Test(object a),传一个参数进去,就能返回 结果。
记住:
1,JS的方法外面必须包含(public class JS)类,这个JS你随便取。
2,JS的所有方法前面必须加入public static,否则引用不到。