Page 1 of 1

dotNETRunner - run JS, C#, Powershell, VB.NET from COM

Posted: 09 Nov 2020, 19:54
by burque505
Link to files: dotNETRunner.
Requires registration of DLLs before use. I had to do it as admin. From the web page:
This library offers the possibility to use C#, VB.NET, JScript or PowerShell code seamlessly in any COM enabled language. Consolidated dotNET code can easily be used on this way.
A caveat: be sure to edit the file dotNETRunner_Register.reg before you use it, because it has hard-coded paths.

Works with AHK. Here's a brief test script ported from AutoIt. If I were you, I'd put this script in the 'Examples' folder that's included in the download. I've used @[Shambles]'s libs AutoHotkey-Type_Checking, you'll need them. Nice libs, @[Shambles], thanks! Very useful.

When the file select dialog comes up, choose one of the following files in the 'Examples' folder:
  • Test.dotNETRunner.cs
  • Test.dotNETRunner.js
  • Test.dotNETRunner.vb
  • Test.dotNETRunner.ps1

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}

#Include Type.ahk
dotNETRunner := ComObjCreate("dotNET.Runner")

FileSelectFile, filename, , %A_ScriptDir%, Select a file to process

if ErrorLevel {
	msgbox You did not select a file, or another error occurred.`nExiting program.
	ExitApp
}

SplitPath, filename, outfilename, outdir, outext, outnamenoext, outdrive
; StringUpper, upext, outext ; Not needed in AHK, extension can be lower case
	
If (outext <> "PS1") {
	
	dotNETRunner.AddAssembly := "System.Windows.Forms.dll"
	result := dotNETRunner.CompileFile(outext, filename)
				if (result <> "") {
					return
				}
	
			
	preface := outfilename . " result: `n"

	result := dotNETRunner.Run("Foo.Bar", "", "SayHelloFunc")
	resulttype := Type(result)
	msgbox , , Result, % preface . " of type " . resulttype . " is " . result

	result := dotNETRunner.Run("Foo.Bar", "", "Say42Func")
	resulttype := Type(result)
	msgbox , , Result, % preface . " of type " . resulttype . " is " . result

	result := dotNETRunner.Run("Foo.Bar", "", "Say166Func")
	resulttype := Type(result)
	msgbox , , Result, % preface . " of type " . resulttype . " is " . result

	result := dotNETRunner.Run("Foo.Bar", "", "Add", "32, 32")
	resulttype := Type(result)
	msgbox , , Result, % preface . " of type " . resulttype . " is " . result

	dotNETRunner.Run("Foo.Bar", "", "Yell")

}

else {
	
    If !(dotNETRunner.IsPowerShellInstalled) {
      MsgBox Powershell isn't installed.
      Return
    }
	
	psversion := dotNETRunner.PowerShellVersion
	msgbox PS Version: %psversion%
	
	$Parameters := ""
    $Parameters := "var1 = AutoHotkey, "
    $Parameters .= "var2 = " . A_AhkVersion . ", "
    $Parameters .= "desc = PowerShell is cool, "
    $Parameters .= "i = 3"
	;msgbox %$Parameters%
    result := dotNETRunner.RunPSFileString(filename, $Parameters)
    MsgBox, ,"PowerShell", %result%
}
			
exitapp
Regards,
burque505