.NET Framework Interop (CLR, C#, VB)

Post your working scripts, libraries and tools
lexikos
Posts: 5410
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

.NET Framework Interop (CLR, C#, VB)

19 Sep 2014, 19:10

Microsoft Common Language Runtime / .NET Framework Interop

CLR v1.2, for AutoHotkey v1.1
License: public domain / CC0

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( [ RuntimeVersion ] )
Loads the Common Language Runtime. 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 [, Arg1, Arg2 ... ] )
Instantiates an object of the specified type from the specified assembly. Optionally accepts a list of arguments to pass to the object's constructor. Use ComObject(Type, Arg) to pass a typed value. A list of type codes can be found here. Alternatively, you can call Assembly.CreateInstance(sType) directly if you do not need to pass parameters.

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.
Note: Some versions of .NET may require an explicit reference to the appropriate language dll, such as Microsoft.CSharp.dll.

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".

Code: [Select all] [Expand] [Download] (CLR.ahk)GeSHi © Codebox Plus


See the original thread for older discussion.
lexikos
Posts: 5410
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: .NET Framework Interop (CLR, C#, VB)

21 Sep 2014, 02:14

Simple C# example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Simple VB example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

lexikos
Posts: 5410
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: .NET Framework Interop (CLR, C#, VB)

21 Sep 2014, 02:32

Event Handling

Handling events can be a little easier with AutoHotkey v1.1 than in AutoHotkey v1.0 thanks to SAFEARRAY support. There are also some techniques that I've learnt about since writing CLR.ahk v1.0, or that were introduced in later .NET Framework versions. These are demonstrated in the C# code below.

There are two methods of handling an event demonstrated below:

  1. For events of type EventHandler, MakeHandler can be used. This simply wraps our callback pointer in a delegate, then creates and returns an anonymous function which wraps the parameters in an array and passes them to our callback via the delegate. Since this only covers events which use the EventHandler delegate type, you would need to basically copy the MakeHandler method for each event type.
  2. For other events, we can call AddHandler, passing the target object, event name and callback pointer. AddHandler dynamically generates a CLR delegate similar to MakeHandler's anonymous function, but matching the signature of the event. This method is quite a bit longer.

In both cases, parameters are passed to our callback via a pointer to a SAFEARRAY, to make things easy. The callback pointer is passed as a string, since COM automation/IDispatch does not support 64-bit integers.

Code: [Select all] [Expand] [Download] (Event_handling.ahk)GeSHi © Codebox Plus


Code: [Select all] [Expand] [Download] (EventHelper.cs)GeSHi © Codebox Plus

User avatar
joedf
Posts: 5812
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: .NET Framework Interop (CLR, C#, VB)

13 Oct 2014, 22:12

Thanks for this. You should put this on GitHub too.
User avatar
BGM
Posts: 366
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: .NET Framework Interop (CLR, C#, VB)

31 Oct 2014, 14:10

This looks interesting! I know some c#.
The basic demo does indeed produce the messagebox! I am impressed!
So I decided to try using c# to navigate xml, so I hooked up this code, and it gives me this error:

Compilation Failed
Error CS0006 on line 0: Metadata file 'System.Xml.Linq' could not be found

How do I resolve *that*? I am guessing I need an explicit reference to the assembly. How do I add that?

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

05 Apr 2015, 19:19

Simple examples for creating/declaring variable types and return their values from C# to AHk
All of my examples assume you've used #include for the CLR library.

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


I will keep posting simple examples as I learn more.
Hope this helps people out some.
Image
IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: .NET Framework Interop (CLR, C#, VB)

05 Apr 2015, 20:18

Wow, Looks good.
I am going to give it a try.
Thanks, nice post.

(So, Main method returns "string"? interesting..)
Last edited by IMEime on 05 Jul 2017, 18:44, edited 1 time in total.
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

06 Apr 2015, 12:37

Compile an executable in C#

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

Creating a console application will be of use to display many examples found on msdn.
Image
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

06 Apr 2015, 15:02

Compiling executable that excepts arguments

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

Note: The Using keyword directive allows for each use of Console to only need the class rather than having to attach System also.
For more info see Directives
htms
Image
IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: .NET Framework Interop (CLR, C#, VB)

06 Apr 2015, 19:02

I tried two codes.

This one is fine.

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



And, the second one, I had namespace error.

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



(And, This one also..
using System.Text.RegularExpressions;
)
Last edited by IMEime on 05 Jul 2017, 18:44, edited 1 time in total.
HotKeyIt
Posts: 1500
Joined: 29 Sep 2013, 18:35
Contact:

Re: .NET Framework Interop (CLR, C#, VB)

06 Apr 2015, 19:32

This is an example from old forum:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: .NET Framework Interop (CLR, C#, VB)

06 Apr 2015, 20:36

Ho,
you made it without using "Main" method.
(Looks like some difficult to me though)

I will give it a try.
Thanks.
Last edited by IMEime on 05 Jul 2017, 18:44, edited 1 time in total.
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

06 Apr 2015, 22:46

IMEime wrote:I tried two codes.... the second one, I had namespace error.
Its because you need to reference the System.Windows.Forms assembly System.Windows.Forms.dll to use messagebox :)

HotKeyIt wrote:This is an example from old forum:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

;...
CLR_Start()
;...
only needed to specify the runtime version of .net
Image
IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: .NET Framework Interop (CLR, C#, VB)

07 Apr 2015, 04:14

Hmm..
How about this ?

My final goal is to make some Excel & Word Ribbon handling codes.
It has bunch of usings.
Do I need to wirte down all and every usings and dlls somewhere else?

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



Something like this ?

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

asm := CLR_CompileC#(c#, "System.dll 
| System.Collections.Generic.dll
| System.ComponentModel.dll
| ...
| ...dll"
)
Last edited by IMEime on 05 Jul 2017, 18:44, edited 1 time in total.
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

07 Apr 2015, 09:47

Manipulate console colors

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

Image
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

08 Apr 2015, 17:30

pass a string to SendWait() ( C# Send )

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

Image
IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: .NET Framework Interop (CLR, C#, VB)

09 Apr 2015, 19:31

RegEx.

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

Last edited by IMEime on 05 Jul 2017, 18:45, edited 1 time in total.
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

23 May 2015, 18:28

Return character descriptions
Image
IMEime
Posts: 411
Joined: 20 Sep 2014, 06:15

Re: .NET Framework Interop (CLR, C#, VB)

23 May 2015, 19:07

TLM>
Thanks for the nice codes.

I have errors for a Chinese string;

"一二三"
0x4E00 一 (One)
0x4E8C 二 (Two)
0x4E09 三 (Three)

Any good tips ?

Error descriptions is something like this;
"There are not resource name in image file."
User avatar
TLM
Posts: 1140
Joined: 01 Oct 2013, 07:52

Re: .NET Framework Interop (CLR, C#, VB)

23 May 2015, 20:25

Only the 1st character is available from that resource.
Image
CharSet.Unicode does nothing..
I'm trying to figure where to get other character descriptions.
Image

Return to “Scripts and Functions”

Who is online

Users browsing this forum: DataLife, Maave and 14 guests