Calling the Javascript functions inside C# for a 6

2019-03-06 03:59发布

问题:

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--- :

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)).

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?

回答1:

You are referring to the following 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)");

The above ClSID is actually for the ScriptControl

  • Adding a ScriptControl - Language Parameter
  • Using ScriptControl Methods - Eval Method

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

I am using a 64-bit machine and I see the file in the following folder C:\Windows\SysWOW64