使用CreateFile()API获取驱动程序句柄 [英] Get the driver handle by using CreateFile() API

查看:612
本文介绍了使用CreateFile()API获取驱动程序句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为WFP标注创建了驱动程序。我想从这个驱动程序向用户模式应用程序发送一些数据。


我该怎么做?


我遇到了API DeviceIoControl()调用驱动程序的回调函数。但为此我需要驱动程序的设备句柄,可以使用CreateFile()创建。但是我不知道在CreateFile()函数中我应该为文件名赋予什么值?


我使用命令"net start WFPSamplerCallouts"启动驱动程序。所以我想在CreatFile()中给出相同的名称,它给出错误为"系统找不到指定的文件。"。


那么我应该用什么名字来获取驱动程序句柄?

解决方案

您是否创建了符号链接?

 NTSTATUS status = STATUS_SUCCESS; 
UNICODE_STRING dosUnicodeString;
UNICODE_STRING ntUnicodeString;

RtlZeroMemory(& dosUnicodeString,
sizeof(UNICODE_STRING));

RtlZeroMemory(& ntUnicodeString,
sizeof(UNICODE_STRING));

pGlobalDeviceObject = 0;

RtlInitUnicodeString(& ntUnicodeString,
L" \\Device \WFPSamplerCallouts");

RtlInitUnicodeString(& dosUnicodeString,
L" \\DosDevice \\WFPSamplerCallouts");

status = IoCreateDevice(pDriverObject,
0,
& ntUnicodeString,
FILE_DEVICE_NETWORK,
0,
FALSE,
&安培; pGlobalDeviceObject);
DRIVER_BAIL_ON_FAILURE(状态);

DRIVER_BAIL_ON_NULL_POINTER(pGlobalDeviceObject);

pGlobalDeviceObject-> Flags | = DO_BUFFERED_IO;

//使用此驱动程序的入口点初始化驱动程序对象。
pDriverObject-> MajorFunction [IRP_MJ_CREATE] = DriverDispatch;
pDriverObject-> MajorFunction [IRP_MJ_CLOSE] = DriverDispatch;
pDriverObject-> MajorFunction [IRP_MJ_CLEANUP] = DriverDispatch;
pDriverObject-> MajorFunction [IRP_MJ_DEVICE_CONTROL] = DriverDispatch;
pDriverObject-> DriverUnload = DriverUnload;

//在Dos设备名称和Nt
之间创建符号链接//测试协议驱动程序的设备名称。
status = IoCreateSymbolicLink(& dosUnicodeString,
& ntUnicodeString);
DRIVER_BAIL_ON_FAILURE(状态);

然后使用设备名称:


< pre class ="prettyprint"> WCHAR pDeviceName [40];
wcscpy_s(pDeviceName,
40,
L" \\\\\\\ quot;);

wcscat_s(pDeviceName,
40,
pDevice);

* pDriver = CreateFileW(pDeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL,/// lpSecurityAttirbutes
OPEN_EXISTING ,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_OVERLAPPED,
NULL); /// lpTemplateFile

希望这会有所帮助,



I have driver created for WFP callout. I want to send some data from this driver to user mode application.

How can I do this?

I came across API DeviceIoControl() which calls the driver's callback function. But for this I need the device handle for the driver which can be created using CreateFile(). But I don't know what value should I give for file name in CreateFile() function??

I start the driver with the command "net start WFPSamplerCallouts". So I thought of giving same name in CreatFile() which is giving error as "The system cannot find the file specified.".

So what name should I use to get the driver handle?

解决方案

Did you create a symbolic link?

   NTSTATUS       status = STATUS_SUCCESS; 
   UNICODE_STRING dosUnicodeString;  
   UNICODE_STRING ntUnicodeString;

   RtlZeroMemory(&dosUnicodeString,
                 sizeof(UNICODE_STRING));

   RtlZeroMemory(&ntUnicodeString,
                 sizeof(UNICODE_STRING));

   pGlobalDeviceObject = 0;

   RtlInitUnicodeString(&ntUnicodeString,
                        L"\\Device\WFPSamplerCallouts");

   RtlInitUnicodeString(&dosUnicodeString,
                        L"\\DosDevice\\WFPSamplerCallouts");

   status = IoCreateDevice(pDriverObject,
                           0,
                           &ntUnicodeString,
                           FILE_DEVICE_NETWORK,
                           0,
                           FALSE,
                           &pGlobalDeviceObject);
   DRIVER_BAIL_ON_FAILURE(status);

   DRIVER_BAIL_ON_NULL_POINTER(pGlobalDeviceObject);

   pGlobalDeviceObject->Flags |= DO_BUFFERED_IO;

   // Initialize the driver object with this driver's entry points.
   pDriverObject->MajorFunction[IRP_MJ_CREATE]         = DriverDispatch;
   pDriverObject->MajorFunction[IRP_MJ_CLOSE]          = DriverDispatch;
   pDriverObject->MajorFunction[IRP_MJ_CLEANUP]        = DriverDispatch;
   pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverDispatch;
   pDriverObject->DriverUnload                         = DriverUnload;

   // Create symbolic link between the Dos Device name and Nt
   // Device name for the test protocol driver.
   status = IoCreateSymbolicLink(&dosUnicodeString,
                                 &ntUnicodeString);
   DRIVER_BAIL_ON_FAILURE(status);

You then use the device name:

   WCHAR    pDeviceName[40];  
   wcscpy_s(pDeviceName,
            40,
            L"\\\\.\\");  

   wcscat_s(pDeviceName,
            40,
            pDevice);  
  
   *pDriver = CreateFileW(pDeviceName,   
                          GENERIC_READ | GENERIC_WRITE,  
                          FILE_SHARE_READ | 
                          FILE_SHARE_WRITE,  
                          NULL, /// lpSecurityAttirbutes
                          OPEN_EXISTING,  
                          FILE_ATTRIBUTE_NORMAL | 
                          FILE_FLAG_OVERLAPPED,
                          NULL); /// lpTemplateFile 

Hope this helps,


这篇关于使用CreateFile()API获取驱动程序句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