我写了一个简单的驱动程序,只打印“Hello World”到调试。 我使用Visual Studio 2012 RC和WDK 8来创建一个空的驱动程序项目并编写了以下代码:
#include <NTDDK.h>
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath)
{
pRegistryPath = pRegistryPath; //unused
DbgPrint("Hello World!");
pDriverObject->DriverUnload = NULL;
return STATUS_SUCCESS;
}
我把它编译成win7 x64。 我已经读过,为了安装和运行这个驱动程序,我需要写一个.inf文件,但我似乎无法做到这一点。 我从WDK 8中获取了一个示例.inf文件并将其更改为与我的.sys文件匹配,但它毁了我的虚拟框win7 x64 :-)。 所以我在VS2012中创建了一个过滤器驱动程序项目,取了.inf文件并将其更改为匹配我的.sys文件,当我安装它时没有任何开心。我试图运行它用
创建的新服务net start MyDriver
但是没有打印到调试中,也没有在计算机中看到MyDriver-&gt; Manage-&gt; Services。 我正在使用DebugView查看打印到调试的内容(http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx)。
当然,我想写一个acctualy做某事的驱动程序,但同时我只想知道如何运行它。
我从VS2012获取的.inf文件已更改为:
;;;
;;; MyDriver2
;;;
[Version]
Signature = "$Windows NT$"
; TODO - Change the Class and ClassGuid to match the Load Order Group value, see http://msdn.microsoft.com/en-us/windows/hardware/gg462963
; Class = "ActivityMonitor" ;This is determined by the work this filter driver does
; ClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} ;This value is determined by the Load Order Group value
Class = "ActivityMonitor"
ClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2}
Provider = %ManufacturerName%
DriverVer = 08/13/2012,1.0.0.0
;CatalogFile = MyDriver2.cat
[DestinationDirs]
DefaultDestDir = 12
MiniFilter.DriverFiles = 12 ;%windir%\system32\drivers
;;
;; Default install sections
;;
[DefaultInstall]
OptionDesc = %ServiceDescription%
CopyFiles = MiniFilter.DriverFiles
[DefaultInstall.Services]
AddService = %ServiceName%,,MiniFilter.Service
;;
;; Default uninstall sections
;;
[DefaultUninstall]
DelFiles = MiniFilter.DriverFiles
[DefaultUninstall.Services]
DelService = %ServiceName%,0x200 ;Ensure service is stopped before deleting
;
; Services Section
;
[MiniFilter.Service]
DisplayName = %ServiceName%
Description = %ServiceDescription%
ServiceBinary = %12%\%DriverName%.sys ;%windir%\system32\drivers\
Dependencies = "FltMgr"
ServiceType = 2 ;SERVICE_FILE_SYSTEM_DRIVER
StartType = 3 ;SERVICE_DEMAND_START
ErrorControl = 1 ;SERVICE_ERROR_NORMAL
; TODO - Change the Load Order Group value, see http://connect.microsoft.com/site221/content/content.aspx?ContentID=2512
; LoadOrderGroup = "FSFilter Activity Monitor"
LoadOrderGroup = "filter"
AddReg = MiniFilter.AddRegistry
;
; Registry Modifications
;
[MiniFilter.AddRegistry]
HKR,,"DebugFlags",0x00010001 ,0x0
HKR,,"SupportedFeatures",0x00010001,0x3
HKR,"Instances","DefaultInstance",0x00000000,%DefaultInstance%
HKR,"Instances\"%Instance1.Name%,"Altitude",0x00000000,%Instance1.Altitude%
HKR,"Instances\"%Instance1.Name%,"Flags",0x00010001,%Instance1.Flags%
;
; Copy Files
;
[MiniFilter.DriverFiles]
%DriverName%.sys
[SourceDisksFiles]
MyDriver2.sys = 1,,
[SourceDisksNames]
1 = %DiskId1%,,,
;;
;; String Section
;;
[Strings]
; TODO - Add your manufacturer
ManufacturerName = "Template"
ServiceDescription = "MyDriver2 Mini-Filter Driver"
ServiceName = "MyDriver2"
DriverName = "MyDriver2"
DiskId1 = "MyDriver2 Device Installation Disk"
;Instances specific information.
DefaultInstance = "MyDriver2 Instance"
Instance1.Name = "MyDriver2 Instance"
; TODO - Change the altitude value, see http://connect.microsoft.com/site221/content/content.aspx?ContentID=2512
;Instance1.Altitude = "370030"
Instance.Altitude = "370030"
Instance1.Flags = 0x0 ; Allow all attachments
当我尝试使用wdreg.exe安装并运行我的驱动程序时,它说“在INF文件中找不到制造商部分”。 (来自http://www.jungo.com/st/support/documentation/windriver/10.3.0/wdpci_manual.mhtml/dyn_windows.html) 我读了很多关于.inf文件(来自一些微软书和很多谷歌),我仍然不知道如何修复我的.inf文件。
如果有更简单的方法来运行我的驱动程序,我很乐意听到它。一旦我知道如何运行它,就可以轻松地调试真实产品。
谢谢!
编辑:我还在测试模式下使用Driver Signature Enforcement Overrider烧录.sys文件(http://www.ngohq.com/home.php?page=dseo)。
答案 0 :(得分:4)
原来Rohan是对的。 我无法找到在win7中查看调试打印的方法(Rohan链接用于vista),所以我刚刚创建了一个文件。
#include <wdm.h>
#include <Ntstrsafe.h>
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath)
{
UNICODE_STRING uniName;
OBJECT_ATTRIBUTES objAttr;
RtlInitUnicodeString(&uniName, L"\\SystemRoot\\example12345.txt");
InitializeObjectAttributes(&objAttr, &uniName,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL, NULL);
HANDLE handle;
NTSTATUS ntstatus;
IO_STATUS_BLOCK ioStatusBlock;
// Do not try to perform any file operations at higher IRQL levels.
// Instead, you may use a work item or a system worker thread to perform file operations.
if(KeGetCurrentIrql() != PASSIVE_LEVEL){
return STATUS_INVALID_DEVICE_STATE;
}
ntstatus = ZwCreateFile(&handle,
GENERIC_WRITE,
&objAttr, &ioStatusBlock, NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0);
CHAR buffer[30];
size_t cb;
if(NT_SUCCESS(ntstatus)) {
ntstatus = RtlStringCbPrintfA(buffer, sizeof(buffer), "This is a test\r\n");
if(NT_SUCCESS(ntstatus)) {
ntstatus = RtlStringCbLengthA(buffer, sizeof(buffer), &cb);
if(NT_SUCCESS(ntstatus)) {
ntstatus = ZwWriteFile(handle, NULL, NULL, NULL, &ioStatusBlock, buffer, (ULONG)cb, NULL, NULL);
}
}
ZwClose(handle);
}
pRegistryPath = pRegistryPath;
pDriverObject = pDriverObject;
return STATUS_SUCCESS;
}
我使用了同样的.inf我在我的问题中写道然后输入了cmd
net start MyDriver2
并且文件example12345.txt是在C:/ Windows中创建的。
答案 1 :(得分:2)
当您加载/启动驱动程序时,它已加载并在内核中运行,因此很可能是您的驱动程序已加载。但是您可能没有看到的DbgPrint
消息是因为在Vista之后,使用DbgPrint
记录的调试消息会被过滤掉而不会显示在输出中。
您可以参考此信息以启用DbgPrint
消息。 Getting DbgPrint Output To Appear In Vista and Later
另一种方法是将DbgPrintEx
与适当的组件和级别一起使用。