CS-Script 3.20.0

C++ scripts


Among other programming languages CS-Script supports C++/CLI.

The C++ script compiler is implemented as a pluggable custom compiler (see Alternative compilers). C++ script compiler assembly is deployed and enabled at the time of the CS-Script installation/upgrade.

Though VisualStudio, VisualStudio Express C++ or Windows SDK must also be installed in order to run C++ scripts.

The most intriguing quality of C++/CLI syntax is that it may contain calls to both managed and unmanaged libraries. The following code illustrates just this:

MessageBoxA(0, "Hello World! (C++)""Non-Managed", 0);
MessageBox::Show("Hello World! (C++)""Managed");

The code above makes use of managed MessageBox.Show() and unmanaged MessageBoxA(). All this imply that C++/CLI script needs to be able to "tell" the script engine what unmanaged libraries it depends on. This can be done directly from code by using standard C++ linker directive. The following code indicates that User32.lib static library is required for running the script:

#pragma comment(lib, "User32.lib")  

The script code below is an example of the complete C++ script. It is the Hello.cpp script from the samples library (see Script Library).

//css_ref System.Windows.Forms;
#pragma comment(lib, "User32.lib")

#include "windows.h"
using namespace System;
using namespace System::Windows::Forms;

public ref class Script
{
    public
        static void Main()
        {
            Console::WriteLine(L"Hello World! (C++)");
            MessageBoxA(0, "Hello World! (C++)""Non-Managed", 0);
            MessageBox::Show("Hello World! (C++)""Managed");
        }
};

Limitations
C++/CLI custom compiler relays on usage of MSBuilder.exe - language independent building utility provided by Microsoft. MSBuild is a bit slower than naked language specific compilers (e.g. csc.exe, cl.exe). Also C++ compilation is slower by it's nature than compilation of C# code. That is why you may notice that startup delay for C++ scripts is slightly longer than for the other languages. However it is the case only for the very first execution of the script.  The script engine uses cached execution data for any consequent executions of the same unchanged script thus it will be no any excessive startup delay. Just as with any scripts written in other languages.

See Also

Alternative compilersScript Library