Welcome, Guest
Username: Password: Remember me
Hier wird Deutsch gesprochen

TOPIC:

Zugriff auf SmartCard Reader mit PCSC 19 Apr 2022 12:03 #22211

  • lagraf
  • lagraf's Avatar
  • Topic Author


  • Posts: 338
  • Ich versuche grad ein paar C# Beispiele nach X# zu übersetzen um auf einen USB SmartCardReader zuzugreifen. Dabei bin ich auf einen Code gestossen, den ich nicht übersetzen kann:
    int.TryParse(relin, out num)
    In X# bekomme ich den Fehler "The name 'int' does not exist in the current context" (relin und num existieren).
    Wie müßte der Code für X# lauten?

    Please Log in or Create an account to join the conversation.

    Last edit: by lagraf.

    Zugriff auf SmartCard Reader mit PCSC 19 Apr 2022 12:06 #22212

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hallo Franz,
    local nVal as int
    nVal := 0
    if int32.TryParse( cVal, ref nVal
    ....
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 19 Apr 2022 16:12 #22219

    • Chris
    • Chris's Avatar


  • Posts: 3990
  • Guys,

    Just a very minor correction, X# supports also the OUT keyword (which has slightly different semantics to REF), so this can be translated 1:1 from c# to X# as:

    Int32.TryParse( relin, OUT num)
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 08:36 #22223

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Hallo Wolfgang, Chris!
    Was ist der Unterschied zwischen REF und OUT?
    Bei folgendem Beispiel bekomme ich eine Warning "warning XS0219: The variable 'readerNames' is assigned but its value is never used", obwohl ich readerNames als Leerarray initialisiere (Dialect Core):
    LOCAL readerNames AS STRING[]
    readerNames := STRING[]{ 0 }
    sc := reader:Status(OUT readerNames, OUT state, OUT proto, OUT atr)
    Wenn ich statt OUT readerNames aber REF readerNames verwende, dann compiliert der Code ohne Warning.

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 08:41 #22224

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hallo Franz,
    den Unterschied zwischen REF und OUT lasse ich Chris erklären.
    Die Fehlermeldung ist klar:
    warning XS0219: The variable 'readerNames' is assigned but its value is never used
    Dieser Variablen wurde ein Wert zugewiesen, aber dieser Wert wurde anschließend nie verwendet.
    Augenscheinlich trickst REF diese Kontrolle aus, dann eigentlich sollte sie auch hier kommen.
    Ich nehme aber an, dass es hierfür einen Grund gibt.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 08:46 #22226

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Hallo Wolfgang,
    ist "readerNames := STRING[]{ 0 }" nicht eine Anlage eines Leerarrays und somit bereits eine Zuweisung?

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 08:52 #22227

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hallo Franz,
    es geht hier nicht um die Zuweisung (das wäre eine andere Fehlermeldung), sondern um die Verwendung der Variablen.
    function Hallo() as void
    local c1 as string
    local c2 as string
    
    c2 := "Franz"
    
    return
    gibt zwei verschiedene Fehlermeldungen:
    warning XS0168: The variable 'c1' is declared but never used
    warning XS0219: The variable 'c2' is assigned but its value is never used

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 10:55 #22234

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Hallo Wolfgang,
    ok verstehe, dann prüft der Compiler nicht nur, ob die Var vor einer Verwendung initialisiert wurde, sondern ob sie auch weiterverwendet wird, damit kein Code liegenbleibt der nichts bewirkt.

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 13:38 #22241

    • Chris
    • Chris's Avatar


  • Posts: 3990
  • Guys,

    In short, the difference between REF and OUT is that :
    - with REF, you are supposed to pass an initialized value to the function/method and expect that possibly this function might modify its value when it returns
    - with OUT, you are not supposed to pass a value to the function (the var is supposed to be uninitialized), but it's mandatory for the function to assign a value to this var, which you will receive back in your calling code.

    Normally when a function expects an OUT param, then it is mandatory to pass it with OUT as well, and the same is true for REF, but for compatibility with vulcan, X# allows to mix those keywords, which is not really good, probably we should make the compiler stricter, at least when using a compiler option.

    For the same reason X# is more lenient in the REF/OUT rules than c#, but for a complete list of "official" differences between OUT and REF in .Net, you can have a look here:

    www.c-sharpcorner.com/UploadFile/ff2f08/...keywords-in-C-Sharp/
    www.geeksforgeeks.org/difference-between...keywords-in-c-sharp/

    this also explains the "IN" modifier keyword when declaring parameters:

    www.pluralsight.com/guides/csharp-in-out-ref-parameters

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Last edit: by Chris.

    Zugriff auf SmartCard Reader mit PCSC 20 Apr 2022 15:30 #22242

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hi Chris,
    thank you very much!
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 21 Apr 2022 08:47 #22249

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Chris, thank you for your explanation!
    Franz

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 22 Apr 2022 17:15 #22264

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Ich habe inzwischen ein paar Methoden zur Ansprache eines SmartCardReaders aus meiner DLL mittels ILSpy extrahiert und in ein Projekt integriert. Dabei gehe ich wie folgt vor:
    • Ich übertrage die Methode in ein C# Project und bringe sie dort zum Laufen
    • Danach übersetze ich sie nach X#
    Nun bin ich aber auf ein Konstrukt gestoßen, das mir gar nichts sagt und das XIDE auch nicht compiliert:
    using System;
    using System.Linq;
    
    public class Program
    {
    	static void Main()
    	{
    		System.Console.WriteLine("Hello XIDE!");
    		System.Console.WriteLine("00A4040007A0000001184543".ToByteArray());
    	}
    
    	public static byte[] ToByteArray(this string hex)
    	{
    		return (from x in Enumerable.Range(0, hex.Length)
    			where x % 2 == 0
    			select Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
    	}
    }
    Wie am Bild1.jpg zu sehen ist, kann XIDE ToByteArray nicht mal korrekt darstellen.
    Was ist ausserdem mit Extension gemeint?

    Attachment not found

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 22 Apr 2022 17:19 #22266

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Attachments:

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 22 Apr 2022 17:51 #22267

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Ich habs noch ein wenig umgebaut:
    using System;
    using System.Linq;
    using Project1.Extensions;
    
    namespace Project1
    {
    	public class Program
    	{
    		static void Main()
    		{
    			System.Console.WriteLine("Hello XIDE!");
    			System.Console.WriteLine("00A4040007A0000001184543".ToByteArray());
    		}
    	}
    }
    
    namespace Project1.Extensions
    {
    	public static class Extensions
    	{
    		public static byte[] ToByteArray(this string hex)
    		{
    			return (from x in System.Linq.Enumerable.Range(0, hex.Length)
    				where x % 2 == 0
    				select Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
    		}
    	}
    }
    Jetzt bekomme ich den Fehler:
    error CS1069: The type name 'Enumerable' could not be found in the namespace 'System.Linq'. This type has been forwarded to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly. 26,34 Program.cs Project1.Extensions.Project1.Extensions:ToByteArray
    Laut ILSpy ist Enumerable.Range in System.Linq vorhanden.
    Referenziert habe ich System und System.Linq.

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 22 Apr 2022 17:56 #22268

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hallo Franz,
    das ist ein Linq-Ausdruck, und lässt sich problemlos in X# umsetzen.
    Warum sich das mit C# auch nicht kompilieren lässt, hat wahrscheinlich mit einem veralteten C#-Compiler auf Deinem System zu tun.
    Hast Du den C#-Code per ILSpy ausgelesen oder hattest Du den als Quellcode gefunden?
    Wenn das zweite der Fall ist, dann wird der IL-Code nicht richtig nach C# umgesetzt.
    Mit LinQ bin ich nicht wirklich fit, da müsste man sich einen Moment damit beschäftigen, aber der Code lässt sich auch ohne Linq schreiben.
    Für das Verwenden von Enumerable brauchst Du nicht nur das "using System.Linq", sondern auch in den Abhängigkeiten die System.Linq.dll:
    docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 22 Apr 2022 18:31 #22269

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hallo Franz,
    in X# funktioniert dieser Code:
    function ToByteArray( cHex as string ) as byte[]
    local aReturn as byte[]
    
    aReturn := ( from x in System.Linq.Enumerable.Range( 0, cHex:Length ) ;
      where x % 2 == 0                                           ;
      select Convert.ToByte( cHex:Substring( x, 2 ), 16 ) ):ToArray()
    
    return aReturn
    Du brauchst Referenzen auf System.Linq und System.Core.
    Logischerweise kannst Du das Ganze auch in eine statische Methode, eine Extension-Methode oder eine normale Methode packen, es ändert sich dann nur der Aufruf.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 22 Apr 2022 21:10 #22271

    • Chris
    • Chris's Avatar


  • Posts: 3990
  • Guys,

    It's the same also in c#, as the error message says, you need to add a reference to the library System.Core (in the app properties window)

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 23 Apr 2022 08:44 #22275

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Hallo Wolfgang,
    kann schon sein dass mein Compiler veraltet ist, ich hab noch W7 drauf. Kann man die Version feststellen oder ihn irgendwie updaten ohne das ganze OS umstellen zu müssen?
    Den C# Code habe ich per ILSpy ausgelesen, mit dem X# Plugin sieht er übrigens so aus, muß aber noch probieren was der Compiler dann dazu sagt. Ist aber schon irgendwie komisch mit den Strichpunkten:
    public static method ToByteArray(self hex as string ) as Byte[]
    	return (from x in Enumerable.Range(0, hex:Length);
    	where x % 2 == 0;
    	select Convert.ToByte(hex:Substring(x, 2), 16)):ToArray()
    Ich hab die TestApp jetzt so erweitert und das funktioniert:
    using System;
    using System.Linq;
    using System.Text;
    using Project1.Extensions;
    
    namespace Project1
    {
    	public class Program
    	{
    		static void Main()
    		{
    			System.Console.WriteLine("Hello XIDE!");
    			System.Console.WriteLine("00A4040007A0000001184543".ToByteArray().ToHexString());
    		}
    	}	// class
    }	// namespace
    
    namespace Project1.Extensions
    {
    	public static class Extensions
    	{
    		public static byte[] ToByteArray(this string hex)
    		{
    			return (from x in System.Linq.Enumerable.Range(0, hex.Length)
    				where x % 2 == 0
    				select Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
    		}
    		public static string ToHexString(this byte[] ba)
    		{
    			if (ba == null)
    			{
    				return "";
    			}
    			StringBuilder stringBuilder = new StringBuilder(ba.Length * 2);
    			foreach (byte b in ba)
    			{
    				stringBuilder.AppendFormat("{0:x2}", b);
    			}
    			return stringBuilder.ToString();
    		}
    	}	// class
    }	// namespace
    Interessant ist aber trotzdem die Anzeige in der XIDE, dürfte sich um einen Fehler handeln, ist beim X# Code identisch:
    Attachments:

    Please Log in or Create an account to join the conversation.

    Last edit: by lagraf.

    Zugriff auf SmartCard Reader mit PCSC 23 Apr 2022 08:48 #22276

    • lagraf
    • lagraf's Avatar
    • Topic Author


  • Posts: 338
  • Hi Chris and Wolfgang,
    with System.Core it runs! How can I find out which usings and references I need if such errors occure?

    Please Log in or Create an account to join the conversation.

    Zugriff auf SmartCard Reader mit PCSC 23 Apr 2022 09:20 #22277

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3369
  • Hi Franz,

    with System.Core it runs! How can I find out which usings and references I need if such errors occure?


    it may sound stupid: read and understand the error message of the compiler. It says it:
    The type name 'Enumerable' could not be found in the namespace 'System.Linq'. This type has been forwarded to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Last edit: by wriedmann.
    Moderators: wriedmann