Roman
Roman

Reputation: 1447

Good C++ driver install and controlling code

I have a kernel driver (.sys), where i can find good source which i can use to install and control this driver?

Upvotes: 0

Views: 2608

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490118

Here's some code I wrote years ago to install an NDIS driver. It's been tested and used on XP, but I'm not sure about anything newer than that. Installing a different type of driver mostly requires changing the group and dependency.

#define Win32_LEAN_AND_MEAN
#include <windows.h>

void install_NDIS_driver(char const *path, char const *name ) {
// This uses the name as both the driver name and the display name.

    SC_HANDLE manager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
    SC_HANDLE service = CreateService(manager, 
        name,                       // driver name
        name,                       // driver display name
        GENERIC_EXECUTE,            // allow ourselves to start the service.
        SERVICE_KERNEL_DRIVER,      // type of driver.
        SERVICE_SYSTEM_START,       // starts after boot drivers.
        SERVICE_ERROR_NORMAL,       // log any problems, but don't crash if it can't be loaded.
        path,                       // path to binary file.
        "NDIS",                     // group to which this driver belongs.
        NULL,                       
        "NDIS\0",                   // driver we depend upon.
        NULL,                       // run from the default LocalSystem account.
        NULL);                      // don't need a password for LocalSystem .
    // The driver is now installed in the machine.  We'll try to start it dynamically.

    StartService(service, 0, NULL); // no arguments - drivers get their "stuff" from the registry.

    CloseServiceHandle(service);    // We're done with the service handle
    CloseServiceHandle(manager);    // and with the service manager.
}

Upvotes: 1

James Beilby
James Beilby

Reputation: 1471

You can use the Windows Service Controller to register and control a kernel-mode driver.

  1. Use "sc create" with type=kernel and binPath pointing to your .sys file to create the service
  2. Use "sc start" and "sc stop" to control the driver

Upvotes: 0

Related Questions