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 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; }  
    • By Stacker
      Hi all,
      i want to start a new application that capture a screen or image from webcam an read all barcode inside the picture.
      I have test this code https://www.autoitscript.com/forum/topic/27925-webcam-example/
      Works fine for my webcam.
      Now i need to read all barcodes (code 128 code39, no 2d) inside the picture. I have found this https://www.codeproject.com/Articles/42852/Reading-Barcodes-from-an-Image-III
      It's possible to make a dll from VB source i linked  with this  parameters :  filename, Array of barcode founded, error code and launch from Autoit with function ?
      Anyone can help me to make dll and autoit function, i don't know VB.
      Thanks
    • By Terenz
      Hello,
      Someone is so kind to compile for me this DLL?
      https://github.com/stephane/libmodbus/tree/master/src/win32
      Many thanks!
    • By cdeb
      I'm trying to verify which is the default scanner and if it is different from what I need to propose a choice among those available.
      All this is possible with eztw32.dll however I can not make certain functions work
      Are these:
      int TWAIN_GetDefaultSourceName(LPSTR pzName)
      string TWAIN_DefaultSourceName()
      int TWAIN_GetSourceList(void)
      source: http://www.eztwain.com/pub/eztp/EZTwain_User_Guide.pdf
      I tried this way but they do not work:
      Local $pzName
      $pzName = DllCall($pathDLL,"int","TWAIN_DefaultSourceName") ;
      $rc = DllCall($pathDLL,"int","TWAIN_GetDefaultSourceName", "str*", $pzName) ;
      $rc = DllCall($pathDLL,"int","TWAIN_GetDefaultSourceName","str", $pzName) ;
      $rc = DllCall($pathDLL,"int","TWAIN_GetSourceList")
      $rc = DllCall($pathDLL,"int","TWAIN_GetSourceList", "NONE", "") ;
       
      Can anyone help me?
      Everything else works, here is the code:
       
      Local $pathDLL = @ScriptDir&"\_res\eztw32.dll" Local $rc = DllCall($pathDLL,'int','TWAIN_EasyVersion') if @error <> 0 then MsgBox(16+262144, "DllCall Error", "Error load eztw32.dll") else ConsoleWrite('--> eztw32.dll version: ' & $rc[0]/100 & @CRLF) $rc = DllCall($pathDLL,"int","TWAIN_IsAvailable") If @error > 0 Then MsgBox(16+262144, "DllCall Error", "Error call DLL - TWAIN_IsAvailable") if $rc[0] <> 0 then ConsoleWrite('--> TWAIN available: RC = ' & $rc[0]&@CRLF) ;;;;;;;;;;;;;;;;;;;;;; I NEED HERE ;manual select scanner Local $hwnd $rc = DllCall($pathDLL,"long","TWAIN_SelectImageSource",'hwnd',$hwnd) ConsoleWrite('--> TWAIN_SelectImageSource = ' & $rc[0]&@CRLF) if $rc[0] <> 0 then ConsoleWrite("--> TWAIN device selected"&@CRLF) else ConsoleWrite("--> no TWAIN device selected"&@CRLF) endif Else ConsoleWrite('--> TWAIN NOT available: RC = ' & $rc[0]&@CRLF) ; es: 0 endif endif thank you
×
×
  • Create New...