CS-Script 3.20.0

Using COM

Existing COM components are valuable resources for using in the managed code. There is almost no difference between using the COM components and managed assemblies in C# script. This is because the COM component is always accessed from the managed code through the type library assembly (which is just an ordinary assembly) prepared with Type Library Importer utility. This utility is a part of .NET Platform SDK. When the type library assembly is prepared it can be used just as any other assembly (see Using COM tutorial). 

Note that importing the type library can be automated with pre-script directive ("Single-line COM access"). What practically means that by having a single line //css_... directive in the script code you can gain access to the specified COM object with no extra development cost.

The following script sample illustrates how COM server WScript.Shell can be used to set permanently an environment variable. Note that WScript.Shell is available on any Windows platform as part of the VBScript.

//css_pre com(WScript.Shell, swshell.dll);
using System;
using swshell;

namespace Scripting
    class Script
        static public void Main(string[] args)
            object envType = "SYSTEM";
            IWshEnvironment wshSysEnv = new WshShellClass().get_Environment(ref envType);
            wshSysEnv["TEST"] = "MyDirectory";

There is a very special COM client scripting scenario when you may need to make a call to CoInitializeSecurity. The problem is that this call must be one of done before any COM calls. Unfortunately  when the script is loaded for the execution it is already too late. Thus CoInitializeSecurity must be invoked from the script engine even befor the script is loaded

CS-Script allows doing this with the dedicated //css_init directive, which is currently dedicated only for the COM scenarios:
 //css_init CoInitializeSecurity[(<dwImpLevel>, <dwCapabilities>)];

dwImpLevel - dwImpLevel parameter of CoInitializeSecurity function (see MSDN for sdetails)
dwCapabilities - dwCapabilities parameter of CoInitializeSecurity function (see MSDN for sdetails)

Note: Currently //css_init directive has no affect if //css_host directive is used.

See Also

Using .NET assemblies