The development of wdk drivers under VS12 is cumbersome and requires many options to be set. This article passed the test under win8 + visual studio2012 + wdk7600.
1. Project property configuration
conventional
Target file extension: .sys // required
VC++ directory
Include directory C:\WinDDK\7600.16385.1\inc\wdf\kmdf\1.9 inc\ddk inc\crt inc\api
Library directory C:\WinDDK\7600.16385.1\lib\win7\i386 C:\WinDDK\7600.16385.1\lib\wdf\kmdf\i386\1.9
C/C++
optimization
optimize(disable/Od) // optional
preprocessor
WIN32=100;_X86_=1;DBG=1 // Required
code generation
Runtime library: multithreaded debugging (/MTd) or multithreaded (/MT) // recommended
buffer security check: no // optional (to avoid error LNK2001: cannot resolve external symbol __security_cookie)
advanced
Calling convention __stdcall(/Gz) //Required
Linker
conventional
Enable incremental linking: no (/INCREMENTAL:NO) //recommended
enter
ntoskrnl.lib // NT-style driver
Hal.lib // HalXXX functions
wdm.lib // WDM-style driver
wdmsec.lib
wmilib.lib // WmiXXX functions
ndis.lib // NdisXXX functions
Ignore all default libraries: yes (/NODEFAULTLIB) //required
manifest file:
Generate manifest file No // Required, note that this option must be selected under vs12 to compile successfully
Enable User Account Control (UAC) No // Required
Otherwise there will be fatal error LNK1295: '/MANIFESTUAC' is incompatible with '/DRIVER' specification; do not use '/MANIFESTUAC' when linking
system
Subsystem: Console(/SUBSYSTEM:NAIVE) //Required
stack reserve size: 4194304 // optional
stack commit size: 4096 // optional
Driver: Driver(/DRIVER) //Required
advanced
Entry point: DriverEntry //Required
Target computer: MachineX86 (/MACHINE:X86) //optional
Random base address: clear // Required, otherwise fatal error will occur LNK1295: "/DYNAMICBASE" is incompatible with "/DRIVER" specification; do not use "/DYNAMICBASE" when linking
Data Execution Prevention (DEP): Clear //Required, otherwise fatal error LNK1295: "/NXCOMPAT:NO" is not compatible with "/DRIVER" specification; "/NXCOMPAT:NO" is not used when linking
2. Coding
#include <ntddk.h> VOID HelloDDKUnload(IN PDRIVER_OBJECT pDriverObject) { KdPrint(("Enter DriverUnload\n")); KdPrint(("Leave DriverUnload\n")); } NTSTATUS DriverEntry ( IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath ) { NTSTATUS status; status = STATUS_SUCCESS; KdPrint(("Enter DriverEntry\n")); pDriverObject->DriverUnload = HelloDDKUnload; KdPrint(("DriverEntry end\n")); return status; }
3. Command line compilation
The VS compilation environment setting is cumbersome, and you can directly write a makefile to compile it through the command line.
Prepare two files, makefile and sources, in the current directory.
#Can be copied directly from the wdk installation directory, do not modify !INCLUDE $(NTMAKEENV)\makefile.def !if $(FREEBUILD) MSC_WARNING_LEVEL=/W1 !else MSC_WARNING_LEVEL=/W3 !endif
TARGETNAME = hello TARGETTYPE = DRIVER TARGETPATH = x32 INCLUDES = .\ TARGETLIBS = $(DDK_LIB_PATH)\tdi.lib SOURCES = hello.c
Bring up the build command line from the start menu, %StartMenu%\Windows Driver Kits\WDK 7600.16385.1\Build Environments\Windows 7
WDK provides support for chips such as intel and AMD, and can compile 32-bit or 64-bit drivers. checked is the compilation environment for the debug version, and free is the compilation environment for the release version.
Start the 32-bit compilation environment and run the build -Cz or bld command to generate hello.sys in the x32 directory.