如果一個(gè)驅(qū)動(dòng)需要和應(yīng)用程序通信 name首先要生成一個(gè)設(shè)備對(duì)象
在windows驅(qū)動(dòng)開(kāi)發(fā)體系中 設(shè)備對(duì)象是非常重要的元素
設(shè)備對(duì)象和分發(fā)函數(shù)構(gòu)成整個(gè)內(nèi)核體系的基本框架 設(shè)備對(duì)象可以在內(nèi)核中暴露給應(yīng)用層 應(yīng)用層可以像操作文件一樣操作它
IoCreateDevice
IoCreateDevice 例程創(chuàng)建設(shè)備對(duì)象供驅(qū)動(dòng)程序使用。
NTSTATUS IoCreateDevice(
[in] PDRIVER_OBJECT DriverObject,
[in] ULONG DeviceExtensionSize,
[in, optional] PUNICODE_STRING DeviceName,
[in] DEVICE_TYPE DeviceType,
[in] ULONG DeviceCharacteristics,
[in] BOOLEAN Exclusive,
[out] PDEVICE_OBJECT *DeviceObject
);
#define DriverName L"\\DEVICE\\MyDriver"
#define SymbolLink L"\\??\\Alex"
NTSTATUS CreateDevice(PDRIVER_OBJECT DriverObject) {
UNICODE_STRING MyDriver;//驅(qū)動(dòng)名字
PDEVICE_OBJECT MyDevice = NULL;
RtlInitUnicodeString(&MyDriver, DriverName);
NTSTATUS Status = IoCreateDevice(DriverObject,
sizeof(DriverObject->DriverExtension),
&MyDriver,
FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN,
FALSE,
&MyDevice);
if (Status == STATUS_SUCCESS) {
KdPrint(("驅(qū)動(dòng)設(shè)備對(duì)象創(chuàng)建成功!\n"));
}
else {
KdPrint(("驅(qū)動(dòng)設(shè)備對(duì)象創(chuàng)建失敗"));
}
UNICODE_STRING SymbolName;
RtlInitUnicodeString(&SymbolName, SymbolLink);
Status = IoCreateSymbolicLink(&SymbolName, &MyDriver);
if (Status == STATUS_SUCCESS) {
KdPrint(("符號(hào)鏈接名已關(guān)聯(lián)\n"));
}
else {
KdPrint(("符號(hào)鏈接名關(guān)聯(lián)失敗"));
IoDeleteDevice(MyDevice);
}
return Status;
}
void DeleteDevice(PDRIVER_OBJECT DriverObject) {
KdPrint(("已進(jìn)入刪除設(shè)備例程"));
if (DriverObject->DeviceObject) {
UNICODE_STRING SymbolName;
RtlInitUnicodeString(&SymbolName, SymbolLink);
IoDeleteSymbolicLink(&SymbolName);
KdPrint(("已經(jīng)刪除符號(hào)鏈接名\n"));
IoDeleteDevice(DriverObject->DeviceObject);
KdPrint(("已經(jīng)刪除驅(qū)動(dòng)設(shè)備"));
}
KdPrint(("已退出刪除設(shè)備例程"));
}
#define IO_TEST CTL_CODE(FILE_DEVICE_UNKNOWN,0x600,METHOD_BUFFERED,FILE_ANY_ACCESS)
NTSTATUS IrpCall(PDEVICE_OBJECT DriverObject, PIRP prip) {
KdPrint(("進(jìn)入irp派遣例程"));
PIO_STACK_LOCATION IoStack;
IoStack = IoGetCurrentIrpStackLocation(prip);
switch (IoStack->MajorFunction)
{
case IRP_MJ_DEVICE_CONTROL:
{
UINT32 ControlCode = IoStack->Parameters.DeviceIoControl.IoControlCode;
if (ControlCode == IO_TEST) {
KdPrint(("進(jìn)入ControlCode->IO_TEST"));
break;
}
}
case IRP_MJ_CREATE: {
KdPrint(("3環(huán)調(diào)用CreateFile\n"));
break;
}
case IRP_MJ_CLOSE: {
KdPrint(("3環(huán)調(diào)用close"));
break;
}
}
prip->IoStatus.Status = STATUS_SUCCESS;
prip->IoStatus.Information = 4;
IofCompleteRequest(prip, IO_NO_INCREMENT);
KdPrint(("離開(kāi)pirpcall\n"));
return STATUS_SUCCESS;
}
/// <summary>
/// 驅(qū)動(dòng)停止函數(shù)
/// </summary>
/// <param name="DriverObject"></param>
/// <returns></returns>
VOID DriverUnload(PDRIVER_OBJECT DriverObject) {
if (DriverObject != NULL) {
DbgPrint("[%ws]Driver Upload,Driver Object Address:%p", __FUNCTIONW__, DriverObject);
}
DeleteDevice(DriverObject);
return;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
DbgPrint("[%ws] Hello Kernel World\n", __FUNCTIONW__);
if (RegistryPath != NULL) {
DbgPrint("[%ws]Driver REgistPath:%wZ\n", __FUNCTIONW__, RegistryPath);
}
if (DriverObject != NULL) {
DbgPrint("[%ws]Driver Object Address:%p\n", __FUNCTIONW__, RegistryPath);
DriverObject->DriverUnload = DriverUnload;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IrpCall;
DriverObject->MajorFunction[IRP_MJ_CREATE] = IrpCall;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = IrpCall;
CreateDevice(DriverObject);
}
return STATUS_SUCCESS;
}
應(yīng)用程序代碼 這里采用的是mfc
#include<winioctl.h>;
#define IO_TEST CTL_CODE(FILE_DEVICE_UNKNOWN,0x600,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define SymbolLink L"\\??\\Alex"
static HANDLE DeviceHandle = NULL;
void Cmfcr3Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知處理程序代碼
DeviceHandle = CreateFileW(SymbolLink,
GENERIC_ALL,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (DeviceHandle == INVALID_HANDLE_VALUE) {
::MessageBoxA(NULL, "打開(kāi)驅(qū)動(dòng)設(shè)備失敗!", "Error", MB_ICONHAND);
return;
}
}
void Cmfcr3Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知處理程序代碼
CloseHandle(DeviceHandle);
}
void Cmfcr3Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知處理程序代碼
DWORD ReturnSize;
DeviceIoControl(DeviceHandle,
IO_TEST,
NULL,
0,
NULL,
0,
&ReturnSize,
NULL);
}
需要注意mfc程序 要以管理員權(quán)限運(yùn)行