|
我刚学习驱动程序的开发,编译好以后用DriverStudio的DriverMonitor加载,可是停不掉,不得已重启系统。请问有什么方法可以在不重启系统情况下把驱动停止?谢谢!下面是驱动代码,系统是xp sp2。
- #define NTNAME_STRING L"\\device\\test_driver1"
- #define WIN32NAME_STRING L"\\dosdevices\\test_driver1"
- VOID
- test_driver1Unload(PDRIVER_OBJECT pdriver_object)
- {
- PDEVICE_OBJECT pdevice_object ;
- UNICODE_STRING win32_name ;
- PAGED_CODE();
- RtlInitUnicodeString(&win32_name,WIN32NAME_STRING);
- IoDeleteSymbolicLink(&win32_name);
- pdevice_object = pdriver_object->DeviceObject ;
- if(pdevice_object!=NULL)
- IoDeleteDevice(pdevice_object);
- }
- NTSTATUS
- test_driver1CreateClose(__in PDEVICE_OBJECT pdevice_object,
- __in PIRP pirp)
- {
- PIO_STACK_LOCATION psLocation ;
- PAGED_CODE();
- psLocation = IoGetCurrentIrpStackLocation(pirp);
- if(psLocation->MajorFunction==IRP_MJ_CREATE ||
- psLocation->MajorFunction==IRP_MJ_CLOSE)
- {
- pirp->IoStatus.Status = STATUS_SUCCESS;
- pirp->IoStatus.Information = 0 ;
- IoCompleteRequest(pirp, IO_NO_INCREMENT) ;
- }
- else
- {
- DbgPrint("\nInvalid major function code in test_driver1CreateClose\n") ;
- return STATUS_NOT_IMPLEMENTED ;
- }
- return STATUS_SUCCESS ;
- }
- NTSTATUS
- test_driver1DeviceControl(__in PDEVICE_OBJECT pdevice_object,
- __in PIRP pirp)
- {
- PIO_STACK_LOCATION psLocate ;
- PCHAR inbuf, outbuf, str = "driver replies" ;
- ULONG len ;
- PAGED_CODE();
- psLocate = IoGetCurrentIrpStackLocation(pirp);
- if(psLocate->Parameters.DeviceIoControl.IoControlCode==BUFFER_METHOD_0)
- {
- len = psLocate->Parameters.DeviceIoControl.InputBufferLength ;
- inbuf = pirp->AssociatedIrp.SystemBuffer ;
- outbuf = pirp->AssociatedIrp.SystemBuffer ;
- RtlCopyBytes(outbuf,str,strlen(str)+1) ;
- pirp->IoStatus.Information = len ;
- pirp->IoStatus.Status = STATUS_SUCCESS ;
- IoCompleteRequest(pirp, IO_NO_INCREMENT) ;
- return STATUS_SUCCESS ;
- }
- else
- {
- DbgPrint("\ninvalid Io control parameter in test_driver1DeviceControl\n") ;
- return STATUS_INVALID_PARAMETER ;
- }
-
- }
- NTSTATUS
- DriverEntry(__in PDRIVER_OBJECT pdriver_object,
- __in PUNICODE_STRING pregistry_path)
- {
- /*driver returned status*/
- NTSTATUS status = STATUS_SUCCESS;
- PDEVICE_OBJECT pdevice_object ;
- UNICODE_STRING nt_name, win32_name ;
- RtlInitUnicodeString(&nt_name, NTNAME_STRING) ;
- status = IoCreateDevice(pdriver_object,
- 0,
- &nt_name,
- FILE_DEVICE_UNKNOWN,
- FILE_DEVICE_SECURE_OPEN,
- FALSE,
- &pdevice_object
- ) ;
- if(status != STATUS_SUCCESS ) {
- DbgPrint("\nIoCreateDevice failed with 0x%x\n", status) ;
- return status ;
- }
- pdriver_object->MajorFunction[IRP_MJ_DEVICE_CONTROL] = test_driver1DeviceControl ;
- pdriver_object->MajorFunction[IRP_MJ_CREATE] = test_driver1CreateClose ;
- pdriver_object->MajorFunction[IRP_MJ_CLOSE] = test_driver1CreateClose ;
- RtlInitUnicodeString(&win32_name, WIN32NAME_STRING) ;
- status = IoCreateSymbolicLink(&win32_name, &nt_name) ;
- if(!NT_SUCCESS(status)) {
- DbgPrint("IoCreateSymbolicLink failed with 0x%x",status);
- return status ;
- }
- return status ;
- }
复制代码
|
|