Kernel Mode Driver Framework >> C/C++ (PNP)

Hello, world!


參考資訊:
1. Source Code

main.c

#include <ntddk.h>
#include <wdf.h>

NTSTATUS AddDevice(WDFDRIVER pOurWDF, PWDFDEVICE_INIT pDeviceInit)
{
  WDFDEVICE device;
  UNICODE_STRING usDeviceName;
 
  DbgPrint("Hello, world!");
  RtlInitUnicodeString(&usDeviceName, L"\\Device\\MyDriver");
  WdfDeviceInitAssignName(pDeviceInit, &usDeviceName);
  return WdfDeviceCreate(&pDeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &device);
}

NTSTATUS DriverEntry(PDRIVER_OBJECT pOurDriver, PUNICODE_STRING pRegistry)
{
  WDF_DRIVER_CONFIG config;

  WDF_DRIVER_CONFIG_INIT(&config, AddDevice);
  return WdfDriverCreate(pOurDriver, pRegistry, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE);
}

DriverEntry()產生WDF Driver Object並且註冊AddDevice Callback
AddDevice()產生WDF Device Object並且輸出Debug字串

sources

TARGETNAME=main
TARGETPATH=obj
TARGETTYPE=DRIVER
KMDF_VERSION_MAJOR=1
INCLUDES=$(INCLUDES);..\inc
SOURCES=main.c

sources會指定編譯輸出的檔名以及需要編譯的參數、檔案。

makefile

!INCLUDE $(NTMAKEENV)\makefile.def

makefile內容是固定的。

main.inf

[Version]
Signature=$CHICAGO$
Class=Unknown
Provider=%MFGNAME%
DriverVer=8/21/2019,1.0.0.0
 
[Manufacturer]
%MFGNAME%=DeviceList
 
[DeviceList]
%DESCRIPTION%=DriverInstall, *MyDriver
 
[DestinationDirs]
DefaultDestDir=10,System32\Drivers
 
[SourceDisksFiles]
main.sys=1,,,
 
[SourceDisksNames]
1=%INSTDISK%,,,
 
[DriverInstall.NT]
CopyFiles=DriverCopyFiles
 
[DriverCopyFiles]
main.sys,,,2
 
[DriverInstall.NT.Services]
AddService=FILEIO,2,DriverService
 
[DriverService]
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%10%\system32\drivers\main.sys
 
[DriverInstall.NT.HW]
AddReg=DriverHwAddReg
 
[DriverHwAddReg]
HKR,,SampleInfo,,""
 
[DriverInstall]
AddReg=DriverAddReg
CopyFiles=DriverCopyFiles
 
[DriverAddReg]
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,main.sys
 
[DriverInstall.HW]
AddReg=DriverHwAddReg
 
[Strings]
MFGNAME="MyDriver"
INSTDISK="MyDriver Disc"
DESCRIPTION="MyDriver"

INF內容也是很制式的,我們先知道如何使用就可以,司徒之後會教導大家如何寫INF檔案

編譯程式:
1. 開始 > 程式集 > Windows Driver Kits > WDK xxxx.xxxx > Build Environments > Windows 7 > x86 Checked Build Environment
2. 開啟Command Line之後,請使用cd命令移到你的目前資料夾
3. 接著輸入編譯指令:build -cefw

安裝程式:
在開始安裝驅動程式之前,我們需要先下載除錯工具,讓驅動程式的Debug訊息可以顯示在除錯工具上面,目前在Kernel Mode以及User Mode上,最佳的Debug輸出訊息工具是DbgView,該公司目前已經被Microsoft併購,所以可以從Microsoft網站下載,下載完後執行DbgView並將Capture > Capture KernelEnable Verbose Kernel Output選項打勾,接著重啟DbgView


對於驅動程式的安裝工具,司徒目前先使用NuMega公司製作的安裝工具EzDriverInstaller,請將main.sys和main.inf放在同一個目錄並執行EzDriverInstaller,選擇File > Open...(開啟main.inf檔案),接著按Add New Device就可以在DbgView上面看到輸出訊息



Device Manager


Device


返回上一頁