static VOID NTAPI driver_unload(IN PDRIVER_OBJECT DriverObject) { WCHAR deviceLinkBuffer[] = L"\\DosDevices\\"CO_DRIVER_NAME; UNICODE_STRING deviceLinkUnicodeString; co_manager_t *manager; manager = DriverObject->DeviceObject->DeviceExtension; if (manager) { co_manager_unload(manager); } RtlInitUnicodeString(&deviceLinkUnicodeString, deviceLinkBuffer); IoDeleteSymbolicLink(&deviceLinkUnicodeString); IoDeleteDevice(DriverObject->DeviceObject); }
NTSTATUS CreateDevice ( IN PDRIVER_OBJECT pDriverObject) { NTSTATUS status; PDEVICE_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; //创建设备名称 UNICODE_STRING devName; RtlInitUnicodeString(&devName,L"\\Device\\MyDDKDevice"); //创建设备 status = IoCreateDevice( pDriverObject, sizeof(DEVICE_EXTENSION), &(UNICODE_STRING)devName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevObj ); if (!NT_SUCCESS(status)) return status; pDevObj->Flags |= DO_DIRECT_IO; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->pDevice = pDevObj; pDevExt->ustrDeviceName = devName; //申请模拟文件的缓冲区 pDevExt->buffer = (PUCHAR)ExAllocatePool(PagedPool,MAX_FILE_LENGTH); //设置模拟文件大小 pDevExt->file_length = 0; //创建符号链接 UNICODE_STRING symLinkName; RtlInitUnicodeString(&symLinkName,L"\\??\\HelloDDK"); pDevExt->ustrSymLinkName = symLinkName; status = IoCreateSymbolicLink( &symLinkName,&devName ); if (!NT_SUCCESS(status)) { IoDeleteDevice( pDevObj ); return status; } return STATUS_SUCCESS; }
VOID BBUnload( IN PDRIVER_OBJECT DriverObject ) { UNICODE_STRING deviceLinkUnicodeString; // Unregister notification PsSetCreateProcessNotifyRoutine( BBProcessNotify, TRUE ); // Cleanup physical regions BBCleanupProcessPhysList(); // Cleanup process mapping info BBCleanupProcessTable(); RtlUnicodeStringInit( &deviceLinkUnicodeString, DOS_DEVICE_NAME ); IoDeleteSymbolicLink( &deviceLinkUnicodeString ); IoDeleteDevice( DriverObject->DeviceObject ); return; }
static VOID i8042RemoveDevice( IN PDEVICE_OBJECT DeviceObject) { PI8042_DRIVER_EXTENSION DriverExtension; KIRQL OldIrql; PFDO_DEVICE_EXTENSION DeviceExtension; DriverExtension = (PI8042_DRIVER_EXTENSION)IoGetDriverObjectExtension(DeviceObject->DriverObject, DeviceObject->DriverObject); DeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension; KeAcquireSpinLock(&DriverExtension->DeviceListLock, &OldIrql); RemoveEntryList(&DeviceExtension->ListEntry); KeReleaseSpinLock(&DriverExtension->DeviceListLock, OldIrql); IoDetachDevice(DeviceExtension->LowerDevice); IoDeleteDevice(DeviceObject); }
VOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject) { PDEVICE_OBJECT pNextObj; KdPrint(("DriverB:Enter B DriverUnload\n")); pNextObj = pDriverObject->DeviceObject; while (pNextObj != NULL) { PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION) pNextObj->DeviceExtension; //删除符号链接 UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName; IoDeleteSymbolicLink(&pLinkName); pNextObj = pNextObj->NextDevice; IoDeleteDevice( pDevExt->pDevice ); } KdPrint(("DriverB:Enter B DriverUnload\n")); }
NTSTATUS DriverEntry(PDRIVER_OBJECT DrvObj, PUNICODE_STRING RegPath) { PDEVICE_OBJECT DevObj; NTSTATUS status; status = IoCreateDevice(DrvObj, 0, &deviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &DevObj); if(NT_SUCCESS(status)) { status = IoCreateSymbolicLink (&deviceLink, &deviceName); DrvObj->MajorFunction[IRP_MJ_CREATE] = DtraceOpen; DrvObj->MajorFunction[IRP_MJ_CLOSE] = DtraceClose; DrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DtraceIoctl; DrvObj->DriverUnload = DtraceUnload; } if (!NT_SUCCESS(status)) { IoDeleteSymbolicLink(&deviceLink); if(DevObj) IoDeleteDevice( DevObj); return status; } status = IoCreateSymbolicLink(&deviceHelperLink, &deviceName); if (!NT_SUCCESS(status)) dprintf("DriverEntry: dtrace helper creation failed\n"); DtraceGetSystemHertz(); DtraceWinOSKernelModuleInfo(); DtraceWinOSInitFunctions(); if (DtraceWinOSHackData() == 0) dprintf("DriverEntry: DtraceWinOSPortData() hack data failure\n"); if (PsSetLoadImageNotifyRoutine(ProcKernelModuleLoaded) != STATUS_SUCCESS) dprintf("DriverEntry: failed to register PsSetLoadImageNotifyRoutine\n"); WorkItem1 = IoAllocateWorkItem(DevObj); WorkItem2 = IoAllocateWorkItem(DevObj); int_morecore(); (void) dtrace_load((void *) RegPath); return status; }
VOID ParUnload( IN PDRIVER_OBJECT DriverObject ) /*++ Routine Description: This routine loops through the device list and cleans up after each of the devices. Arguments: DriverObject - Supplies the driver object. Return Value: None. --*/ { PDEVICE_OBJECT currentDevice; PDEVICE_EXTENSION extension; KEVENT event; PARALLEL_INTERRUPT_SERVICE_ROUTINE interruptService; PIRP irp; IO_STATUS_BLOCK ioStatus; while (currentDevice = DriverObject->DeviceObject) { extension = currentDevice->DeviceExtension; if (extension->CreatedSymbolicLink) { IoDeleteSymbolicLink(&extension->SymbolicLinkName); ExFreePool(extension->SymbolicLinkName.Buffer); } IoDeleteDevice(currentDevice); } }
NTSTATUS DestroySoundDevice( IN PDEVICE_OBJECT DeviceObject, IN PCWSTR WideDosDeviceName, IN UCHAR Index) { NTSTATUS Status; UNICODE_STRING DosDeviceName; /* Check for NULL parameters */ if ( ( ! WideDosDeviceName ) || ( ! DeviceObject ) ) { DPRINT("Unexpected NULL parameter"); return STATUS_INVALID_PARAMETER; } /* Range-check */ if ( Index >= SOUND_MAX_DEVICES ) { DPRINT("Device %d exceeds maximum", Index); return STATUS_INVALID_PARAMETER; } Status = ConstructDeviceName(WideDosDeviceName, Index, &DosDeviceName); if ( ! NT_SUCCESS(Status) ) { return Status; } DPRINT("Deleting symlink %ws\n", DosDeviceName.Buffer); Status = IoDeleteSymbolicLink(&DosDeviceName); DPRINT("Status of symlink deletion is 0x%08x\n", Status); /* ASSERT(NT_SUCCESS(Status)); */ IoDeleteDevice(DeviceObject); return STATUS_SUCCESS; }
NTSTATUS VBoxIrpPnP(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION pStack; PVBOXMOUSE_DEVEXT pDevExt; NTSTATUS rc; LOGF_ENTER(); pStack = IoGetCurrentIrpStackLocation(Irp); pDevExt = (PVBOXMOUSE_DEVEXT) DeviceObject->DeviceExtension; switch (pStack->MinorFunction) { case IRP_MN_REMOVE_DEVICE: { LOGF(("IRP_MN_REMOVE_DEVICE")); IoReleaseRemoveLockAndWait(&pDevExt->RemoveLock, pDevExt); VBoxDeviceRemoved(pDevExt); Irp->IoStatus.Status = STATUS_SUCCESS; rc = VBoxIrpPassthrough(DeviceObject, Irp); IoDetachDevice(pDevExt->pdoParent); IoDeleteDevice(DeviceObject); break; } default: { rc = VBoxIrpPassthrough(DeviceObject, Irp); break; } } if (!NT_SUCCESS(rc) && rc != STATUS_NOT_SUPPORTED) { WARN(("rc=%#x", rc)); } LOGF_LEAVE(); return rc; }
NTSTATUS PassThruAddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo) { DebugPrint("AddDevice"); NTSTATUS status; PDEVICE_OBJECT fdo; // Create our Functional Device Object in fdo status = IoCreateDevice (DriverObject, sizeof(PASSTHRU_DEVICE_EXTENSION), NULL, // No Name FILE_DEVICE_UNKNOWN, 0, FALSE, // Not exclusive &fdo); if( !NT_SUCCESS(status)) return status; // Remember fdo in our device extension PPASSTHRU_DEVICE_EXTENSION dx = (PPASSTHRU_DEVICE_EXTENSION)fdo->DeviceExtension; dx->fdo = fdo; DebugPrint("FDO is %x",fdo); // Register and enable our device interface status = IoRegisterDeviceInterface(pdo, &WDM1_GUID, NULL, &dx->ifSymLinkName); if( !NT_SUCCESS(status)) { IoDeleteDevice(fdo); return status; } IoSetDeviceInterfaceState(&dx->ifSymLinkName, TRUE); DebugPrint("Symbolic Link Name is %T",&dx->ifSymLinkName); // Attach to the driver stack below us dx->NextStackDevice = IoAttachDeviceToDeviceStack(fdo,pdo); // Set fdo flags appropriately fdo->Flags |= DO_BUFFERED_IO|DO_POWER_PAGABLE; fdo->Flags &= ~DO_DEVICE_INITIALIZING; return STATUS_SUCCESS; }
/* Handle an IRP. */ NTSTATUS AoeBusIrpDispatch( IN PDEVICE_OBJECT dev_obj, IN PIRP irp ) { PIO_STACK_LOCATION io_stack_loc; NTSTATUS status; ULONG POINTER_ALIGNMENT code; io_stack_loc = IoGetCurrentIrpStackLocation(irp); switch (io_stack_loc->MajorFunction) { case IRP_MJ_PNP: status = WvlBusPnp(&AoeBusMain, irp); /* Did the bus detach? */ if (AoeBusMain.State == WvlBusStateDeleted) { AoeStop(); /* Delete. */ IoDeleteDevice(AoeBusMain.Fdo); /* Disassociate. */ AoeBusMain.Fdo = NULL; } return status; case IRP_MJ_DEVICE_CONTROL: code = io_stack_loc->Parameters.DeviceIoControl.IoControlCode; return AoeBusDevCtl(irp, code); case IRP_MJ_POWER: return WvlBusPower(&AoeBusMain, irp); case IRP_MJ_CREATE: case IRP_MJ_CLOSE: /* Always succeed with nothing to do. */ return WvlIrpComplete(irp, 0, STATUS_SUCCESS); case IRP_MJ_SYSTEM_CONTROL: return WvlBusSysCtl(&AoeBusMain, irp); default: ; } return WvlIrpComplete(irp, 0, STATUS_NOT_SUPPORTED); }
NTSTATUS DriverEntry( IN PDRIVER_OBJECT InDriverObject, IN PUNICODE_STRING InRegistryPath) { UNREFERENCED_PARAMETER(InRegistryPath); NTSTATUS Status; PDEVICE_OBJECT DeviceObject = NULL; /* Create device... */ if (!NT_SUCCESS(Status = IoCreateDevice( InDriverObject, 0, // DeviceExtensionSize NULL, 0x893D, // DeviceType 0, // DeviceCharacteristics TRUE, // Exclusive &DeviceObject // [OUT] ))) goto ERROR_ABORT; InDriverObject->MajorFunction[IRP_MJ_CREATE] = TestDriverDispatchCreate; InDriverObject->MajorFunction[IRP_MJ_CLOSE] = TestDriverDispatchClose; InDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = TestDriverDispatchDeviceControl; InDriverObject->DriverUnload = TestDriverUnload; // run test code... return RunTestSuite(); ERROR_ABORT: /* Rollback in case of errors... */ if (DeviceObject != NULL) IoDeleteDevice(DeviceObject); return Status; }
//entry point STDCALL NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath){ UNICODE_STRING DeviceNameUnicodeString; UNICODE_STRING DeviceLinkUnicodeString; NTSTATUS ntStatus; PDEVICE_OBJECT DeviceObject = NULL; OutputDebugString ("dhahelper: entering DriverEntry"); RtlInitUnicodeString (&DeviceNameUnicodeString, L"\\Device\\DHAHELPER"); // Create an EXCLUSIVE device object (only 1 thread at a time // can make requests to this device). ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_DHAHELPER,0,TRUE,&DeviceObject); if (NT_SUCCESS(ntStatus)){ // Create dispatch points for device control, create, close. DriverObject->MajorFunction[IRP_MJ_CREATE] = DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = dhahelperdispatch; DriverObject->DriverUnload = dhahelperunload; // Create a symbolic link, e.g. a name that a Win32 app can specify // to open the device. RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER"); ntStatus = IoCreateSymbolicLink(&DeviceLinkUnicodeString,&DeviceNameUnicodeString); if (!NT_SUCCESS(ntStatus)){ // Symbolic link creation failed- note this & then delete the // device object (it's useless if a Win32 app can't get at it). OutputDebugString ("dhahelper: IoCreateSymbolicLink failed"); IoDeleteDevice (DeviceObject); } } else{ OutputDebugString ("dhahelper: IoCreateDevice failed"); } OutputDebugString ("dhahelper: leaving DriverEntry"); return ntStatus; }
// // Delete the associated device and return // static STDCALL void RwPortsUnload(IN PDRIVER_OBJECT DriverObject) { UNICODE_STRING DeviceLinkUnicodeString; NTSTATUS ntStatus=STATUS_SUCCESS; OutputDebugString ("rwports: entering RwPortsUnload"); OutputDebugString ("rwports: unmapping remaining memory"); RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\rwports"); ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString); if (NT_SUCCESS(ntStatus)) { IoDeleteDevice (DriverObject->DeviceObject); } else { OutputDebugString ("rwports: IoDeleteSymbolicLink failed"); } OutputDebugString ("rwports: leaving RwPortsUnload"); }
VOID UnloadDriver(PDRIVER_OBJECT DriverObject) { UNICODE_STRING uniLinkName; PDEVICE_OBJECT CurrentDeviceObject; PDEVICE_OBJECT NextDeviceObject; RtlInitUnicodeString(&uniLinkName,LINK_NAME); IoDeleteSymbolicLink(&uniLinkName); if (DriverObject->DeviceObject!=NULL) { CurrentDeviceObject = DriverObject->DeviceObject; while(CurrentDeviceObject!=NULL) { NextDeviceObject = CurrentDeviceObject->NextDevice; IoDeleteDevice(CurrentDeviceObject); CurrentDeviceObject = NextDeviceObject; } } DbgPrint("UnloadDriver\r\n"); }
VOID DkDetachAndDeleteTgt(PDEVICE_EXTENSION pDevExt) { PUSBPCAP_DEVICE_DATA pDeviceData = pDevExt->context.usb.pDeviceData; if (pDevExt->parentRemoveLock) { IoReleaseRemoveLock(pDevExt->parentRemoveLock, NULL); } if (pDevExt->pNextDevObj) { IoDetachDevice(pDevExt->pNextDevObj); pDevExt->pNextDevObj = NULL; } if (pDevExt->pThisDevObj) { IoDeleteDevice(pDevExt->pThisDevObj); pDevExt->pThisDevObj = NULL; } USBPcapFreeDeviceData(pDevExt); }
/** * @name DriverUnload * * Driver cleanup funtion. * * @param DriverObject * Driver Object */ static VOID NTAPI DriverUnload( IN PDRIVER_OBJECT DriverObject) { PAGED_CODE(); UNREFERENCED_PARAMETER(DriverObject); DPRINT("DriverUnload\n"); TestUnload(DriverObject); if (TestDeviceObject) IoDeleteDevice(TestDeviceObject); if (KmtestDeviceObject) ObDereferenceObject(KmtestDeviceObject); }
VOID AWEAllocUnload(IN PDRIVER_OBJECT DriverObject) { PDEVICE_OBJECT device_object = DriverObject->DeviceObject; UNICODE_STRING sym_link; KdPrint(("AWEAlloc: Unload.\n")); PAGED_CODE(); RtlInitUnicodeString(&sym_link, AWEALLOC_SYMLINK_NAME); IoDeleteSymbolicLink(&sym_link); while (device_object != NULL) { PDEVICE_OBJECT next_device = device_object->NextDevice; IoDeleteDevice(device_object); device_object = next_device; } }
// // 删除一个设备 // VOID XFilter_Delete( IN PDEVICE_OBJECT pDeviceObject ) { PXPACKET_DEVICE_EXTENSION pDeviceExtension; UNICODE_STRING SymbolicLinkName; pDeviceExtension = (PXPACKET_DEVICE_EXTENSION)pDeviceObject->DeviceExtension; RtlInitUnicodeString(&SymbolicLinkName, XPACKET_XFILTER_DOS_DEVICE_NAME); IoDeleteSymbolicLink( &SymbolicLinkName ); pDeviceExtension->ulNodeType = 0; pDeviceExtension->ulNodeSize = 0; IoDeleteDevice( pDeviceObject ); dprintf(("XFilter_Delete Success\n")); }
//------------------------------------------------------------------------------------------------------------------- VOID DriverUnload(IN PDRIVER_OBJECT DriverObject) { UNICODE_STRING deviceLinkUnicodeString; PDEVICE_EXTENSION extension; PIRP pNewIrp = NULL; ULONG m_size; NTSTATUS ntStatus; extension = DriverObject->DeviceObject->DeviceExtension; // Create counted string version of our Win32 device name. RtlInitUnicodeString(&deviceLinkUnicodeString, DOS_DEVICE_NAME); // Delete the link from our device name to a name in the Win32 namespace. IoDeleteSymbolicLink(&deviceLinkUnicodeString); // Finally delete our device object IoDeleteDevice(DriverObject->DeviceObject); }
VOID DkDetachAndDeleteHubFilt(PDEVICE_EXTENSION pDevExt) { NTSTATUS status; if (pDevExt->parentRemoveLock) { IoReleaseRemoveLock(pDevExt->parentRemoveLock, NULL); } if (pDevExt->pNextDevObj) { IoDetachDevice(pDevExt->pNextDevObj); pDevExt->pNextDevObj = NULL; } if (pDevExt->pThisDevObj) { IoDeleteDevice(pDevExt->pThisDevObj); pDevExt->pThisDevObj = NULL; } USBPcapFreeDeviceData(pDevExt); }
NTSTATUS HandleRemoveDevice(PDEVICE_EXTENSION pdx, PIRP Irp) { PAGED_CODE(); KdPrint(("Enter HandleRemoveDevice\n")); Irp->IoStatus.Status = STATUS_SUCCESS; NTSTATUS status = DefaultPnpHandler(pdx, Irp); IoSetDeviceInterfaceState(&pdx->interfaceName, FALSE); RtlFreeUnicodeString(&pdx->interfaceName); //调用IoDetachDevice()把fdo从设备栈中脱开: if (pdx->NextStackDevice) IoDetachDevice(pdx->NextStackDevice); //删除fdo: IoDeleteDevice(pdx->fdo); KdPrint(("Leave HandleRemoveDevice\n")); return status; }
/****************************************************************************** * Driver unload handler * ******************************************************************************/ static VOID DDKAPI my_unload(PDRIVER_OBJECT DriverObject) { ANSI_STRING SymbolicLinkNameA; UNICODE_STRING SymbolicLinkNameW; DbgPrint("DriverUnload called\r\n"); PsSetCreateProcessNotifyRoutine(create_process_watcher, TRUE); PsRemoveLoadImageNotifyRoutine(load_image_watcher); RtlInitString(&SymbolicLinkNameA, MY_DOSDEVICE_NAME); RtlAnsiStringToUnicodeString(&SymbolicLinkNameW, &SymbolicLinkNameA, TRUE); IoDeleteSymbolicLink(&SymbolicLinkNameW); IoDeleteDevice(DriverObject->DeviceObject); for (int i = 0; i < ENT_CNT; ++i) if(g_proc_table[i].pid) DbgPrint("Registered process stays: %d\r\n", g_proc_table[i].pid); }
VOID DokanUnload( __in PDRIVER_OBJECT DriverObject ) /*++ Routine Description: This routine gets called to remove the driver from the system. Arguments: DriverObject - the system supplied driver object. Return Value: NTSTATUS --*/ { PDEVICE_OBJECT deviceObject = DriverObject->DeviceObject; WCHAR symbolicLinkBuf[] = DOKAN_GLOBAL_SYMBOLIC_LINK_NAME; UNICODE_STRING symbolicLinkName; PAGED_CODE(); DDbgPrint("==> DokanUnload\n"); if (GetIdentifierType(deviceObject->DeviceExtension) == DGL) { DDbgPrint(" Delete Global DeviceObject\n"); RtlInitUnicodeString(&symbolicLinkName, symbolicLinkBuf); IoDeleteSymbolicLink(&symbolicLinkName); IoDeleteDevice(deviceObject); } ExDeleteNPagedLookasideList(&DokanIrpEntryLookasideList); DDbgPrint("<== DokanUnload\n"); return; }
NTSTATUS AMFAddDevice( IN PDRIVER_OBJECT DriverObject ) { UNICODE_STRING deviceNameUnicodeString; UNICODE_STRING deviceLinkUnicodeString; NTSTATUS ntStatus; PEPROCESS ep_idle, ep_system; void * ptrNtMajorVersion; RtlInitUnicodeString(&deviceNameUnicodeString, deviceNameBuffer); RtlInitUnicodeString(&deviceLinkUnicodeString, deviceLinkBuffer); ntStatus = IoCreateDevice(DriverObject, 0, &deviceNameUnicodeString, FILE_DEVICE_AMF, 0, TRUE, &g_AMFDevice); if(! NT_SUCCESS(ntStatus)) { DbgPrint("Failed to create device!\n"); return ntStatus; } ntStatus = IoCreateSymbolicLink(&deviceLinkUnicodeString, &deviceNameUnicodeString); if(!NT_SUCCESS(ntStatus)) { IoDeleteDevice(DriverObject->DeviceObject); DbgPrint("Failed to create symbolic link!\n"); return ntStatus; } DbgPrint("AMF driver is loaded\n"); ep_idle = GetIdleProcess(); DbgPrint("Idle Process = 0x%08x\n", ep_idle); ep_system = PsInitialSystemProcess; DbgPrint("System Process = 0x%08x\n", ep_system); ptrNtMajorVersion = GetNtMajorVersion(); DbgPrint("pointer to OS Major Version = 0x%08x\n", ptrNtMajorVersion); PatchDispatcherHeaderSize(ep_idle); PatchPoolTag(ep_system); PatchKernel(ptrNtMajorVersion); return STATUS_SUCCESS; }
VOID DDK_Unload (IN PDRIVER_OBJECT pDriverObject) { PDEVICE_OBJECT pDev;//用来取得要删除设备对象 UNICODE_STRING symLinkName; // UnHook(); if (ishook) {//unhook __asm //去掉页面保护 { cli mov eax,cr0 and eax,not 10000h //and eax,0FFFEFFFFh mov cr0,eax } pcur->E9= oldCode.E9;//1字节 pcur->JMPADDR= oldCode.JMPADDR;//4字节 __asm //恢复页保护 { mov eax,cr0 or eax,10000h //or eax,not 0FFFEFFFFh mov cr0,eax sti } } //end unhook pDev=pDriverObject->DeviceObject; IoDeleteDevice(pDev); //删除设备 //取符号链接名字 RtlInitUnicodeString(&symLinkName,L"\\??\\My_DriverLinkName"); //删除符号链接 IoDeleteSymbolicLink(&symLinkName); KdPrint(("驱动成功被卸载...OK-----------")); //sprintf,printf //取得要删除设备对象 //删掉所有设备 DbgPrint("卸载成功"); }
NTSTATUS AddDevice(PDRIVER_OBJECT pFiDO, PDEVICE_OBJECT pPDO) { NTSTATUS ntStatus; PDEVICE_OBJECT pMause, pLowerDO; PDEVICE_EXTENSION pDeviceExtension; DbgPrint("Mouse Filter: Entry in AddDevice\n"); ntStatus = IoCreateDevice( pFiDO, sizeof(DEVICE_EXTENSION), NULL, /* Device Name like object name. */ FILE_DEVICE_MOUSE, /* Device type. */ 0, FALSE, /* Exclusive. */ &pMause ); if (NT_SUCCESS(ntStatus)) { pLowerDO = IoAttachDeviceToDeviceStack(pMause, pPDO); if (pLowerDO) { DbgPrint("Mouse Filter: AddDevice: Success IoAttachDeviceToDeviceStac\n"); pDeviceExtension = (PDEVICE_EXTENSION)pMause->DeviceExtension; pDeviceExtension->pLowerDO = pLowerDO; pDeviceExtension->inverse = FALSE; pMause->Flags |= pLowerDO->Flags & DO_POWER_PAGABLE;; pMause->Flags &= ~DO_DEVICE_INITIALIZING; ntStatus = STATUS_SUCCESS; } else { DbgPrint("Mouse Filter: AddDevice: Fail IoAttachDeviceToDeviceStack\n"); ntStatus = STATUS_DEVICE_REMOVED; IoDeleteDevice(pMause); } } else { DbgPrint("Mouse Filter: AddDevice - IoCreateDevice failed - %X\n", ntStatus); } return ntStatus; }
NTSTATUS DriverExamplePnP(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { DbgPrint("Enter PnP\n"); PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp); PDriverExample_DEVICE_EXTENSION pExt = ((PDriverExample_DEVICE_EXTENSION)DeviceObject->DeviceExtension); NTSTATUS status; ASSERT(pExt); switch (irpSp->MinorFunction) { case IRP_MN_START_DEVICE: IoSetDeviceInterfaceState(&pExt->DeviceInterface, TRUE); Irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(Irp, IO_NO_INCREMENT); return STATUS_SUCCESS; case IRP_MN_QUERY_REMOVE_DEVICE: Irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(Irp, IO_NO_INCREMENT); return STATUS_SUCCESS; case IRP_MN_REMOVE_DEVICE: IoSetDeviceInterfaceState(&pExt->DeviceInterface, FALSE); status = DriverExampleForwardIrpSynchronous(DeviceObject, Irp); IoDetachDevice(pExt->TargetDeviceObject); IoDeleteDevice(pExt->DeviceObject); RtlFreeUnicodeString(&pExt->DeviceInterface); Irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(Irp, IO_NO_INCREMENT); return STATUS_SUCCESS; case IRP_MN_QUERY_PNP_DEVICE_STATE: status = DriverExampleForwardIrpSynchronous(DeviceObject, Irp); Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT); return status; } DbgPrint("Exit PnP\n"); return DriverExampleDefaultHandler(DeviceObject, Irp); }
VOID HelloDDKUnload( IN PDRIVER_OBJECT pDriverObject ) { PDEVICE_OBJECT pNextObj; KdPrint(("enter DriverUnload!\n")); pNextObj = pDriverObject->DeviceObject; while (pNextObj != NULL) { PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pNextObj->DeviceExtension; // delete symlink //UNICODE_STRING pSymLinkName = pDevExt->ustrSymLinkName; //IoDeleteSymbolicLink(&pSymLinkName); // delete self pNextObj = pNextObj->NextDevice; IoDeleteDevice(pDevExt->pDevice); } }
VOID ioctlUnloadDriver(__in PDRIVER_OBJECT DriverObject) { PDEVICE_OBJECT deviceObject = DriverObject->DeviceObject; UNICODE_STRING uniWin32NameString; UNREFERENCED_PARAMETER(deviceObject); netmap_fini(); keexit_GST(); RtlInitUnicodeString(&uniWin32NameString, NETMAP_DOS_DEVICE_NAME); // Delete the link from our device name to a name in the Win32 namespace. IoDeleteSymbolicLink(&uniWin32NameString); if (deviceObject != NULL) { IoDeleteDevice(deviceObject); } return; }