在C#中为64位项目调用Javascript函数 [英] Calling the Javascript functions inside C# for a 64 bit project
问题描述
我正在尝试从C#代码内部调用Javascript函数 eval(以将字符串用于运算符解析器)。我使用了以下代码:
https://stackoverflow.com/a/12431435/712700
I am trying to call the Javascript function "eval" from inside C# code (to utilise the string to operators parser). I used the following code: https://stackoverflow.com/a/12431435/712700
尽管崩溃,但显示以下消息---:
It crashes though with the following message--- :
异常详细信息: System.Runtime.InteropServices.COMException:检索具有CLSID
{0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC}的组件的COM类工厂,由于以下
错误而失败:80040154类未注册(HRESULT异常:
0x80040154(REGDB_E_CLASSNOTREG))。
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
我相信问题是因为项目/机器是64位。我不想将项目更改为32位,是否可以使用另一个CLSID使其与64位一起工作?还是有另一种方法可以简单地在C#代码中利用Javascript中的 eval功能?
I believe the problem is because the project/machine is 64 bit. I do not want to change the project to 32 bit, is there another CLSID I can use to make it work with 64 bit? Or is there another approach to simply utilise the "eval" function from Javascript within C# code?
推荐答案
代码
Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "javascript";
var res = obj.Eval("a=3; 2*a+32-Math.sin(6)");
以上ClSID实际上是针对ScriptControl的
The above ClSID is actually for the ScriptControl
- 添加ScriptControl -语言参数
- 使用ScriptControl方法-评估方法
- Adding a ScriptControl - Language Parameter
- Using ScriptControl Methods - Eval Method
从上面的2个链接中,您会看到这与VB6有关,如果您要使用问题中提到的上述代码,则需要 msscript.ocx 。请检查您是否有此文件。您可能必须使用regsvr32正确注册它。
From the 2 above links you will see that this is VB6 related and if you want to use the code above as mentioned in your question you will need msscript.ocx. Please check if you have this file. You may have to register it correctly using regsvr32
我正在使用64位计算机,并且在以下文件夹 C中看到该文件:\ \Windows\SysWOW64
I am using a 64-bit machine and I see the file in the following folder C:\Windows\SysWOW64
这篇关于在C#中为64位项目调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!