Creating Your First KMDF Driver

This guide will walk you through the process of creating a simple Kernel-Mode Driver Framework (KMDF) driver. KMDF simplifies driver development by providing a framework that handles many common driver tasks, allowing you to focus on the unique logic of your driver.

Prerequisites

Note: It is highly recommended to use the latest stable versions of the Windows SDK and WDK for optimal compatibility and access to the latest features.

Steps to Create Your First KMDF Driver

1. Create a New Driver Project in Visual Studio

  1. Open Visual Studio.
  2. Go to File > New > Project....
  3. In the "Create a new project" dialog, search for "Kernel Mode Driver".
  4. Select the "Kernel Mode Driver (KMDF)" template and click Next.
  5. Enter a Project name (e.g., `MyFirstKmdfDriver`) and choose a Location.
  6. Click Create.

2. Understanding the Project Structure

Visual Studio will create a basic project with several files:

3. Implementing the Driver Entry Point

The DriverEntry function is the first function that the operating system calls when your driver is loaded. In a KMDF driver, this function typically initializes the framework and creates a framework driver object.

Open Driver.c. You will find a function that looks like this:


NTSTATUS
DriverEntry(
    _In_ PDRIVER_OBJECT  DriverObject,
    _In_ PUNICODE_STRING RegistryPath
    )
{
    NTSTATUS                        status;
    WDF_DRIVER_CONFIG               config;
    WDF_OBJECT_ATTRIBUTES           attributes;

    //
    // Initialize the Framework Driver Object attributes.
    //
    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);

    //
    // Framework does not use the DriverObject parameter.
    //
    UNREFERENCED_PARAMETER(DriverObject);

    //
    // Register the driver with the framework.
    //
    WDF_DRIVER_CONFIG_INIT(
        &config,
        NULL // No callback for AddDevice in this basic example
    );

    //
    // Create a framework driver object.
    //
    status = WdfDriverCreate(
        DriverObject,
        RegistryPath,
        &attributes,
        &config,
        WDF_NO_HANDLE
    );

    if (NT_SUCCESS(status)) {
        // TODO: Add code to create a device if needed
        // For a basic driver, it might not create a device on load.
    }

    return status;
}
            
Important: In a real-world scenario, you would typically implement an EvtDriverDeviceAdd callback within WDF_DRIVER_CONFIG_INIT to handle device creation. For this minimal example, we'll omit it.

4. Building and Signing the Driver

To build the driver:

  1. Right-click on the project in Solution Explorer and select Properties.
  2. Navigate to Configuration Properties > Driver Settings > General.
  3. Ensure the Target OS is set to "Desktop".
  4. In the Configuration Manager, select "Win7 Debug" or a similar target for building.
  5. Build the solution (Build > Build Solution).

Kernel-mode drivers require signing to be loaded on most modern Windows versions. For development purposes, you can use a test certificate.

Warning: Do not deploy unsigned drivers to production systems. Always use properly signed drivers.

5. Loading and Testing the Driver

Loading a kernel-mode driver typically involves using tools like:

A simple way to load is using sc.exe:


sc create MyFirstKmdfDriver type= kernel binPath= "C:\Path\To\Your\Driver.sys" displayname= "My First KMDF Driver"
sc start MyFirstKmdfDriver
            

Remember to replace C:\Path\To\Your\Driver.sys with the actual path to your built driver file (usually found in the project's output directory, e.g., `x64/Debug`).

Next Steps