我是靠谱客的博主 忐忑百合,最近开发中收集的这篇文章主要介绍使用设备对象指针来进行驱动调用驱动,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

// dest
#include <ntddk.h>

// 卸载函数
VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject)
{
	KdPrint(("驱动卸载n"));
	UNREFERENCED_PARAMETER(pDriverObject);

	UNICODE_STRING DevSymbolicLinkName = { 0 };
	RtlInitUnicodeString(&DevSymbolicLinkName, L"\??\DeviceObjectPointer");

	IoDeleteSymbolicLink(&DevSymbolicLinkName);

	if (pDriverObject->DeviceObject != NULL)
	{
		IoDeleteDevice(pDriverObject->DeviceObject);
	}
}

// 入口函数
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath)
{
	NTSTATUS status = STATUS_SUCCESS;
	PDEVICE_OBJECT pDevObj = NULL;
	UNICODE_STRING DevName = { 0 };
	UNICODE_STRING DevSymbolicLinkName = { 0 };

	KdPrint(("驱动加载n"));
	UNREFERENCED_PARAMETER(pRegistryPath);

	RtlInitUnicodeString(&DevName, L"\Device\DeviceObjectPointer");
	RtlInitUnicodeString(&DevSymbolicLinkName, L"\??\DeviceObjectPointer");

	status = IoCreateDevice(pDriverObject, 0, &DevName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDevObj);
	if (!NT_SUCCESS(status))
	{
		KdPrint(("创建设备失败:%xn", status));
		return status;
	}

	status = IoCreateSymbolicLink(&DevSymbolicLinkName, &DevName);
	if (!NT_SUCCESS(status))
	{
		KdPrint(("创建符号链接失败:%xn", status));
		IoDeleteDevice(pDevObj);
		return status;
	}

	pDevObj->Flags |= DO_BUFFERED_IO;
	pDevObj->Flags &= ~DO_DEVICE_INITIALIZING;

	pDriverObject->DriverUnload = DriverUnload;


	return status;
}

// test
#include <ntddk.h>

// 卸载函数
VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject)
{
	KdPrint(("卸载函数n"));
	UNREFERENCED_PARAMETER(pDriverObject);
}

// 测试函数-同步
VOID CallDriverTestSync()
{
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING DevName = { 0 };
	OBJECT_ATTRIBUTES oa = { 0 };
	IO_STATUS_BLOCK block = { 0 };
	KEVENT Event = { 0 };
	LARGE_INTEGER offset = RtlConvertLongToLargeInteger(0);
	PDEVICE_OBJECT pDevObj = NULL;
	PFILE_OBJECT pFileObj = NULL;
	PIRP pIrp = NULL;
	PIO_STACK_LOCATION stack = NULL;

	RtlInitUnicodeString(&DevName, L"\Device\DeviceObjectPointer");
	InitializeObjectAttributes(&oa, &DevName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);

	status = IoGetDeviceObjectPointer(&DevName, FILE_ANY_ACCESS, &pFileObj, &pDevObj);
	if (!NT_SUCCESS(status))
	{
		KdPrint(("获取设备对象指针失败:%xn", status));
		return;
	}

	KeInitializeEvent(&Event, SynchronizationEvent, FALSE);

	pIrp = IoBuildSynchronousFsdRequest(IRP_MJ_READ, pDevObj, NULL, 0, &offset, &Event, &block);
	stack = IoGetNextIrpStackLocation(pIrp);
	stack->FileObject = pFileObj;

	status = IoCallDriver(pDevObj, pIrp);
	if (status == STATUS_PENDING)
	{
		KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
		KdPrint(("读操作完成n"));
	}

	ObDereferenceObject(pFileObj);
}

// 测试函数-异步
VOID CallDriverTestASync()
{
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING DevName = { 0 };
	OBJECT_ATTRIBUTES oa = { 0 };
	IO_STATUS_BLOCK block = { 0 };
	KEVENT Event = { 0 };
	LARGE_INTEGER offset = RtlConvertLongToLargeInteger(0);
	PDEVICE_OBJECT pDevObj = NULL;
	PFILE_OBJECT pFileObj = NULL;
	PIRP pIrp = NULL;
	PIO_STACK_LOCATION stack = NULL;

	RtlInitUnicodeString(&DevName, L"\Device\DeviceObjectPointer");
	InitializeObjectAttributes(&oa, &DevName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);

	status = IoGetDeviceObjectPointer(&DevName, FILE_ANY_ACCESS, &pFileObj, &pDevObj);
	if (!NT_SUCCESS(status))
	{
		KdPrint(("获取设备对象指针失败:%xn", status));
		return;
	}

	KeInitializeEvent(&Event, SynchronizationEvent, FALSE);

	pIrp = IoBuildAsynchronousFsdRequest(IRP_MJ_READ, pDevObj, NULL, 0, &offset, &block);
	pIrp->UserEvent = &Event;
	stack = IoGetNextIrpStackLocation(pIrp);
	stack->FileObject = pFileObj;

	status = IoCallDriver(pDevObj, pIrp);
	if (NT_SUCCESS(status))
	{
		if (status == STATUS_PENDING)
		{
			KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
			KdPrint(("读操作完成n"));
		}
	}
	else
	{
		KdPrint(("失败:%xn", status));
	}


	ObDereferenceObject(pFileObj);
}

// 入口函数
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath)
{
	NTSTATUS status = STATUS_SUCCESS;

	KdPrint(("驱动加载n"));
	UNREFERENCED_PARAMETER(pRegistryPath);

	pDriverObject->DriverUnload = DriverUnload;

	return status;
}

最后

以上就是忐忑百合为你收集整理的使用设备对象指针来进行驱动调用驱动的全部内容,希望文章能够帮你解决使用设备对象指针来进行驱动调用驱动所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部