Jump to content

Create a .net class and run it as object from your AutoIt script


paulpmeier
 Share

Recommended Posts

Write a code file in your @ScriptDir like hello.vb

Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Namespace myDotNetLibrary
  <ClassInterface(ClassInterfaceType.AutoDual)> _
  Public Class myDotNetClass
    Private myProperty As String

    Public Sub New()
    End Sub

    Public Function myDotNetMethod(input As String) As String
      Return "Hello " & input
    End Function

    Public Property myDotNetProperty() As String
      Get
        Return myProperty
      End Get
      Set(ByVal value As String)
        myProperty = value
      End Set
    End Property

  End Class
End Namespace

and run it from your AutoIt script

$vbc = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\vbc.exe" ;  ; check the path of your version
$RegAsm = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" ; check the path of your version

RunWait($vbc & " /target:library hello.vb", @ScriptDir, @SW_HIDE) ; compile the .net DLL
RunWait($RegAsm & " /codebase hello.dll", @ScriptDir, @SW_HIDE) ; register the .net DLL

$obj = ObjCreate("myDotNetLibrary.myDotNetClass")
$obj.myDotNetProperty = "AutoIt-World"
ConsoleWrite("! " & $obj.myDotNetMethod($obj.myDotNetProperty) & @CRLF)

RunWait($RegAsm & " /unregister hello.dll", @ScriptDir, @SW_HIDE) ; unregister the .net DLL

dito for C#: multsub.cs

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace myCSLibrary
{
  [ClassInterface(ClassInterfaceType.AutoDual)]
  public class MultSubClass
  {
    private int myProperty;
    public MultSubClass()
    {
    }
    public int mult2(int input)
    {
      return 2 * input;
    }
    public int sub1(int input)
    {
      return input - 1;
    }

    public int myDotNetProperty {
      get { return myProperty; }
      set { myProperty = value; }
    }
  }
}

and the AutoIt script:

$csc = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe" ; check the path of your version
$RegAsm = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" ; check the path of your version

RunWait($csc & " /target:library multsub.cs", @ScriptDir, @SW_HIDE) ; compile the .net DLL
RunWait($RegAsm & " /codebase multsub.dll", @ScriptDir, @SW_HIDE) ; register the .net DLL

$obj = ObjCreate("myCSLibrary.MultSubClass")
$obj.myDotNetProperty = 3
ConsoleWrite("! " & $obj.sub1($obj.mult2($obj.sub1($obj.mult2($obj.myDotNetProperty)))) & @CRLF)

RunWait($RegAsm & " /unregister multsub.dll", @ScriptDir, @SW_HIDE) ; unregister the .net DLL

Tested with Windows XP SP3 and .net Framework 3.5/4

Paul

DotNetDLLs.zip

Edited by paulpmeier
Link to comment
Share on other sites

  • 5 months later...

Hi paulpmeier,

what a shame, that I've never seen before this thread. :D

I like this idea, to make NET available with AutoIt. Because that, I feeled me free to cast this in an function - hope you like it.

The function loads given .vb or .cs file and register the related dll file. On AutoIt-Exit or new call of this function, the dll will unregister.

I've implemented an automatically NET-detection. On my system (Win7 pro, 32bit, SP1) the required files only included in NET2.0 and 4.0. Because that, I ask only for this installation folder.

Here is the function:

#Region - TimeStamp
; 2011-11-08 13:43:46   v 0.1
#EndRegion - TimeStamp

