Using jscript.dll for COM Script Execution in C#

Deepak Rane (drane) 60 Reputation points
2025-01-07T07:47:11.1233333+00:00

What are the steps to utilize "C:\Windows\System32\jscript.dll" for executing scripts with COM?

I am looking for a sample code that demonstrates how to load the JScript DLL dynamically or by reflection, and how to utilize the methods or run scripts with it.

Note: The use of the Microsoft.JScript namespace is restricted in my case.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,237 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,856 Reputation points Microsoft Vendor
    2025-01-07T09:01:32.06+00:00

    Hi @Deepak Rane (drane) , Welcome to Microsoft Q&A,

    Use Type.GetTypeFromProgID("MSScriptControl.ScriptControl") to dynamically load COM types. Set the Language property to "JScript" to use the JScript engine. Use the Eval method to directly run the script and get the return value.

    using System;
    
    namespace xxx
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    // Load the COM object of the JScript engine
                    Type scriptControlType = Type.GetTypeFromProgID("MSScriptControl.ScriptControl");
                    if (scriptControlType == null)
                    {
                        Console.WriteLine("Unable to load MSScriptControl.ScriptControl. Make sure the correct component is registered and installed.");
                        return;
                    }
    
                    dynamic scriptControl = Activator.CreateInstance(scriptControlType);
    
                    // Set the language to JScript
                    scriptControl.Language = "JScript";
    
                    // Define and execute the script
                    string script = @"
    function add(a, b) {
    return a + b;
    }
    add(10, 20);
    ";
    
                    object result = scriptControl.Eval(script);
    
                    Console.WriteLine($"Script execution result: {result}");
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error occurred: {ex.Message}");
                }
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Michael Taylor 56,441 Reputation points
    2025-01-10T17:20:11.91+00:00

    JScript is a COM library. In order to use COM you MUST load it via COM. You cannot simply load the DLL directly and use COM objects from it because those objects require COM to be set up and running based upon the rules they require. Hence just loading the DLL doesn't work. Even if you could just load the DLL you are still limited to what functions are exposed and those functions are only the standard COM-related functions like register/unregister and unloading of DLL. You would not have access to any of the COM classes that you actually need.

    If you need to run Javascript code in your app then the legacy COM stuff probably isn't recommended anyway. I would recommend you use NuGet to import a managed package that provides support for Javascript directly. This eliminates the need for any COM interactions, better integrates with your code and allows you to ship with support for updated JS code. One such library might be ClearScript from Google but you can research the options and find one that fits your needs. This would be how I would solve that problem rather than trying to use COM.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.