⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcsc_drv.cpp

📁 这是通用pcsc读卡器的驱动源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        else
            status = STATUS_INSUFFICIENT_RESOURCES;     	
    }
    else
    {
        KdPrint(("\t%sCreateDevice IOCREATEDEVICE FAILED status %#X\n",HEAD, (ULONG)status));
        if (status == STATUS_OBJECT_NAME_EXISTS)
        {
            KdPrint(("\t%sCreateDevice IOCREATEDEVICE %#X\n", HEAD, STATUS_OBJECT_NAME_EXISTS));
        }
        if (status == STATUS_OBJECT_NAME_COLLISION)
        {
            KdPrint(("\t%sCreateDevice IOCREATEDEVICE %#X\n", HEAD, STATUS_OBJECT_NAME_COLLISION));
        }
        if (status == STATUS_INSUFFICIENT_RESOURCES)
        {
            KdPrint(("\t%sCreateDevice IOCREATEDEVICE %#X\n", HEAD, STATUS_INSUFFICIENT_RESOURCES));
        }
        if (status == STATUS_OBJECT_NAME_INVALID)
        {
            KdPrint(("\t%sCreateDevice IOCREATEDEVICE %#X\n", HEAD, STATUS_OBJECT_NAME_INVALID));
        }
    }
    KdPrint(("\t%sCreateDevice status %#X\n", HEAD, status));
    return status;
}
//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdCreate</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="pIrp">                     				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdCreate(IN PDEVICE_OBJECT DeviceObject, IN PIRP pIrp)
{
    NTSTATUS status = STATUS_SUCCESS;
    KdPrint(("%sVdCreate\n",HEAD));
    ReportEvent(LOG_LEVEL_DEBUG,MSG_OPENING_HANDLE,ERRORLOG_OPENING_HANDLE,
        (PVOID)DeviceObject,pIrp,NULL, 0,NULL, 0 );
    pIrp->IoStatus.Status = status;
    pIrp->IoStatus.Information = 0;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdClose</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="pIrp">                     				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP pIrp)
{
    KdPrint(("%sVdClose\n",HEAD));
    ReportEvent(LOG_LEVEL_DEBUG,MSG_CLOSING_HANDLE,ERRORLOG_CLOSING_HANDLE,
        (PVOID)DeviceObject,pIrp,NULL, 0,NULL, 0 );
    pIrp->IoStatus.Status = STATUS_SUCCESS;
    pIrp->IoStatus.Information = 0;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
}


