 |
【HelloWDM.inf】 [Version] Signature="$CHICAGO$" Provider=Zhangfan_Device DriverVer=11/1/2007,3.0.0.3 Class=ZhangfanDevice ClassGUID={EF2962F0-0D55-4bff-B8AA-2221EE8A79B0}
[SourceDisksNames] 1 = "HelloWDM",Disk1,,
[SourceDisksFiles] HelloWDM.sys = 1,MyDriver_Check,
[ClassInstall] Addreg=Class_AddReg
[ClassInstall32] Addreg=Class_AddReg
[Class_AddReg] HKR,,,,%DeviceClassName% HKR,,Icon,,"-5"
[DestinationDirs] YouMark_Files_Driver = 10,System32\Drivers
[Manufacturer] %MfgName%=Mfg0
[Mfg0] %DeviceDesc%=YouMark_DDI, PCI\VEN_9999&DEV_9999
[YouMark_DDI] CopyFiles=YouMark_Files_Driver AddReg=YouMark_9X_AddReg
[YouMark_9X_AddReg] HKR,,DevLoader,,*ntkern HKR,,NTMPDriver,,HelloWDM.sys HKR, "Parameters", "BreakOnEntry", 0x00010001, 0
[YouMark_DDI.NT] CopyFiles=YouMark_Files_Driver AddReg=YouMark_NT_AddReg
[YouMark_DDI.NT.Services] Addservice = HelloWDM, 0x00000002, YouMark_AddService
[YouMark_AddService] DisplayName = %SvcDesc% ServiceType = 1 StartType = 3 ErrorControl = 1 ServiceBinary = %10%\System32\Drivers\HelloWDM.sys
[YouMark_NT_AddReg] HKLM, "System\CurrentControlSet\Services\HelloWDM\Parameters",\ "BreakOnEntry", 0x00010001, 0
[YouMark_Files_Driver] HelloWDM.sys
[Strings] ProviderName="Zhangfan." MfgName="Zhangfan Soft" DeviceDesc="Hello World WDM!" DeviceClassName="Zhangfan_Device" SvcDesc="Zhangfan"
|
 |
【main.c】 #include <wdm.h>
struct device_extension { PDEVICE_OBJECT device; PDEVICE_OBJECT next_stack_device; UNICODE_STRING device_name; UNICODE_STRING symlink_name; };
NTSTATUS hello_add_device(PDRIVER_OBJECT driver, PDEVICE_OBJECT physical_device) { struct device_extension *ext; NTSTATUS status; PDEVICE_OBJECT device; UNICODE_STRING device_name; RtlInitUnicodeString(&device_name, L"\\Device\\MyWDMDevice"); status = IoCreateDevice(driver, sizeof(struct device_extension), &device_name, FILE_DEVICE_UNKNOWN, 0, FALSE, &device); if (!NT_SUCCESS(status)) return status; device->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE; ext = device->DeviceExtension; ext->device = device; ext->next_stack_device = IoAttachDeviceToDeviceStack(device, physical_device); ext->device_name = device_name; RtlInitUnicodeString(&ext->symlink_name, L"\\DosDevices\\HelloDDK"); status = IoCreateSymbolicLink(&ext->symlink_name, &device_name); if (!NT_SUCCESS(status)) { IoDeleteDevice(device); return status; } device->Flags &= ~DO_DEVICE_INITIALIZING; return STATUS_SUCCESS; }
void hello_unload(PDRIVER_OBJECT driver) { KdPrint(("Enter hello_unload\n")); KdPrint(("Leave hello_unload\n")); }
NTSTATUS hello_default_pnp_handler(struct device_extension *ext, PIRP irp) { KdPrint(("default pnp handler\n")); IoSkipCurrentIrpStackLocation(irp); return IoCallDriver(ext->next_stack_device, irp); }
NTSTATUS hello_pnp(PDEVICE_OBJECT device, PIRP irp) { struct device_extension *ext; PIO_STACK_LOCATION stack; NTSTATUS status; KdPrint(("Enter hello_pnp\n")); ext = device->DeviceExtension; stack = IoGetCurrentIrpStackLocation(irp); if (stack->MinorFunction == 2) { KdPrint(("handle remove device\n")); status = hello_default_pnp_handler(ext, irp); IoDeleteSymbolicLink(&ext->symlink_name); if (ext->next_stack_device) IoDetachDevice(ext->next_stack_device); IoDeleteDevice(ext->device); } else status = hello_default_pnp_handler(ext, irp); KdPrint(("Leave hello_pnp\n")); return status; }
NTSTATUS hello_proc(PDEVICE_OBJECT device, PIRP irp) { KdPrint(("Enter hello_proc\n")); irp->IoStatus.Status = STATUS_SUCCESS; irp->IoStatus.Information = 0; // bytes transfered IoCompleteRequest(irp, IO_NO_INCREMENT); KdPrint(("Leave hello_proc\n")); return STATUS_SUCCESS; }
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING registry_path) { KdPrint(("Enter DriverEntry\n")); driver->DriverExtension->AddDevice = hello_add_device; driver->DriverUnload = hello_unload; driver->MajorFunction[IRP_MJ_PNP] = hello_pnp; driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = hello_proc; driver->MajorFunction[IRP_MJ_CREATE] = hello_proc; driver->MajorFunction[IRP_MJ_WRITE] = hello_proc; driver->MajorFunction[IRP_MJ_READ] = hello_proc; KdPrint(("Leave DriverEntry\n")); return STATUS_SUCCESS; }
|
 |
【Sources】 TARGETNAME=HelloWDM TARGETTYPE=DRIVER DRIVERTYPE=WDM TARGETPATH=OBJ
INCLUDES=$(BASEDIR)\inc;$(BASEDIR)\inc\ddk
SOURCES=main.c
|
 |
改下%DeviceDesc%=YouMark_DDI, PCI\VEN_8086&DEV_9D3A就可以匹配真實的未安裝驅動的硬件。 
|