rana-mp
Goto Top

C-Sharp - Schreiben in die Zwischenablage

Hallo zusammen,

ich habe ein Problem, was fuer mich als relativ unerfahrenen Programmierer im Momoment unloesbar erscheint.

Ein wenig Hintergrund: Ich habe einen USB Smardcard Leser, der intern mit einem FTDI USB to Serial Chip angebunden ist, unter Windows also nur eine schnoede COM Schnittstelle bereitstellt.
Das Ding ist schon ein paar Jaehrchen aelter, und eine spezialle Software dafuer gibt es nicht. Kurze Tests mit Putty haben ergeben das er die Katennummer im Klartext ueber die Schnittstelle jagt, da dachte ich mir, schreib ich mir doch fix mein eigenes Programm. Dies in C#, da ich da noch am meisten Erfahrung habe. Das Programm soll die Kartennummer auslesen und anschliessend diese in die Zwischenablage kopieren.

Hier mein kompletter Code:
using System;
using System.Text;
using System.IO.Ports;
using System.Windows;

namespace ConsoleApplication1
{
    class Program
    {
        [STAThread]
        static void Main(string args)
        {
            SerialPort mySerialPort = new SerialPort();
        
            Console.WriteLine("Please enter the Number of the COM Port of the Cardreader as seen in the Devicemanager.\n");  
            
            //COM ID has to be user supplied
            mySerialPort.PortName = "COM"+Console.ReadLine();  

            //Serialport properties. These are customized for the Cardreader
            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.XOnXOff;

            //Eventhandler is fired twice everytime a card is read
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            try
            {
                mySerialPort.Open();
            }
            catch
            {
                Console.WriteLine("Cannot open COM Port. Please check if the Port is correct and not already in use");  
                Console.WriteLine("Press any key to exit...");  
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Program Ready");  
            Console.WriteLine("Read Card or press any key to exit");  
            Console.WriteLine();
            Console.ReadKey();
            mySerialPort.Close();
        }

        [STAThread]
        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            Char Buffer = new Char[20];
            
            //Sleep is required to give the device enough time to buffer the complete Card ID
            System.Threading.Thread.Sleep(500);
            sp.Read(Buffer, 0, 20);
            

            String cardID = "";  

            //If the card is read multiple times in too short a time, the event fires again before the buffer is completely read. If that happens parts of the buffer are empty
            //This is to check if the buffer only has digits in it (which makes it a valid card ID)
            foreach (Char Ziffer in Buffer)
            {
                if (Ziffer == '0' || Ziffer == '1' || Ziffer == '2' || Ziffer == '3' || Ziffer == '4' || Ziffer == '5' || Ziffer == '6' || Ziffer == '7' || Ziffer == '8' || Ziffer == '9')  
                {
                    cardID = cardID + Ziffer.ToString();
                }
            }
            //when there are 20 digits in the ID, it is valid
            if (cardID.Length == 20)
            {
                Console.WriteLine(cardID);
                System.Windows.Clipboard.SetText(cardID);
            }
            //If there are some digits in the ID, but not 20, that is the result of a too fast re-read
            else if (cardID.Length != 0)
            {
                Console.WriteLine("Read Error {0}", cardID.Length);  
            }
            //on an OK read, on the second firing of the event, the buffer will be empty
            else if (cardID.Length == 0)
            {
                Console.WriteLine("Read OK");  
            }
            //Console.WriteLine("");  
            cardID = "";  
        }
    }
}

Grundlegend funktioniert dieser Code so wie er sollte und kann auch Fehler beim Lesen kompensieren. Aber beim schreiben in die Zwischenablage bekomme ich folgende Fehlermeldung:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.

Dazu findet man haufenweise im Netz, aber die fast immer angegebene Standardloesung [STAThread] an die Main anzufuegen funktioniert hier nicht. Dabei ist es egal ob ich die Clipboard Klasse aus System.Windows oder aus System.Windows.Forms nehme, beides gibt den gleichen Fehler.

Gruss,

rana-mp

Content-Key: 236786

Url: https://administrator.de/contentid/236786

Ausgedruckt am: 19.03.2024 um 09:03 Uhr

Mitglied: colinardo
Lösung colinardo 29.04.2014, aktualisiert am 30.04.2014 um 10:28:28 Uhr
Goto Top
Hallo rana-mp,
das ist etwas schwierig zu erklären, das liegt aber daran wie [Single Thread Appartments] funkionieren. Das steht hier erklärt:http://msdn.microsoft.com/en-us/library/windows/desktop/ms680112%28v=vs ...

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.
http://stackoverflow.com/questions/17175783/prevent-event-from-spawning ...

Das hier sollte aber alternativ dazu problemlos funktionieren: (dann musst du auch nicht so eine große Referenz wie "Windows.Forms" zu deiner Console-App hinzufügen)
back-to-topNutze das Win32-API dazu (Beispiel):
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll")]  
        internal static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("user32.dll")]  
        internal static extern bool CloseClipboard();

        [DllImport("user32.dll")]  
        internal static extern bool SetClipboardData(uint uFormat, IntPtr data);
        
        [STAThread]
        static void Main(string args)
        {
            Console.WriteLine("Bitte einen Text eingeben und mit Enter bestätigen:");  
            String myText = Console.ReadLine();

            //Text ans Clipboard senden
            OpenClipboard(IntPtr.Zero);
            var ptr = Marshal.StringToHGlobalUni(myText);
            SetClipboardData(13, ptr);
            CloseClipboard();
            Marshal.FreeHGlobal(ptr);
            //------------------------

            Console.WriteLine("Dein Text wurde in die Zwischenablage kopiert! Drücke eine Taste zum beenden");  
            Console.ReadLine();   
        }
    }
}
Mitglied: rana-mp
rana-mp 30.04.2014 um 10:28:21 Uhr
Goto Top
Hallo colinardo,

auch wenn ich gesetehen muss das ich den Code (noch) nicht 100%tig verstehe, so funktioniert es doch einwandfrei face-smile

Vielen dank.
Mitglied: papasmurf
papasmurf 11.07.2014 um 16:40:43 Uhr
Goto Top
The copy-to-clipboard code in question is slightly problematic. StringToHGlobalUni doesn't do what it is natural to think it does. It actually uses LocalAlloc with LMEM_FIXED, but SetClipboardData requires memory allocated with GlobalAlloc and GMEM_MOVABLE. The code can thus cause crashes.

Here's a link to my response on the subject on StackOverflow, which you can follow to get a more robust solution on GitHub if you'd like:

http://stackoverflow.com/a/24698804/2682101