//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdDeviceControl</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="pIrp">                     				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP pIrp)
{


    PDEVICE_EXTENSION      pDeviceExtension     = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    PSMARTCARD_EXTENSION   pSmartcardExtension  =&pDeviceExtension->SmartcardExtension;
    NTSTATUS               status               = STATUS_SUCCESS;
    PIO_STACK_LOCATION     pIrpStack            = NULL;
    KdPrint(("%sVdDeviceControl\n",HEAD));

    pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
    if ((status = SmartcardAcquireRemoveLock(pSmartcardExtension)))
    {
        pSmartcardExtension->IoRequest.Information = 0;
        // We should immediately return the control back
    }
    else
    {
        
        KdPrint(("%ssVdDeviceControl:: InputBufferLength ::%d\n",HEAD,
            pIrpStack->Parameters.DeviceIoControl.InputBufferLength));
         KdPrint(("%ssVdDeviceControl:: OutputBufferLength ::%d\n",HEAD,
        pIrpStack->Parameters.DeviceIoControl.OutputBufferLength));
        if(pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_EJECT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_EJECT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_POWER)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_POWER\n",HEAD)); 
            KdPrint(("\t%spIrp %X\n",HEAD,pIrp));
            if ((pIrp != NULL) && (pIrp->AssociatedIrp.SystemBuffer != NULL))
                KdPrint(("\t%sPOWER CODE - %0x\n",HEAD, *(ULONG*)pIrp->AssociatedIrp.SystemBuffer));
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_ATTRIBUTE)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_ATTRIBUTE\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_CONFISCATE)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_CONFISCATE\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_TRANSMIT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_TRANSMIT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_SWALLOW)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_SWALLOW\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_IS_PRESENT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_IS_PRESENT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_IS_ABSENT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_IS_ABSENT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_SET_PROTOCOL)
        { 
            KdPrint(("\t%sIOCTL_SMARTCARD_SET_PROTOCOL\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_STATE)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_STATE\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_LAST_ERROR)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_LAST_ERROR\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_PERF_CNTR)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_PERF_CNTR\n",HEAD)); 
        }
        status = SmartcardDeviceControl(pSmartcardExtension, pIrp);
        SmartcardReleaseRemoveLock(pSmartcardExtension);
    }
    KdPrint(("%sVdDeviceControl END, status : %#X\n",HEAD,status));
    return status;
}
//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdDriverUnload</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DriverObject">             				       </param>
//<returns> </returns>
// ======================================================================
void VdDriverUnload(IN PDRIVER_OBJECT DriverObject)
{
    PDEVICE_OBJECT          pDeviceObject       = DriverObject->DeviceObject;
    PDEVICE_EXTENSION       pDeviceExtension    = (PDEVICE_EXTENSION) pDeviceObject->DeviceExtension;
    KIRQL                   CancelIrql;
    PSMARTCARD_EXTENSION    pSmartcardExtension = NULL;
    PIRP                    pNotificationIrp    = NULL;
    KdPrint(("%sVdDriverUnload\n",HEAD));
#ifdef USE_SYSTEM_THREAD   
    IoFreeWorkItem(pDeviceExtension->_PIO_WORKITEM);
    ExFreePool(pDeviceExtension->pMemory_Buf);
    pDeviceExtension->pMemory_Buf = NULL;
#endif   
#ifdef USE_WORK_THREAD   
    // let's kill the auxilary WORK_THREAD
    if (DEVICE_READER_EXTENSION(pevWORK_THREAD_AWAKE) && DEVICE_READER_EXTENSION(pThreadObj))
        KillThread(pDeviceExtension);   
#endif USE_WORK_THREAD
    //	report to the lib that the device will be unloaded
    if (pDeviceExtension->SmartcardExtension.OsData != NULL)
    {
        pSmartcardExtension = &pDeviceExtension->SmartcardExtension;
        IoAcquireCancelSpinLock(&CancelIrql);        
        if (pSmartcardExtension->OsData->NotificationIrp != NULL)
        {
            pNotificationIrp = (PIRP)InterlockedExchangePointer(
                &(pSmartcardExtension->OsData->NotificationIrp),NULL);
            IoSetCancelRoutine(pNotificationIrp, NULL);
            IoReleaseCancelSpinLock(CancelIrql);
            //	finish the request
            pNotificationIrp->IoStatus.Status = STATUS_SUCCESS;
            pNotificationIrp->IoStatus.Information = 0;
            IoCompleteRequest(pNotificationIrp, IO_NO_INCREMENT);
        }
        else 
        {
            IoReleaseCancelSpinLock(CancelIrql);
        }
        // Wait until we can safely unload the device
        // SmartcardReleaseRemoveLockAndWait(SmartcardExtension);
        SmartcardExit(&pDeviceExtension->SmartcardExtension);
    }
#ifdef REGISTER_INTERFACE
    //	delete the symbolic link
    if (pDeviceExtension->DeviceName.Buffer != NULL)
    {
        RtlFreeUnicodeString(&pDeviceExtension->DeviceName);
        pDeviceExtension->DeviceName.Buffer = NULL;
    }
#else
    if (STATUS_SUCCESS != IoDeleteSymbolicLink (&pDeviceExtension->LinkName))
    {
       KdPrint(("%ssVdDriverUnload symbol link was not deleted\n"));
    }
    VASSERT("ssVdDriverUnload",pDeviceExtension->LinkName.Buffer != NULL);
    if (pDeviceExtension->LinkName.Buffer != NULL)
    {
       KdPrint(("%ssVdDriverUnload buffer of symbol link was not deleted\n"));
       RtlFreeUnicodeString (&pDeviceExtension->LinkName);
       SET_NULL(pDeviceExtension->LinkName.Buffer)
    }
#endif
    if (pDeviceExtension->SmartcardExtension.ReaderExtension != NULL)
    {
        ExFreePool(pDeviceExtension->SmartcardExtension.ReaderExtension);
        pDeviceExtension->SmartcardExtension.ReaderExtension = NULL;
    }
    //	delete the device object
    IoDeleteDevice(pDeviceObject);
    KdPrint(("%sVdUnloadDevice: Exit\n",HEAD));
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdPower</newpara>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -