Yubico Forum
https://forum.yubico.com/

COM API deviceInserted and deviceRemoved events not firing
https://forum.yubico.com/viewtopic.php?f=3&t=857
Page 1 of 1

Author:  tekuru [ Mon Sep 03, 2012 1:32 am ]
Post subject:  COM API deviceInserted and deviceRemoved events not firing

Hello,

I seem to be having an issue with the COM API using C# .NET, specifically with the deviceInserted and deviceRemoved events. I am aware that I need to set the notification mode to asynchronous, which I do, however my code is never called when I insert and remove my YubiKey. The Test MFC C++ app does however work properly. Below is my code, stripped down to just the essentials. This is a WinForms project:

Code:
    public partial class Form1 : Form
    {
        public YubiClientAPILib.YubiClient api;

        public Form1()
        {
            InitializeComponent();

            api = new YubiClientAPILib.YubiClient();
            api.enableNotifications = YubiClientAPILib.ycNOTIFICATION_MODE.ycNOTIFICATION_ON;

            // removal/insertion events
            api.deviceInserted += this.api_deviceInserted;
            api.deviceRemoved += this.api_deviceRemoved;
        }

        private void api_deviceRemoved()
        {
            MessageBox.Show("Device Removed!! Boo...");
        }

        private void api_deviceInserted()
        {
            MessageBox.Show("Device Inserted!! Wahoo");
        }
   }


It is Yubico Client API 1.0 Type Library, version 1.0.0.0, x64 with Visual Studio 2010. I just downloaded the API installer today, 02 Sep 2012.

See anything obvious? Unfortunately the provided C# example does not include this functionality so I couldn't use that as a reference.

Thank you for any help,
David

Author:  Klas [ Mon Sep 03, 2012 8:24 am ]
Post subject:  Re: COM API deviceInserted and deviceRemoved events not firi

Hello,

We've used that functionality in the settingspanel for the YubiKey Logon for Windows, you can have a look at it at: https://github.com/Yubico/yubico-window ... gs.cs#L129

/klas

Author:  tekuru [ Mon Sep 03, 2012 6:06 pm ]
Post subject:  Re: COM API deviceInserted and deviceRemoved events not firi

Thank you for that link. I noticed that the event handlers were actually:

Code:
api.deviceInserted += new _IYubiClientEvents_deviceInsertedEventHandler(api_deviceInserted);
api.deviceRemoved += new _IYubiClientEvents_deviceRemovedEventHandler(api_deviceRemoved);

However they still didn't fire.

This got me experimenting a bit, and I discovered that the COM api only enables notifications if you enable the notifications via a button or checkbox after the app has loaded. What I mean is, in my initial code example I was setting the notification mode inside the forms constructor, however I was never getting notifications.

I then noticed that in both the test MFC app and in your link for the logon app, you were enabling the notifications via a check box. So I did the same, in my code I removed the enableNotifications statement from the constructor and put it into the event handler for the checkBox's checkChanged event, and suddenly the events were firing.

There was still one problem though, the event handlers are running on a different thread than the UI thread, and if I paid attention to the debug log I saw an illegal operation exception as I was trying to set my result label from a different thread. To get around this I used:

Code:
        private void api_deviceRemoved()
        {
            this.Invoke((MethodInvoker)delegate
            {
                result.Text = "Device Removed!! Boo...";
            });
        }

Hope this helps someone.

David

Author:  tekuru [ Mon Sep 03, 2012 6:35 pm ]
Post subject:  Re: COM API deviceInserted and deviceRemoved events not firi

By the way I got around the notifications-only-via-checkbox thing by adding api.enableNotifications = ycNOTIFICATION_MODE.ycNOTIFICATION_ON; into the forms _Load() event handler.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/