A novice would like to ask about the driver version of helloworld, experts please give me some advice![Copy link]
I've been learning about drivers recently and have come across many confusing questions. I'm begging for advice from experts. 1. How do I trace and debug into the sys file? Is there any specific method? I only know how to add a MessageBox in main.c, and then use softice to break the MessageBox, but I still can't get into sys. 2. I traced into DriverControl in main.c, and after passing in -start, DeviceIoControl returned 0, and GetLastErr() returned 87. What went wrong? Will entering DeviceIoControl jump to the driver's Dispath function? Here is the code: HelloWorld.c #ifndef __HELLOWORLD_C__ #define __HELLOWORLD_C__ #define DEBUGMSG #include
#define DEVICE_HELLO_INDEX 0x860 //2 IOCTL macros #define START_HELLPWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX,METHOD_BUFFERED,FILE_ANY_ACCESS) #define STOP_HELLPWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX+1,METHOD_BUFFERED,FILE_ANY_ACCESS) #define NT_DEVICE_NAME L"\\Device\\HelloWorld" //Device name #define DOS_DEVICE_NAME L"\\DosDevices\\HelloWorld" //Symbolic connection NTSTATUS HelloWorldDispatch (IN PDEVICE_OBJECT DeviceObject,IN PIRP pIrp); VOID HelloWorldUnLoad (IN PDRIVER_OBJECT DriverObject); //Driver entry NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath) { NTSTATUS ntStatus=STATUS_SUCCESS; PDEVICE_OBJECT lpDeviceObject=NULL; //Pointer to device object UNICODE_STRING DeviceNameString={0}; //Device name UNICODE_STRING DeviceLinkString ={0}; //Symbolic link //Debug information #ifdef DEBUGMSG DbgPrint("Starting DriverEntry()\n"); #endif RtlInitUnicodeString(&DeviceNameString,NT_DEVICE_NAME); //Initialize Unicode string //Create device ntStatus=IoCreateDevice (DriverObject,0,&DeviceNameString,FILE_DEVICE_UNKNOWN,0,FALSE,&lpDeviceObject); //Use NT_SUCCESS macro to detect whether the function call is successful if (!NT_SUCCESS(ntStatus)) { #ifdef DEBUGMSG DbgPrint("IoCreateDevice() error reports 0x%08X\n",ntStatus); #endif return ntStatus; } RtlInitUnicodeString(&DeviceLinkString,DOS_DEVICE_NAME); //Create symbolic link ntStatus=IoCreateSymbolicLink (&DeviceLinkString,&DeviceNameString); if (!NT_SUCCESS(ntStatus)) { #ifdef DEBUGMSG DbgPrint("IoCreateSymbolicLink() error reports 0x%08X\n",ntStatus); #endif if (lpDeviceObject) IoDeleteDevice(lpDeviceObject); return ntStatus; } //Set IRP dispatch routine and unload routine DriverObject->MajorFunction[IRP_MJ_CREATE]= DriverObject->MajorFunction[IRP_MJ_CLOSE]= DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]=HelloWorldDispatch; DriverObject->DriverUnload=HelloWorldUnLoad; return ntStatus; } NTSTATUS HelloWorldDispatch (IN PDEVICE_OBJECT DeviceObject,IN PIRP pIrp) { NTSTATUS ntStatus=STATUS_SUCCESS; PIO_STACK_LOCATION IrpStack=NULL; //IRP stack ULONG IoControlCodes=0; //I/O control code//Set IRP status pIrp->IoStatus.Status=STATUS_SUCCESS; pIrp->IoStatus.Information=0; #ifdef DEBUGMSG DbgPrint("Starting HelloWorldDispatch()\n"); #endif IrpStack=IoGetCurrentIrpStackLocation(pIrp); //Get the IRP of the current caller switch (IrpStack->MajorFunction) { case IRP_MJ_CREATE: #ifdef DEBUGMSG DbgPrint("IRP_MJ_CREATE\n"); #endif break; case IRP_MJ_CLOSE: #ifdef DEBUGMSG DbgPrint( "IRP_MJ_CLOSE\n"); #endif break; case IRP_MJ_DEVICE_CONTROL: #ifdef DEBUGMSG DbgPrint("IRP_MJ_DEVICE_CONTROL\n"); #endif //Get I/O control code IoControlCodes=IrpStack->Parameters.DeviceIoControl.IoControlCode;
switch (IoControlCodes)
{
//启动
case START_HELLPWORLD:
DbgPrint("Starting \"Hello World\"\n");
break;
//停止
case STOP_HELLPWORLD:
DbgPrint("Stoping \"Hello World\"\n");
break;
I changed the call of DeviceIoControl function to DeviceIoControl(hDevice,STOP_HELLPWORLD,NULL,0,NULL,0,,&BytesReturn,,(LPOVERLAPPED)NULL) but it didn't work.