CLR (for AutoHotkey), CLR_L (for AutoHotkey_L)
public domain
CLR (not CLR_L) requires COM Standard Library
Key Features:
- Load the Common Language Runtime into the script's process.
- Load .NET assemblies (dll files) by full name, partial name, or path.
- Instantiate objects and call instance methods or properties.
- Compile C# or VB code on the fly or to file.
Functions
CLR_Start() (CLR)
CLR_Start( [ RuntimeVersion ] ) (CLR_L)
Loads the Common Language Runtime.
CLR_L only: RuntimeVersion specifies the exact version to load - for example, "v2.0.50727" or "v4.0.30319". If omitted, the latest version is loaded. If this function is not called and another CLR function requires the runtime to be loaded, the latest version will be loaded.
CLR_StartDomain( ByRef AppDomain [, BaseDirectory ] )
Starts a new AppDomain and stores a pointer or reference to it in AppDomain. This can be passed to CLR_LoadLibrary() to load an assembly into the AppDomain. BaseDirectory defines the base search path used when loading assemblies into the AppDomain.
CLR_StopDomain( AppDomain )
Stops the specified AppDomain and attempts to unload any assemblies that were loaded into it.
CLR_LoadLibrary( AssemblyName [, AppDomain ] )
Loads an assembly, where AssemblyName is its full name, partial name or path. Optionally loads the assembly into the given AppDomain instead of the default AppDomain. Returns a pointer or reference to the Assembly, which can be used with CLR_CreateObject.
Note: Once an assembly is loaded, it can only be unloaded by stopping the AppDomain which contains it.
CLR_CreateObject( Assembly, sType [, Type1, Arg1, Type2, Arg2 ... ] ) (CLR)
CLR_CreateObject( Assembly, sType [, Arg1, Arg2 ... ] ) (CLR_L)
Instantiates an object of the specified type from the specified assembly. Optionally accepts a list of arguments to pass to the object's constructor. For AutoHotkey Basic, TypeN is a value from the VARENUM enumeration. For AutoHotkey_L, use ComObject(Type, Arg) to pass a typed value. A list of type codes can be found in the AutoHotkey_L documentation.
CLR_CompileC#( Code, References [, AppDomain, FileName, CompilerOptions ] )
CLR_CompileVB( Code, References [, AppDomain, FileName, CompilerOptions ] )
Compile the specified C# or VB code. If FileName is omitted, the assembly is compiled "in-memory" and automatically loaded. DLL and EXE files may be generated. Specify for References a pipe (|) delimited list of assemblies that the code requires. If FileName is omitted and compilation is successful, returns a pointer or reference to the compiled Assembly, which can be used with CLR_CreateObject; otherwise returns FileName on success or 0 on failure.
Additional command-line arguments can be passed to the compiler via CompilerOptions. For instance, if FileName specifies an .exe file, a console app is generated unless CompilerOptions includes "/target:winexe".
Examples
Additional Functions
Not included in CLR.ahk since they generally aren't of use.
CLR_GetVersion( ByRef sVer )
If the Common Language Runtime is loaded, gets the version in use. According to MSDN, it should otherwise get the latest installed version. However, on my Windows 7 system with both v2.0.50727 and v4.0.30319, it returns v2.0.50727.
CLR_GetAssemblies( [ pAppDomain ] )
Gets a `n-delimited list of assemblies loaded into the specified AppDomain (or the default AppDomain if not specified.) This function is not supported with CLR_L v1.2 or later. Instead, use the much simpler code shown further below.
; AutoHotkey Basic users need StrGet.ahk: ; http://www.autohotkey.com/forum/topic59738.html ; Gets either the CLR version in use, or the latest installed version. CLR_GetVersion(ByRef sVer) { VarSetCapacity(wsVer, 40) hr := DllCall("MSCorEE\GetCORVersion",A_PtrSize ? "ptr":"uint",&wsVer,"uint",20,"uint*",ccVer) sVer := StrGet(&wsVer,ccVer,"UTF-16") return hr } ; Lists the assemblies loaded into an AppDomain. CLR_GetAssemblies(pAppDomain=0) { pApp := pAppDomain ? pAppDomain : CLR_GetDefaultDomain() if !pApp return if p_App := COM_QueryInterface(pApp,"{05F696DC-2B29-3663-AD8B-C4389CF2A713}") { DllCall(NumGet(NumGet(p_App+0)+228),"uint",p_App,"uint*",pa_Asm) DllCall("oleaut32\SafeArrayGetUBound","uint",pa_Asm,"uint",1,"int*",ubound) Loop, % ubound+1 { DllCall("oleaut32\SafeArrayGetElement","uint",pa_Asm,"int*",A_Index-1,"uint*",p_Asm) DllCall(NumGet(NumGet(p_Asm+0)+112),"uint",p_Asm,"uint*",psAsm) list .= StrGet(psAsm,DllCall("oleaut32\SysStringLen","uint",psAsm),"UTF-16") "`n" COM_Release(p_Asm), COM_SysFreeString(psAsm) } DllCall("oleaut32\SafeArrayDestroy","uint",pa_Asm) } if (pAppDomain != pApp) COM_Release(pApp) return SubStr(list,1,-1) }
; Example: Enumerating loaded assemblies using CLR_L. for assembly in CLR_GetDefaultDomain().GetAssemblies() MsgBox % assembly.Location
2009/09/21 - Added list of links to examples.
2010/01/10 - Added CLR_L.
2010/06/03 - Corrected CLR_CreateObject documentation for CLR_L.
2011/02/16 - Added RuntimeVersion parameter to CLR_Start and changed default behaviour to load latest runtime version. Fixed CLR_LoadLibrary to work with file path or partial name on .NET v4.
2011/02/17 - Updated CLR_GetVersion and CLR_GetAssemblies.
2011/03/31 - v1.2 - Updated CLR_L to take advantage of native COM support.