📄 afunc.cpp
字号:
{
// Wait indefinitely for an IRP to appear in
// the work queue or for the RemoveDevice
// routine to stop the thread.
KdPrint(("%sWorkerThreadMain, Enter into wait state\n",HEAD));
KeWaitForSingleObject(READER_EXTENSION(pevWORK_THREAD_AWAKE), Executive, KernelMode,
FALSE, NULL);
// See if thread was awakened because device is being removed
if (READER_EXTENSION(bThreadShouldStop))
{
status = PsTerminateSystemThread(STATUS_SUCCESS);
KdPrint(("%sWorkerThreadMain, will be killed\n",HEAD));
}
else
{
KdPrint(("%sWorkerThreadMain, virt. scard reader will change its state\n",HEAD));
//------------------------------------------------------------
//reader of the virtual smartcard reader will change its state
//------------------------------------------------------------
VdCompleteCardTracking(pSmartcardExtension, READER_EXTENSION(uStateOfCard));
READER_EXTENSION(uStateOfCard) = !READER_EXTENSION(uStateOfCard);
}
} // end of while-loop
}
//<summary>
// ======================================================================
//<newpara> Author :BD Date : 04-12-2001 </newpara>
// ======================================================================
//<newpara> Function : KillThread</newpara>
//<newpara> Comments : </newpara>
//</summary>
//Parameters :
// <param name="pDE"> </param>
//<returns> </returns>
// ======================================================================
void KillThread(IN PDEVICE_EXTENSION pDeviceExtension)
{
PSMARTCARD_EXTENSION pSmartcardExtension = &pDeviceExtension->SmartcardExtension;
if (READER_EXTENSION(pevWORK_THREAD_AWAKE) && READER_EXTENSION(pThreadObj))
{
KdPrint(("%sKillThread\n",HEAD));
// Set the Stop flag
READER_EXTENSION(bThreadShouldStop) = TRUE;
// Make sure the thread wakes up
KeSetEvent(READER_EXTENSION(pevWORK_THREAD_AWAKE), 0, false);
// Wait for the thread to terminate
KeWaitForSingleObject(&READER_EXTENSION(pThreadObj), Executive, KernelMode, FALSE, NULL);
ObDereferenceObject(&READER_EXTENSION(pThreadObj));
READER_EXTENSION(pevWORK_THREAD_AWAKE) = 0;
READER_EXTENSION(pThreadObj) = 0;
READER_EXTENSION(bThreadShouldStop) = false;
if (ZwClose(READER_EXTENSION(hEvent)) == STATUS_SUCCESS)
READER_EXTENSION(hEvent) = NULL;
}
}
#endif
//<summary>
// ======================================================================
//<newpara> Author :BD Date : 04-12-2001 </newpara>
// ======================================================================
//<newpara> Function : VdCompleteCardTracking</newpara>
//<newpara> Comments : </newpara>
//</summary>
//Parameters :
// <param name="pSmartcardExtension"> </param>
// <param name="bCardState"> </param>
//<returns> </returns>
// ======================================================================
void VdCompleteCardTracking(IN PSMARTCARD_EXTENSION pSmartcardExtension, bool bCardState)
{
PIRP pNotificationIrp = NULL;
KIRQL irql, CurrentIrql;
KeAcquireSpinLock(&pSmartcardExtension->OsData->SpinLock, &irql);
#ifdef VERSION_READER_2
if (!bCardState)
{
if (!READER_EXTENSION(uReaderPowerState))
// Card is inserted
pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_PRESENT;
else
pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED;
KdPrint(("%sSmart card inserted\n",HEAD));
}
else
{
// Card is removed
pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_ABSENT;
KdPrint(("%sSmart card removed\n",HEAD));
}
#endif
if (pSmartcardExtension->OsData->NotificationIrp != NULL)
{
IoAcquireCancelSpinLock(&CurrentIrql);
pNotificationIrp = pSmartcardExtension->OsData->NotificationIrp;
IoSetCancelRoutine(pNotificationIrp, NULL);
// reset the Cancel routine for the IRP to NULL.
IoReleaseCancelSpinLock(CurrentIrql);
// pass the CancelIRQL specified in the IRP to release the cancel spin lock for the IRP
if (!pNotificationIrp->Cancel)
// finish the request
pNotificationIrp->IoStatus.Status = STATUS_SUCCESS;
else
pNotificationIrp->IoStatus.Status = STATUS_CANCELLED;
pNotificationIrp->IoStatus.Information = 0;
IoCompleteRequest(pNotificationIrp, IO_NO_INCREMENT);
pSmartcardExtension->OsData->NotificationIrp = NULL;
}
KeReleaseSpinLock(&pSmartcardExtension->OsData->SpinLock, irql);
}
//============================================================================================
#ifdef USE_EVENT_LOG
//<summary>
// ======================================================================
//<newpara> Author :BD Date : 04-12-2001 </newpara>
// ======================================================================
//<newpara> Function : GetStringSize</newpara>
//<newpara> Comments : </newpara>
//</summary>
//Parameters :
// <param name="String"> </param>
//<returns> </returns>
// ======================================================================
ULONG GetStringSize(IN PWSTR String)
{
UNICODE_STRING TempString;
// Use an RTL routine to get the length
RtlInitUnicodeString( &TempString, String );
// Size is actually two greater because
// of the UNICODE_NULL at the end.
return( TempString.Length + sizeof( WCHAR));
}
//<summary>
// ======================================================================
//<newpara> Author :BD Date : 04-12-2001 </newpara>
// ======================================================================
//<newpara> Function : ReportEvent</newpara>
//<newpara> Comments : </newpara>
//</summary>
//Parameters :
// <param name="ULONG MessageLevel"> </param>
// <param name="NTSTATUS ErrorCode"> </param>
// <param name="ULONG UniqueErrorValue"> </param>
// <param name="PVOID pIoObject"> </param>
// <param name="PIRP pIrp"> </param>
// <param name="ULONG DumpData[]"> </param>
// <param name="ULONG DumpDataCount"> </param>
// <param name="PWSTR Strings[]"> </param>
// <param name="ULONG StringCount"> </param>
//<returns> </returns>
// ======================================================================
bool ReportEvent(IN ULONG MessageLevel,IN NTSTATUS ErrorCode,IN ULONG UniqueErrorValue,
IN PVOID pIoObject,IN PIRP pIrp,IN ULONG DumpData[],IN ULONG DumpDataCount,
IN PWSTR Strings[],IN ULONG StringCount)
{
PIO_ERROR_LOG_PACKET pPacket = NULL;
PDEVICE_EXTENSION pDeviceExtension = NULL;
PIO_STACK_LOCATION pIrpStack = NULL;
PUCHAR pInsertionString = NULL;
UCHAR PacketSize;
UCHAR StringSize[ MAX_INSERTION_STRINGS ];
ULONG i;
bool bReturn = TRUE;
// If we're not logging, or the message is out of range, don't do anything.
if(( LogLevel != LOG_LEVEL_NONE ) && (MessageLevel >= LogLevel ))
{
PacketSize = sizeof( IO_ERROR_LOG_PACKET );
if( DumpDataCount > 0 )
PacketSize += (UCHAR) sizeof( ULONG ) *( DumpDataCount - 1 );
if( StringCount > 0 )
{
if( StringCount > MAX_INSERTION_STRINGS )
StringCount = MAX_INSERTION_STRINGS;
for( i=0; i<StringCount; i++ )
{
// Keep track of individual string sizes
StringSize[i] = (UCHAR)GetStringSize( Strings[i] );
PacketSize += StringSize[i];
}
}
if((pPacket = (PIO_ERROR_LOG_PACKET) IoAllocateErrorLogEntry(pIoObject,PacketSize )) != NULL )
{
pPacket->ErrorCode = ErrorCode;
pPacket->UniqueErrorValue = UniqueErrorValue;
if( pIrp != NULL )
{
pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
pDeviceExtension = (PDEVICE_EXTENSION) ((PDEVICE_OBJECT)pIoObject)->DeviceExtension;
pPacket->MajorFunctionCode = pIrpStack->MajorFunction;
pPacket->RetryCount = 1;
pPacket->FinalStatus = pIrp->IoStatus.Status;
pPacket->SequenceNumber = 0;
if( pIrpStack->MajorFunction == IRP_MJ_DEVICE_CONTROL ||
pIrpStack->MajorFunction == IRP_MJ_INTERNAL_DEVICE_CONTROL )
{
pPacket->IoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;
}
else pPacket->IoControlCode = 0;
}
else
{
pPacket->MajorFunctionCode = 0;
pPacket->RetryCount = 0;
pPacket->FinalStatus = 0;
pPacket->SequenceNumber = 0;
pPacket->IoControlCode = 0;
}
if( DumpDataCount > 0 )
{
pPacket->DumpDataSize = (USHORT)( sizeof( ULONG ) * DumpDataCount );
for( i=0; i<DumpDataCount; i++ )
pPacket->DumpData[i] = DumpData[i];
}
else pPacket->DumpDataSize = 0;
pPacket->NumberOfStrings = (USHORT)StringCount;
pPacket->StringOffset = (USHORT)(sizeof( IO_ERROR_LOG_PACKET ) +
( DumpDataCount - 1 ) * sizeof( ULONG ));
if( StringCount > 0 )
{
pInsertionString = (PUCHAR)pPacket + pPacket->StringOffset;
for( i=0; i<StringCount; i++ )
{
RtlCopyBytes(pInsertionString,Strings[i],StringSize[i] );
pInsertionString += StringSize[i];
}
}
IoWriteErrorLogEntry( pPacket );
}
else
bReturn = FALSE;
}
return bReturn;
}
//<summary>
// ======================================================================
//<newpara> Author :BD Date : 04-12-2001 </newpara>
// ======================================================================
//<newpara> Function : InitializeEventLog</newpara>
//<newpara> Comments : </newpara>
//</summary>
//Parameters :
// <param name="pDriverObject"> </param>
//<returns> </returns>
// ======================================================================
void InitializeEventLog(IN PDRIVER_OBJECT pDriverObject)
{
LogLevel = LOG_LEVEL_DEBUG;
ReportEvent(LOG_LEVEL_DEBUG,MSG_LOGGING_ENABLED,ERRORLOG_INIT,(PVOID)pDriverObject,
NULL, NULL, 0, NULL, 0);
}
#endif
#pragma warning(pop)
#else
#error Modification required for this type of OS
#endif
#endif // ADD_FUN_CPP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -