Jump to content

Make Net c# DLL and call


Go to solution Solved by LarsJ,

Recommended Posts

Posted (edited)
using System.Runtime.InteropServices;

namespace dllSource
{
    public class Operation
    {

        [ComVisible(true)]
        public int Add(int x, int y)
        {
            int z;
            if ((x == 10) && (y == 20))
            {
                z = x + y;
            }
            else
            {
                z = x;
            }
            return z;
        }
    }
}

 

Local $aResult = DllCall("HelloWordDll.dll", "str", "Operation", "int", 2, _
                "int", 1)
ConsoleWrite("Error : " & @error & @CRLF)
ConsoleWrite("Result : " & $aResult & @CRLF)

Hello, can someone explain to me how to call this DLL, or how to call it, or how to fix the DLL, I'm new to C# but I know it's possible.

Edited by boludoz
Link to post
Share on other sites
Posted (edited)
Local $aResult = DllCall("HelloWordDll.dll", "int:cdecl", "Add", "int", 2, _
                "int", 1)
ConsoleWrite("Error : " & @error & @CRLF)
if IsArray($aResult) Then ConsoleWrite("Result : " & $aResult[0] & @CRLF)

Untested (never used C# in my life; not going to either). Calling convention is cdecl; you return an int, not a string. AutoIt DllCall func returns an array if successful. Check that you compile as dll (not as exe!) and with the correct bitness (x86 vs x64).

EDIT: Never mind; my suggestion would work with C++, not C#.:lol:

Edited by RTFC
clarifications & typos
Link to post
Share on other sites
53 minutes ago, RTFC said:
Local $aResult = DllCall("HelloWordDll.dll", "int:cdecl", "Add", "int", 2, _
                "int", 1)
ConsoleWrite("Error : " & @error & @CRLF)
if IsArray($aResult) Then ConsoleWrite("Result : " & $aResult[0] & @CRLF)

Untested (never used C# in my life; not going to either). Calling convention is cdecl; you return an int, not a string. AutoIt DllCall func returns an array if successful. Check that you compile as dll (not as exe!) and with the correct bitness (x86 vs x64).

Sorry, I can't figure out how to find the function I'm calling, thanks, there must be something in my compiler.

Link to post
Share on other sites

You can't do it that way.  You would need to register a COM then use ObjCreate. You can use C# using CLR. check DotNet.

 

Saludos

Link to post
Share on other sites
  • Solution
Posted (edited)

The AutoIt DllCall() function can only be used to execute functions in dll-files implemented in unmanaged languages such as C, C++ and Pascal. The DllCall() function cannot be used to execute functions in .NET assembly dll-files implemented in managed languages such as C#, F# or VB.NET.

With an AutoIt background, the DotNetAll.au3 UDF as implemented in Using C# and VB.NET Code is by far the easiest way to execute C# or VB.NET code directly within an AutoIt script.

This is a slightly simplified version of the C# code. COM visibility is handled by the DotNetAll.au3 UDF. Test.cs

using System;

public class Operation
{
  public int Add(int x, int y)
  {
    int z;
    if ((x == 10) && (y == 20))
    {
      z = x + y;
    }
    else
    {
      z = x;
    }
    return z;
  }
}

And the AutoIt code. Test.au3

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=Y

Opt( "MustDeclareVars", 1 )

#include "DotNetAll.au3"

Example()

Func Example()
  ; Create $oOperation object
  Local $oNetCode = DotNet_LoadCScode( FileRead( "Test.cs" ), "System.dll" )
  Local $oOperation = DotNet_CreateObject( $oNetCode, "Operation" )

  ; Now the Add() function can be executed
  Local $z = $oOperation.Add( 10,  3 )
  ConsoleWrite( "$oOperation.Add( 10,  3 ) = " & $z & @CRLF )

  $z = $oOperation.Add( -5, 20 )
  ConsoleWrite( "$oOperation.Add( -5, 20 ) = " & $z & @CRLF )

  $z = $oOperation.Add( 10, 20 )
  ConsoleWrite( "$oOperation.Add( 10, 20 ) = " & $z & @CRLF )
EndFunc

Run the AutoIt code in SciTE with F5. The C# code doesn't need to be registered. Output in SciTE console: 

$oOperation.Add( 10,  3 ) = 10
$oOperation.Add( -5, 20 ) = -5
$oOperation.Add( 10, 20 ) = 30

The benefits of the DotNetAll.au3 UDF are:

  • The .NET source code can be easily written in SciTE without the need for a specific development tool
  • The .NET code is compiled and executed directly in your AutoIt script, where you apply the code
  • If the AutoIt script is run in SciTE with F5, .NET error messages appear in the console
  • The .NET code can be easily distributed in the form of source files (text files)
  • You avoid the registration/unregistration tyranny of .NET assembly dll-files

Another way to execute C# code is briefly described in this post. This method requires an IDE to compile the C# code into a .NET assembly dll-file and it requires the dll-file to be registered/unregistered.

All required code is contained in the 7z-file:
Operation.7z

Edited by LarsJ
Link to post
Share on other sites
Posted (edited)
7 hours ago, LarsJ said:

The AutoIt DllCall() function can only be used to execute functions in dll-files implemented in unmanaged languages such as C, C++ and Pascal. The DllCall() function cannot be used to execute functions in .NET assembly dll-files implemented in managed languages such as C#, F# or VB.NET.

With an AutoIt background, the DotNetAll.au3 UDF as implemented in Using C# and VB.NET Code is by far the easiest way to execute C# or VB.NET code directly within an AutoIt script.

This is a slightly simplified version of the C# code. COM visibility is handled by the DotNetAll.au3 UDF. Test.cs

using System;

public class Operation
{
  public int Add(int x, int y)
  {
    int z;
    if ((x == 10) && (y == 20))
    {
      z = x + y;
    }
    else
    {
      z = x;
    }
    return z;
  }
}

And the AutoIt code. Test.au3

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=Y

Opt( "MustDeclareVars", 1 )

#include "DotNetAll.au3"

Example()

Func Example()
  ; Create $oOperation object
  Local $oNetCode = DotNet_LoadCScode( FileRead( "Test.cs" ), "System.dll" )
  Local $oOperation = DotNet_CreateObject( $oNetCode, "Operation" )

  ; Now the Add() function can be executed
  Local $z = $oOperation.Add( 10,  3 )
  ConsoleWrite( "$oOperation.Add( 10,  3 ) = " & $z & @CRLF )

  $z = $oOperation.Add( -5, 20 )
  ConsoleWrite( "$oOperation.Add( -5, 20 ) = " & $z & @CRLF )

  $z = $oOperation.Add( 10, 20 )
  ConsoleWrite( "$oOperation.Add( 10, 20 ) = " & $z & @CRLF )
EndFunc

Run the AutoIt code in SciTE with F5. The C# code doesn't need to be registered. Output in SciTE console: 

$oOperation.Add( 10,  3 ) = 10
$oOperation.Add( -5, 20 ) = -5
$oOperation.Add( 10, 20 ) = 30

The benefits of the DotNetAll.au3 UDF are:

  • The .NET source code can be easily written in SciTE without the need for a specific development tool
  • The .NET code is compiled and executed directly in your AutoIt script, where you apply the code
  • If the AutoIt script is run in SciTE with F5, .NET error messages appear in the console
  • The .NET code can be easily distributed in the form of source files (text files)
  • You avoid the registration/unregistration tyranny of .NET assembly dll-files

Another way to execute C# code is briefly described in this post. This method requires an IDE to compile the C# code into a .NET assembly dll-file and it requires the dll-file to be registered/unregistered.

All required code is contained in the 7z-file:
Operation.7z

With the greatest respect and admiration for your work, that statement is not correct, AutoIt can execute DLLs made in the net framework, and I can tell because I've seen it.

P.S, I loved your solution so much that I'm going to mark it. What you did is a work of art!

Edited by boludoz
Link to post
Share on other sites
6 hours ago, boludoz said:

AutoIt can execute DLLs made in the net framework

Please post a proof of concept!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites
Posted (edited)
14 hours ago, jchd said:

Please post a proof of concept!

 

16 hours ago, argumentum said:

Cool, so, can you post code to show how is done straight from AutoIt ?

Using this, https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

https://github.com/3F/DllExport

Unfortunately I don't know much about DLL and C#.

For example:

class Test
{
  [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
  public static int TestExport(int left, int right)
  {
     return left + right;
  } 
}

The other way would be with a kind of wrapper.

Edited by boludoz
Link to post
Share on other sites

Yes you can use the nuget package and restrict your C# to (more or less) C-compatible code, but still without #-style marshalling, overloading or recursion.

At this point you'd rather just produce the DLL from plain C.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...