커 널 스 레 드 생 성 및 소각

본문
커 널 에 스 레 드 를 만 들 려 면 PsCreate System Thread 를 사용 하 십시오.독서 노트 일 뿐, 고수 가 날 아 간다 ~ ~ ~ ~
KmdManager 로 드라이버 를 불 러 온 다음 DebugView 에서...
SysThread. c 부분 코드
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING regPath
)
{
PDEVICE_OBJECT pDeviceObject = NULL;
NTSTATUS ntStatus;
UNICODE_STRING uniNtNameString, uniWin32NameString;


RtlInitUnicodeString( &uniNtNameString, NT_DEVICE_NAME );
ntStatus = IoCreateDevice (
            pDriverObject,
                           sizeof(SYSTHREAD_DEVICE_EXTENSION), // DeviceExtensionSize
                           &uniNtNameString,
                           FILE_DEVICE_UNKNOWN,         //
                           0,              // No standard device characteristics
                           FALSE,             // not exclusive device
                           &pDeviceObject
                           );
if( !NT_SUCCESS(ntStatus) ) {
         return ntStatus;
}

//     
pDriverObject->MajorFunction[IRP_MJ_CREATE] = SysThreadOpen;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = SysThreadClose;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SysThreadDeviceIoControl;
pDriverObject->DriverUnload = SysThreadUnload;

pDeviceObject->Flags |= DO_BUFFERED_IO;


RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );
ntStatus = IoCreateSymbolicLink( &uniWin32NameString, &uniNtNameString );
if (!NT_SUCCESS(ntStatus)){
         IoDeleteDevice( pDriverObject->DeviceObject );
}

return ntStatus;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void
SysThreadUnload(
IN PDRIVER_OBJECT pDriverObject
)
{
PDEVICE_OBJECT pDeviceObject;
UNICODE_STRING uniWin32NameString;

pDeviceObject = pDriverObject->DeviceObject;

RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );
IoDeleteSymbolicLink( &uniWin32NameString );
IoDeleteDevice( pDriverObject->DeviceObject );
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

NTSTATUS
SysThreadOpen(
IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp
)
{
KdPrint((" SysThreadOpen() was Called.... 
")); pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); return STATUS_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// NTSTATUS SysThreadClose( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIrp ) { KdPrint((" SysThreadClose() was Called....
")); pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); return STATUS_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// NTSTATUS SysThreadDeviceIoControl( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIrp ) { NTSTATUS ntStatus = STATUS_SUCCESS; PIO_STACK_LOCATION pIrpStack; PSYSTHREAD_DEVICE_EXTENSION pdx; ULONG dwControlCode; pdx = (PSYSTHREAD_DEVICE_EXTENSION) pDeviceObject->DeviceExtension; pIrpStack = IoGetCurrentIrpStackLocation( pIrp ); dwControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode; switch(dwControlCode) { case IOCTL_SYSTHREAD_START: StartThread(pdx); // break; case IOCTL_SYSTHREAD_STOP: StopThread(pdx); // break; default: break; } pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); return ntStatus; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// NTSTATUS StartThread(PSYSTHREAD_DEVICE_EXTENSION pdx) { NTSTATUS status; HANDLE hthread; // event KeInitializeEvent(&pdx->evKill, SynchronizationEvent, // auto reset FALSE // initial state : FALSE ==> non-signaled ); // ThreadProc status = PsCreateSystemThread(&hthread, THREAD_ALL_ACCESS, NULL, NULL, NULL, (PKSTART_ROUTINE) ThreadProc, pdx ); if( !NT_SUCCESS(status)) { KdPrint(("Fail Start ThreadProc()!
")); return status; } ObReferenceObjectByHandle( hthread, THREAD_ALL_ACCESS, NULL, KernelMode, (PVOID *) &pdx->thread, NULL ); ZwClose(hthread); return STATUS_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// VOID StopThread(PSYSTHREAD_DEVICE_EXTENSION pdx) { KeSetEvent(&pdx->evKill, 0, FALSE); // KeSetEvent KeWaitForSingleObject(pdx->thread, Executive, KernelMode, FALSE, NULL); ObDereferenceObject(pdx->thread); } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// VOID ThreadProc(PSYSTHREAD_DEVICE_EXTENSION pdx) { NTSTATUS status; int cnt = 0; LARGE_INTEGER timeout; timeout.QuadPart = -1 * 10000000; // 1 second // , while(1) { status = KeWaitForSingleObject(&pdx->evKill, Executive, KernelMode, FALSE, &timeout); if( status == STATUS_TIMEOUT ) KdPrint(("^_^ ThreadProc() %d !
", cnt++)); else break; } KdPrint(("^_^ ThreadProc() !
")); PsTerminateSystemThread(STATUS_SUCCESS); }

좋은 웹페이지 즐겨찾기