Jump to content
Luigi

How generate a error code from C# dll to macro @error in AutoIt?

Recommended Posts

Greetings, someone can give a exemple, how send a error from a C#'s dll to AutoIt?

I use this line, to send an error... but, I want get a error code In AutoIt with macro @error, it's possible?

 

throw new ArgumentException("arquivo map não existe", "value" );

In this way, work, I know ther are error, but, @errror always is zero.

I don't want this, I want a number as error code.

Can you help me?

 

Best regards

Share this post


Link to post
Share on other sites

@Jos, thank you for reply, this is the code:

Obs: this is not game automation, this is my game writed in AutoIt.

https://www.youtube.com/watch?v=dq8UWEDU7q8

I want re-write some code in C#/dll.

AutoIt:

Global Const $sNameSpace = "Borius"
Global Const $sClass = "Engine"
Global Const $BORIUS_DLL = """" & @ScriptDir & "\borius.dll"""
ConsoleWrite($BORIUS_DLL & @LF)
Global Const $REGASM = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe"
;~ RunWait($REGASM & " /unregister " & $BORIUS_DLL, @ScriptDir, @SW_HIDE)

;~ Global $PID = RunWait($REGASM & " /register /codebase /tlb " & $BORIUS_DLL, @ScriptDir, @SW_HIDE)
;~ Global $PID = RunWait($REGASM & "  /codebase " & $BORIUS_DLL, @ScriptDir, @SW_HIDE)
Global $PID = Run(@ComSpec & " /c " & $REGASM & " /register /codebase /tlb " & $BORIUS_DLL, @ScriptDir,  @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD + $STDERR_MERGED)

ProcessWaitClose($PID)
Local $sOutput = StdoutRead($PID)
ConsoleWrite($sOutput & @LF)

Global $wer = ObjCreate($sNameSpace & "." & $sClass)
If @error Or Not IsObj($wer) Then
    Local $sOutput = StdoutRead($PID)
    ConsoleWrite($sOutput & @LF)
    ConsoleWrite($sNameSpace & "." & $sClass & " @error[" & @error & "]" & @LF)
    Exit
Else
    ConsoleWrite($sNameSpace & "." & $sClass & " @ok" & @LF)
EndIf

The dll:

