Yubico Forum

...visit our web-store at store.yubico.com
It is currently Tue Jan 30, 2018 3:21 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Sep 03, 2012 1:32 am 
Offline

Joined: Mon Sep 03, 2012 1:22 am
Posts: 3
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


Top
 Profile  
Reply with quote  

Share On:

Share on Facebook FacebookShare on Twitter TwitterShare on Tumblr TumblrShare on Google+ Google+

PostPosted: Mon Sep 03, 2012 8:24 am 
Offline
Site Admin
Site Admin

Joined: Thu Apr 19, 2012 1:45 pm
Posts: 148
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 03, 2012 6:06 pm 
Offline

Joined: Mon Sep 03, 2012 1:22 am
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 03, 2012 6:35 pm 
Offline

Joined: Mon Sep 03, 2012 1:22 am
Posts: 3
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group