; ==
; Idea integration NET.Framework: paulpmeier (http://www.autoitscript.com/forum/topic/129164-create-a-net-class-and-run-it-as-object-from-your-autoit-script/)
; ==
;===============================================================================
; Script Name......: NETFramework.au3
; Description......: Run a .net class as object from AutoIt script
; AutoIt version...: 3.3.6.1
; Author(s)........: paulpmeier, (BugFix - cast into a function, automated NET-detection)
;===============================================================================

#include-once
#include <File.au3>

OnAutoItExitRegister('__UnLoad_NET_Dll')

Global $__User_NET_Dll = '', $__RegAsmPath

;===============================================================================
; Function Name....: _NETFramework_Load
; Description......: Loads .vb or .cs file into a library and register them
;                   If another one always registered - will unregister that at first
; Parameter(s).....: $sVB_CS_File  Name of .vb/.cs file
; Requirement(s)...: $sVB_CS_File must be stored in @ScriptDir
; Return Value(s)..: Succes   1
;                   Failure  0  set @error - 1  wrong filetype given
;                                            2  given file not exists
;                                            3  NET Framework 2.0 or 4.0 required, not installed
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _NETFramework_Load($sVB_CS_File) ; == $sVB_CS_File must stored in @ScriptDir!!
    If $__User_NET_Dll <> '' Then __UnLoad_NET_Dll()
    Local $sExt = StringRight($sVB_CS_File, 3)
    If Not StringInStr('.vb .cs', $sExt) Then Return SetError(1,0,0) ; == .vb / .cs required
    If Not FileExists(@ScriptDir & '\' & $sVB_CS_File) Then Return SetError(2,0,0) ; == file does not exist
    Local $sRoot = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework", "InstallRoot")
    If @error Then Return SetError(3,0,0) ; == NET Framework 2.0 or 4.0 required!
    Local $aFolder = _FileListToArray($sRoot , "*", 2), $sNETFolder = ''
    For $i = $aFolder[0] To 1 Step -1
        If StringRegExp($aFolder[$i], "v4\.0\.\d+", 0) Then
            $sNETFolder = $aFolder[$i]
            ExitLoop
        ElseIf StringRegExp($aFolder[$i], "v2\.0\.\d+", 0) Then
            $sNETFolder = $aFolder[$i]
            ExitLoop
        EndIf
    Next
    If $sNETFolder = '' Then Return SetError(3,0,0) ; == NET Framework 2.0 or 4.0 required!
    If $sExt = '.vb' Then
        RunWait($sRoot & $sNETFolder & "\vbc.exe /target:library " & $sVB_CS_File, @ScriptDir, @SW_HIDE) ; compile the .net DLL
    Else
        RunWait($sRoot & $sNETFolder & "\csc.exe /target:library " & $sVB_CS_File, @ScriptDir, @SW_HIDE) ; compile the .net DLL
    EndIf
    $__User_NET_Dll = StringTrimRight($sVB_CS_File, 2) & 'dll'
    $__RegAsmPath = $sRoot & $sNETFolder & "\RegAsm.exe"
    RunWait($__RegAsmPath & " /codebase " & $__User_NET_Dll, @ScriptDir, @SW_HIDE) ; register the .net DLL
    Return 1
EndFunc  ;==>_NETFramework_Load

Func __UnLoad_NET_Dll()
    RunWait($__RegAsmPath & " /unregister " & $__User_NET_Dll, @ScriptDir, @SW_HIDE) ; unregister the .net DLL
EndFunc

Now your example looks so:

#include 'NETFramework.au3'

_NETFramework_Load('hello.vb')
$obj = ObjCreate("myDotNetLibrary.myDotNetClass")
$obj.myDotNetProperty = "AutoIt-World"
ConsoleWrite("! " & $obj.myDotNetMethod($obj.myDotNetProperty) & @CRLF)


_NETFramework_Load('multsub.cs') ; == unload automatically "hello.vb"
$obj = ObjCreate("myCSLibrary.MultSubClass")
$obj.myDotNetProperty = 3
ConsoleWrite("! " & $obj.sub1($obj.mult2($obj.sub1($obj.mult2($obj.myDotNetProperty)))) & @CRLF)

But now I've a question:

To create own classes and use them - it works fine. Have you always tried to get access to existing NET classes on this way?

NETFramework0.1.au3

Best Regards BugFix  

Link to comment
Share on other sites

Hallo BugFix,

danke für Ihre schöne Funktion, sie macht die Einbindung von .Net-DLLs in AutoIt-Skripte einfacher und eleganter.

Ich bin nicht der große .Net-Experte und da im Mai keiner auf diesen Post hier reagierte, habe ich das Thema auch nicht weiterverfolgt.

Was den Zugriff auf vorhandene .Net-Klassen angeht, habe ich noch versucht die Ideen von

Robert Giseke umzusetzen. Giseke hat ein C# Project Template for Unmanaged Exports geschrieben, mit dem ich eine .Net-DLL erzeugt habe, die auf die DateTime.AddDays-Methode zugreift.

http://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

Anbei meine ungeordneten Experimente incl. Links.

Ich hoffe, das hilft Ihnen etwas weiter. Für tiefergehende Informationen müsste ich mich leider erst selbst wieder in die Thematik einarbeiten.

Viele Grüße

Paul

UnmanagedExports.zip

Link to comment
Share on other sites

Vielen Dank Paul, für Code und Links.

Ich werde in nächster Zeit versuchen mich in diese Thematik etwas tiefer einzuarbeiten. Ich möchte einige NET-Controls in AutoIt verfügbar machen (z.B. Progressbar mit Text-Eigenschaft). Sollten diese Versuche Erfolg haben, werde ich das auch im Forum posten.

@all

So, that you have a chance to understand:

My destination is to learn something more about NET and, if possible, create functions to get acces of some NET-controls (i.e. progress bar with text-property).

If successful, i'll post it.

Best Regards BugFix  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...