/*
 * Criado por SharpDevelop.
 * Usuário: master
 * Data: 14/09/2017
 * Hora: 18:21
 * 
 * Para alterar este modelo use Ferramentas | Opções | Codificação | Editar Cabeçalhos Padrão.
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace borius{
    /// <summary>
    /// Description of MyClass.
    /// </summary>
    public class Engine{
    private string _map;
    
    private int height;
    //private int layers;
    private int nextobjectid;
    private string orientation;
    //private int properties;
    //private int propertytypes;
    private int renderorder;
    private int tiledversion;
    private int tileheight;
    //private int tilesets;
    private int tilewidth;
    private string type;
    private int version;
    private int width;
        
        dynamic json;
        
        
        
        public string map{
            get{ return _map; }
            set{
                value = value.Replace("\\", "\\\\");
                if( !File.Exists(value) ){
                    throw new ArgumentException("arquivo map não existe", "value" );
                }
                JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            
                using (StreamReader r = new StreamReader(value)) {
                    string str = r.ReadToEnd();
                    json = serializer.DeserializeObject(str);
                }
                _map_build();
                _map = value;
            }
        }
        

        public void _map_build(){
            foreach (KeyValuePair<string, object> entry in json){
                var key = entry.Key;
                switch( key ){
                    case "height" :
                        height = Int32.Parse(entry.Value as string);
                        break;
                    case "layers" :
                    case "nextobjectid" :
                        nextobjectid = Int32.Parse(entry.Value as string);
                        break;
                    case "orientation" : 
                    case "properties" : 
                    case "propertytypes" : 
                    case "renderorder" : 
                    case "tiledversion" :
                    case "tileheight" :
                        tileheight = Int32.Parse(entry.Value as string);
                        break;
                    case "tilesets" : 
                    case "tilewidth" : 
                        tilewidth = Int32.Parse(entry.Value as string);
                        break;
                    case "type" :
                    case "version" :
                        version = Int32.Parse(entry.Value as string);
                        break;
                    case "width" :
                        width = Int32.Parse(entry.Value as string);
                        break;

                }
                
                
                var value = entry.Value as string;
                Console.WriteLine(String.Format("{0} : {1}",key, value));
            }
            
        }
        
    }
}

 

Share this post


Link to post
Share on other sites

Instead of a void dll function, make it return an int to AutoIt containing a status code, e.g., 0=success, 1-n = various error codes. Capture this in your dllcall from autoit ($return=DllCall(...), $myErrorCode=$return[0]), and use SetError to fill the @error macro). Letting your dll write to console and AutoIt capturing that seems needlessly circuitous (and potentially error-prone), since dllcalls are designed to return information to the caller.

Share this post


Link to post
Share on other sites

Thank you for your reply @RTFC, but I thinking dll writed in C++, you work like you say.

I my opinion, dll writed in C#, work throug COM (componente object model), see this:

http://forum.autoitbrasil.com/index.php?/topic/1869-criando-uma-dll-em-c-csharp/

Any "throw new xxxxxx" break the script, and not set AutoIt's @error.

Your way is smart, but I still unknow hos set @error code.

If I wrong, apologize-me, it's new for me.

Edited by Luigi

Share this post


Link to post
Share on other sites

Luigi, You are right. C# code is managed .NET code and a C# DLL-file is a .NET assembly DLL-file and can as such not be used by DllCall. DllCall can be used to call functions implemented in unmanaged code in conventional DLL-files like the DLL-files in the Windows operating system that implements Windows API functions.

C# is an object oriented language and everything is done through objects, properties and methods. To call a method in a C# DLL-file you create an object and executes the object method.

A general and easy way to set the @error macro when an error occurs in C# code is demonstrated in this pseudocode (cannot be run):

// Use a global variable and a separate
// function to pass an error code to AutoIt.

using System;
class Au3Class {
  int iErrorCode = 0;

  public int GetErrorCode() {
    return iErrorCode;
  }

  public void Method1() {
    iErrorCode = 0; // Initialize iErrorCode
    Console.WriteLine( "Method1 without errors" );
  }

  public void Method2() {
    iErrorCode = 0; // Initialize iErrorCode
    Console.WriteLine( "Method2 with an error" );
    iErrorCode = 1; // Set iErrorCode on error
  }
}
Example()
If @error Then
  ConsoleWrite( "@error = " & @error & @CRLF )
  Exit ; Exit on error
EndIf

Func Example()
  Local $iError

  $oAu3Class.Method1() ; Call the actual method
  $iError = $oAu3Class.GetErrorCode() ; Check error code
  If $iError Then Return SetError( $iError, 0, -1 )
  ; No errors, continue Example()
  ConsoleWrite( "No Errors" & @CRLF & @CRLF )

  $oAu3Class.Method2() ; Call the actual method
  $iError = $oAu3Class.GetErrorCode() ; Check error code
  If $iError Then Return SetError( $iError, 0, -1 )
  ; No errors, continue Example()
  ConsoleWrite( "No Errors" & @CRLF & @CRLF )
EndFunc

 

Share this post


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

  • Similar Content

    • By RAMzor
      Hello all!
      I got the Acroname Programmable USB Hub based on BrainStem platform. I will use it to manage ports and read current.
      They have development kit (BRAINSTEM DEVELOPMENT KIT) with examples for any languages like C#, C++, LabVIEW and python but no AutoIt.
      The kit include BrainStem2.dll (x32 and x64), BrainStem2_LabVIEW.dll, BrainStem2CLI.dll and many examples.
      Anyone used this dll and implemented it in AutoIt?
      Please share some example for Device Discovering, User LED Flashing or power enable/disable on port to start play with this HUB
      BrainStem Reference 
       
      BrainStem2 lib+example.7z USB HUB User Manual s79-usbhub-3p_v1.10_0.pdf
    • By matwachich
      Hi guys!
      A pretty simple UDF to convert HTML to PDF using wkHTMLtoPDF.
      It uses the C API of the tool (DLL), so no external process, no ActiveX or COM sh*t.
      See the example, and the documentation of wkHTMLtoPDF.
      Cheers
      https://github.com/matwachich/wkhtmltopdf-au3
    • By Skysnake
      This is relevant
      From here https://stackoverflow.com/questions/3454315/is-it-possible-to-pin-a-dll-in-memory-to-prevent-unloading
      I use several UDFs on the Forum to do various things.  Those UDFs work very well.
      Effectively the UDFs are DLL wrappers, that make it possible to access DLL functions easily without the long hard slog of DLLCall() every time.
      However, I have now run into the issue that multiple UDF DLLCalls are slow. Not mind numbingly slow, but slow enough to become noticeable with a large of repeated function calls.
      So I was wondering, is it possible to "load a DLL into memory" and leave it there for the duration of my script's lifetime, avoid repeated DLL on-disk reads with a persistent in memory DLL?
      From Microsoft
      https://docs.microsoft.com/en-us/windows/desktop/dlls/about-dynamic-link-libraries
      Looks like what I want to do is: load-time dynamic linking,
      So next question, (a) how do I do this with AutoIt (b) How would this impact on standard AutoIt type DLL calls?
       
      The point is speed.  Is there a different approach?
      Or am I barking up the wrong tree?
      Skysnake
    • By DiegoCorradini
      Hi all,
      I have a problem to handle the controls of an application.
      Using AutoIT Windows Tool I can get only the Window (see Summary of the picture).
      Any tips to get the controls without knowing the name?
      (PS Using TestStack.White everything works, however I want the HIDE application feature of AutoIT). 
       
      Many thanks

    • By bladem2003
      Hello,
      i need help to translate the c code to autoit .
      I don't understand the callback function.
       
      #include <windows.h> #include <stdio.h> // native IR Data by PAnsiChar typedef void CALLBACK CallBackPAnsiChar(char*, char*, char*, char*); typedef int (__stdcall *impInitPAnsiChar)(CallBackPAnsiChar); CALLBACK MyCallBackPAnsiChar(char* Protocol, char* Address, char* Command, char* Flags) { printf("\nIR Data received: Protocol: %s, Address: 0x%s, Command: 0x%s, Flags: 0x%s", Protocol, Address, Command, Flags); fflush(stdout); } int main(int argc, char **argv) { impInitPAnsiChar InitPAnsiChar = NULL; // Load DLL file HINSTANCE hinstLib = LoadLibrary(TEXT("USB_IR_Remote_Receiver.dll")); if (hinstLib == NULL) { printf("\nERROR: unable to load DLL\n"); return 1; } // Get function pointer InitPAnsiChar InitPAnsiChar = (impInitPAnsiChar)GetProcAddress(hinstLib, "InitPAnsiChar"); if (InitPAnsiChar == NULL) { printf("\nERROR: unable to find DLL function\n"); FreeLibrary(hinstLib); return 1; } if (InitPAnsiChar(*MyCallBackPAnsiChar)) { printf("\nInit DLL with InitPAnsiChar successfull"); } else { // Unload DLL file FreeLibrary(hinstLib); return 0; } while(1) { } //return 0; }  
×
×
  • Create New...