boludoz Posted May 16, 2022 Share Posted May 16, 2022 (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 May 16, 2022 by boludoz Link to comment Share on other sites More sharing options...
RTFC Posted May 16, 2022 Share Posted May 16, 2022 (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#. Edited May 16, 2022 by RTFC clarifications & typos My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
boludoz Posted May 16, 2022 Author Share Posted May 16, 2022 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 comment Share on other sites More sharing options...
Danyfirex Posted May 16, 2022 Share Posted May 16, 2022 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 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Solution LarsJ Posted May 17, 2022 Solution Share Posted May 17, 2022 (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 May 17, 2022 by LarsJ DinFuv, mLipok, boludoz and 1 other 2 2 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
pixelsearch Posted May 17, 2022 Share Posted May 17, 2022 @LarsJ we missed you. Glad to have you back after 4 months and let's hope everything is alright for you Link to comment Share on other sites More sharing options...
boludoz Posted May 17, 2022 Author Share Posted May 17, 2022 (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 May 17, 2022 by boludoz Link to comment Share on other sites More sharing options...
argumentum Posted May 18, 2022 Share Posted May 18, 2022 (edited) 3 hours ago, boludoz said: AutoIt can execute DLLs made in the net framework, and I can tell because I've seen it. Cool, so, can you post code to show how is done straight from AutoIt ? Edited May 18, 2022 by argumentum can't remove the link =/ Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
jchd Posted May 18, 2022 Share Posted May 18, 2022 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 hereRegExp tutorial: enough to get startedPCRE 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 comment Share on other sites More sharing options...
boludoz Posted May 18, 2022 Author Share Posted May 18, 2022 (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 May 18, 2022 by boludoz Link to comment Share on other sites More sharing options...
jchd Posted May 19, 2022 Share Posted May 19, 2022 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. boludoz 1 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 hereRegExp tutorial: enough to get startedPCRE 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 comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now