tbKMode Package

Author: fafalone. Version: 0.3.0.5. On GitHub: fafalone/tbKMode.

Declarations for writing a Windows kernel-mode driver in twinBASIC.

The tbKMode contributed package declares types, routines, constants and macros of the Windows Driver Kit (WDK), from its headers wdm.h, ntddk.h, ntifs.h, fltkernel.h, ntdddisk.h, mountmgr.h and ntimage.h, for a driver built as a .sys file. It covers a part of the WDK: its author describes it as a start, to be extended by each project that uses it.

Building a driver

A driver is a project whose Build Type is Standard EXE, with these project settings:

  • Native Subsystem: Yes, which marks the built file as a native-subsystem image, as a driver is.
  • Override Entry Point: DriverEntry, the name of the driver’s entry point. It is a Public Function of a module that receives the driver’s DRIVER_OBJECT and the path of its registry key, as in the example of modWDM.
  • Runtime Binding Of DLL Declares: No, which puts the routines that the driver calls into the import table of the built file, for the kernel to resolve when it loads the driver. Only the routines that the driver calls are imported.
  • Strip PE File Relocation Symbols: NO, which keeps the relocation data in the built file. Its default, AUTO, leaves it out of an EXE. With the relocations kept, Enable Address Space Layout Randomization (ASLR), Yes by default, also marks the file as one that can be loaded at any address.

The Build Output Path ends in .sys in place of .${FileExtension}. A driver is built for the architecture of the Windows it runs on: the kernel runs no 32-bit code on 64-bit Windows. Project Types says more about kernel-mode projects.

Names shared with VBA

modWDM has procedures named LCase, UCase, InStr and MsgBox, as VBA’s functions are. They take the addresses of null-terminated strings, since a driver works on the kernel’s strings, and the package’s changelog calls them overrides of VBA’s.

Modules

  • modWDM – the Windows Driver Model: driver and device objects, IRPs, memory, synchronization, the registry, processes and threads, strings and callbacks; the types the other modules share
  • modFltMgr – the Filter Manager, for a file-system minifilter driver
  • modSysInfo – the information classes and structures of the Zw query and set routines
  • modDisk – disk geometry, partitions and the structures of a driver that presents a disk
  • modImage – the headers, sections and tables of an executable image in the PE format
  • modKmOpaque – offsets of fields in opaque kernel structures

See Also

Test cases

The notes and warnings of this section are checked by the test cases in tbKMode-tests.twinproj, a twinBASIC project. It uses tbKMode as a linked package: put the package’s .twinpack in %APPDATA%\twinBASIC\packages, then run the project, and each case prints whether it passes. The tbKMode Verifier, a Node.js program, checks the package’s Declares against what the Windows kernel exports, and a built driver’s imports.