本文主要是介绍DDK 最纯净的Hello world,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
只包含DriverEntry,IRP_MJ_DEVICE_CONTROL,DriverUnload函数
是DDK的基本结构
没有任何功能,所有DDK都要以这个结构来写
- #include <ntddk.h>
- #define DEVICE_NAME L"//Device//hello"
- #define DOS_NAME L"//??//hello"
- void Unload(IN PDRIVER_OBJECT DriverObject);
- NTSTATUS MyIoControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
- NTSTATUS MydrvDispatch (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
- NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject , IN PUNICODE_STRING RegistryPath)
- {
- UNICODE_STRING nameString, linkString;
- NTSTATUS status=STATUS_SUCCESS;
- ULONG i;
- PDEVICE_OBJECT deviceObject;
- RtlInitUnicodeString(&nameString,DEVICE_NAME);
- RtlInitUnicodeString(&linkString,DOS_NAME);
- status = IoCreateDevice( DriverObject,0,&nameString,FILE_DEVICE_DISK_FILE_SYSTEM,FILE_DEVICE_SECURE_OPEN,TRUE,&deviceObject);
- if (!NT_SUCCESS( status ))
- return status;
- deviceObject->Flags|=DO_BUFFERED_IO;
- status = IoCreateSymbolicLink (&linkString, &nameString);
- if (!NT_SUCCESS( status ))
- {
- IoDeleteDevice (DriverObject->DeviceObject);
- return status;
- }
- for ( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
- {
- DriverObject->MajorFunction[i] = MydrvDispatch;
- }
- DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = MyIoControl;
- DriverObject->DriverUnload=Unload;
- DbgPrint("DriverEntry.../n");
- return status;
- }
- void Unload(IN PDRIVER_OBJECT DriverObject)
- {
- PDEVICE_OBJECT deviceObject = DriverObject->DeviceObject;
- UNICODE_STRING uniWin32NameString;
- RtlInitUnicodeString( &uniWin32NameString, DOS_NAME );
- IoDeleteSymbolicLink( &uniWin32NameString );
- if ( deviceObject != NULL )
- {
- IoDeleteDevice( deviceObject );
- }
- return;
- }
- NTSTATUS MydrvDispatch (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
- {
- Irp->IoStatus.Status = STATUS_SUCCESS;
- Irp->IoStatus.Information = 0L;
- IoCompleteRequest( Irp, 0 );
- return Irp->IoStatus.Status;
- }
- NTSTATUS MyIoControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
- {
- NTSTATUS status=STATUS_SUCCESS;
- PIO_STACK_LOCATION irpStack;
- PVOID ioBuffer;
- ULONG inputBufferLength;
- ULONG outputBufferLength;
- ULONG ioControlCode;
- irpStack = IoGetCurrentIrpStackLocation(Irp);
- ioControlCode= irpStack->Parameters.DeviceIoControl.IoControlCode;
- ioBuffer = Irp->AssociatedIrp.SystemBuffer;
- inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
- outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
- switch (ioControlCode)
- {
- }
- Irp->IoStatus.Status = status;
- Irp->IoStatus.Information = outputBufferLength;
- IoCompleteRequest(Irp, IO_NO_INCREMENT);
- return status;
- }
这篇关于DDK 最纯净的Hello world的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!