如何在C ++中监听智能卡插入和删除事件?

时间:2011-11-23 07:55:29

标签: c++ events smartcard

我想听一下智能购物车的插入和删除事件......该应用程序适用于Windows,智能卡使用x.509证书。我使用的读卡器是标准读卡器,插入大多数新笔记本电脑中,您也可以购买它们供USB使用..

我发现的一件事是: cryptware.it/apidoc/scapi/index.html 但它不可能是唯一的方式,我只想知道我的选择......

有谁知道最好的方法是什么?

提前致谢!

2 个答案:

答案 0 :(得分:5)

Windows API具有以下功能:

LONG WINAPI SCardGetStatusChange(
  __in     SCARDCONTEXT hContext,
  __in     DWORD dwTimeout,
  __inout  LPSCARD_READERSTATE rgReaderStates,
  __in     DWORD cReaders
);

然后,您可以检查rgReaderStates是否包含SCARD_STATE_EMPTYSCARD_STATE_PRESENT。请在此处阅读详细信息:MSDN description

严格来说,它不是事件驱动的,但它会阻止执行直到发生更改。因此,通过创建一个在循环中调用它的单独线程,您可以自己轻松生成事件。

答案 1 :(得分:0)

一个例子。

这应该包含在线程函数中,该函数以一定的时间间隔(1秒)运行此函数。线程函数应该使用它并向应用程序发送驱动程序已更改的通知。

警告:UGLY CODE。请以此为例,并在您看到合适的情况下改进它。

BOOL CheckDirProperties(const CString& path, BOOL& bReadOnly, BOOL& bRemovable)
{
    DWORD   FileAttributes;
    DWORD   DriveAttributes;
    UINT    uDriveType;

    if( path.GetLength() < 2 ||path.GetAt( 1 ) != ':' )
    {
        // invalid path, abort
        return FALSE;
    }
//Ugly path handling

    CString szFormattedDrivePath("C:\\"); // string of length 3 where drive letter will be replaced
// Replace the drive letter with the drive letter from the path
    szFormattedDrivePath.SetAt( 0, path.GetAt( 0 ) ); 

    DriveAttributes = GetFileAttributes( szFormattedDrivePath );
    FileAttributes  = GetFileAttributes( path);
    uDriveType      = GetDriveType( szFormattedDrivePath );

    if( !(FileAttributes  & FILE_ATTRIBUTE_DIRECTORY) || 
        !(DriveAttributes & FILE_ATTRIBUTE_DIRECTORY) )
    {   // Not a directory
        return FALSE;
    }

    if( (FileAttributes  & FILE_ATTRIBUTE_ARCHIVE) || 
        (DriveAttributes & FILE_ATTRIBUTE_ARCHIVE) ||
        (FileAttributes  & FILE_ATTRIBUTE_ENCRYPTED) || 
        (DriveAttributes & FILE_ATTRIBUTE_ENCRYPTED) ||
        (FileAttributes  & FILE_ATTRIBUTE_OFFLINE) || 
        (DriveAttributes & FILE_ATTRIBUTE_OFFLINE) ||
        (FileAttributes  & FILE_ATTRIBUTE_OFFLINE) || 
        (DriveAttributes & FILE_ATTRIBUTE_OFFLINE) )
    {   // Not a directory
        TRACE("The directory %s on drive %s has unexpected file attributes. Problems may occur.\n",path, szFormattedDrivePath );
    }   

    // To set m_bReadOnly to true, we need to know that the entire subtree is readonly.
    // Even if the drive or the directory has the FILE_ATTRIBUTE_READONLY set, the content may not be read-only.
    // Therefore the default value of bReadOnly must be FALSE.
    bReadOnly    = FALSE;

    switch( uDriveType )
    {
        case DRIVE_FIXED: 
        case DRIVE_REMOTE:
            bRemovable = FALSE;
            break;
        case DRIVE_CDROM:
            bRemovable = TRUE;
            bReadOnly    = TRUE; // We know that a CD-ROM drive is always read-only
            break;
        case DRIVE_REMOVABLE: 
        case DRIVE_RAMDISK:
            bRemovable = TRUE;
            break;
        case DRIVE_NO_ROOT_DIR: // fall through
        case DRIVE_UNKNOWN:     // fall through
        default:
            bRemovable = TRUE; // assume it is removable if we don't know what value to set
            break;
    }

    return TRUE;
}