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

Post your working scripts, libraries and tools for AHK v1.1 and older
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

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

10 Mar 2017, 18:04

Why ask the question when it's so simple to just change the code and see what happens?

Yes, it's possible.
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

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

10 Mar 2017, 21:58

I can't imagine what would cause that.

Modifying my example to use a BoundFunc is very simple:
  • Obviously, if handler (on the C# side) is a BoundFunc, handler.Handle() won't work. Change it to handler() or handler.call().
  • Change util.Test(Handler) to util.Test(Handler.Handle.Bind(Handler)).
raskun
Posts: 13
Joined: 01 Mar 2017, 20:30

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

12 Mar 2017, 07:58

How to use this when my main ahk script was referencing to calling to another ahk script?
b0bi
Posts: 7
Joined: 07 Apr 2016, 13:11

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

20 Mar 2017, 10:14

This seems very interesting, but the download link is dead :/
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

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

20 Mar 2017, 11:31

Confirming dead link 03/20/17 at 10:31 MST
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

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

20 Mar 2017, 17:04

Whee! C# is fun!

I am currently mucking around with Managed Extensibility Framework (MEF). MEF is basically a framework to make it easy to write plugins which can advertise their capabilities etc.

I have a post here if anyone is interested in how to rig up a DLL which can load other DLLs as plugins - it's pretty easy:

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace InputWrapper
{
    public class InputWrappers
    {
        public interface IInputWrapper
        {
            int GetButtonCount();
            bool Subscribe();
        }

        [Export(typeof(IInputWrapper))]
        [ExportMetadata("Name", "SharpDX_DirectInput")]
        public class SharpDX_DirectInput : IInputWrapper
        {
            public int GetButtonCount()
            {
                return 128;
            }

            public bool Subscribe()
            {
                return true;
            }

        }

        [Export(typeof(IInputWrapper))]
        [ExportMetadata("Name", "SharpDX_XInput")]
        public class SharpDX_XInput : IInputWrapper
        {
            public int GetButtonCount()
            {
                return 10;
            }

            public bool Subscribe()
            {
                return true;
            }

        }

        public interface IInputWrapperMetadata
        {
            string Name { get; }
        }
    }

    public class SubscriptionHandler
    {
        [ImportMany(typeof(InputWrappers.IInputWrapper))]
        IEnumerable<Lazy<InputWrappers.IInputWrapper, InputWrappers.IInputWrapperMetadata>> _interfaceMeta;

        public SubscriptionHandler()
        {
            Compose();
            foreach (Lazy<InputWrappers.IInputWrapper, InputWrappers.IInputWrapperMetadata> inputWrapper in _interfaceMeta)
            {
                if (inputWrapper.Metadata.Name == "SharpDX_DirectInput")
                {
                    Console.WriteLine(inputWrapper.Value.GetButtonCount());
                }
            }
            Console.WriteLine();

            Console.ReadKey();
        }

        private void Compose()
        {
            AssemblyCatalog catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            CompositionContainer container = new CompositionContainer(catalog);
            container.SatisfyImportsOnce(this);
        }
    }
}
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

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

21 Mar 2017, 05:18

The download link was to my Dropbox public folder. Dropbox killed all public folders.

I've replaced the link. (It just links to the download function of the code box at the bottom of my post now.)
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

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

21 Mar 2017, 10:05

Thanks, lexikos.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

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

26 Apr 2017, 13:36

How would I load a DLL that loads either a 64 bit or 32 bit DLL?

I'm getting a cannot load file or assembly error, and loading the 64-bit DLL directly returns error "'00.dll' could not be opened -- ‘An attempt was made to load a program with an incorrect format."
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

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

01 May 2017, 01:39

Alright. After some extensive testing, code that works fine in PowerShell gives me an error in AHK. Upon loading an external Dll, The object is successfully created using CLR_CreateObject, but any calls to the created object fail. (Ex. obj.Version does not return the version number. Works fine in Powershell).

I tried debugging the object, and the DispatchType and IID fields are blank.

If anyone can help, I'm trying to convert this code. https://github.com/jourdant/powershell-paperless (DLLS are downloaded via powershell script.)

Code: Select all

#include CLR.ahk
tesslibrary := CLR_LoadLibrary(A_ScriptDir "\lib\Tesseract.dll")
tess := CLR_CreateObject(tesslibrary, "Tesseract.TesseractEngine", "lib\tessdata", "eng")
MsgBox % tess.Version
; Error 0x80131509

/* Powershell
Add-Type -Path ".\Lib\Tesseract.dll"
$tesseract = New-Object Tesseract.TesseractEngine("Lib\tessdata", "eng")
$text = $tesseract.Version
Write-Output $text
*/
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

03 Jun 2017, 19:57

I am wondering which versions of Windows OS CLR applies to? From general knowledge I know that CLR 4.5 is installed on all Windows 10 operating systems, however I was wondering how far back this goes? CLR 1.0 appears to go back to Windows 98 OS, but I have no idea whether CLR.AHK will work with the functions provided by CLR 1.0. (P.S. I don't expect Windows 98 to be supported... :p)
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

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

06 Jun 2017, 16:52

I manged to figure out how to get a message pump going in a C# DLL with no visible GUI.
In this post, I demo using it to allow my code to process RawInput (WM_INPUT) messages, and fire a callback to AHK, though I would imagine this technique would be handy for other things too.
My code uses SharpDX, so that seems to preclude the need for implementing a message processing loop, but the code has links to an article which shows how that is done.
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

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

06 Jun 2017, 17:03

@iseahound - have you tried running the AHK script in Scite4AutoHotkey?
I get what looks like exception messages in the output pane when using CLR, so that may yield some useful info.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

23 Jun 2017, 05:33

How do you parse a List object from CLR to AHK?

Code: Select all

code =
(
    Imports System
    Imports System.Collections.Generic
    Class MainClass
        Function TestList() as List(Of String)
            return New List(Of String)(New String(){"This","is","a","list"})
        end function
    end class
)

asm := CLR_CompileVB(code, "System.dll")
obj := CLR_CreateObject(asm,"MainClass")
list = obj.TestList()

msgbox, % ComObjType(list) ; returns VT_DISPATCH
msgbox, % list.count() ; VB.NET methods don't return values?
msgbox, % list.item(0) ; ""
msgbox, % list.item(1) ; ""
Last edited by sancarn on 23 Jun 2017, 05:39, edited 1 time in total.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 140 guests