0

I'm using PCSC library for SmartCard Readers events detection and trying to use it in Windows service. Readers search function:

 private void CheckPresentReaders()
    {
        using (var context = new SCardContext())
        {
            context.Establish(SCardScope.System);
            PresentCardReaders = context.GetReaders();
        }
    }

SmartCard removed function:

private void SCardRemoved(object sender, CardStatusEventArgs e)
    {
        WriteToLog("Locking machine. SmartCard was removed.");
       // LockWorkStation();
    }

Monitor creation:

 CheckPresentReaders();
        if (PresentCardReaders.Length != 0)
        {
            SCardMonitor monitor = new SCardMonitor(ContextFactory.Instance, SCardScope.System);
            monitor.CardRemoved += new CardRemovedEvent(SCardRemoved);
            foreach (string reader in PresentCardReaders)
                monitor.Start(reader);
        }

WriteToLog function is a simple Log Entry creation function.

When it is compiled - service starting and then stopping immediately. I have two suspects - not delegated WriteToLog and/or SCardRemoved, which requires two parameters -

(object sender, CardStatusEventArgs e) Those are required by library.

Can this be a problem? Any other suggestions? Thanks.

Ivan Temchenko
  • 814
  • 1
  • 9
  • 12
  • Have you checked http://stackoverflow.com/questions/26233593/pcsc-invalidcontextexception-when-running-as-a-windows-service and https://blogs.msdn.microsoft.com/alejacma/2011/05/19/scardestablishcontext-fails-with-scard_e_no_service-error/ already? What kind of exception does it throw? If it crashes, the eventlog should contain the stacktrace+exception message. – Daniel Müller May 11 '17 at 17:23

1 Answers1

0

I've implemented it properly into Topshelf Service and it does work.

https://github.com/35359595/SmartCardMonitorService

Ivan Temchenko
  • 814
  • 1
  • 9
  • 12