Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a self-written driver

I wrote a driver in Visual Studio 2013. The building-Process was successful. Then I prepared a traget-computer and copied the driver-files to it. Then I installed the driver:

C:\Windows\system32>pnputil -a "E:\driverZeug\KmdfHelloWorldPackage\KmdfHelloWorld.inf"
Microsoft-PnP-Dienstprogramm

Verarbeitungsinf.:            KmdfHelloWorld.inf
Das Treiberpaket wurde erfolgreich hinzugefügt.
Veröffentlichter Name:            oem42.inf


Versuche gesamt:              1
Anzahl erfolgreicher Importe: 1

It seems like it was successful. I ran DebugView on the PC but now I don't know how to start the driver, so that I can see a debug-output. I have a DbgPrintEx()-Statement in my sourcecode.

Can someone tell me how to start this driver so that I can see the output.

This is the sourcecode of the driver:

#include <ntddk.h>
#include <wdf.h>
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD KmdfHelloWorldEvtDeviceAdd;

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

    DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n");
    KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n"));
    WDF_DRIVER_CONFIG_INIT(&config, KmdfHelloWorldEvtDeviceAdd);
    status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE);
    return status;
}

NTSTATUS KmdfHelloWorldEvtDeviceAdd(_In_ WDFDRIVER Driver, _Inout_ PWDFDEVICE_INIT DeviceInit)
{
    NTSTATUS status;
    WDFDEVICE hDevice;
    UNREFERENCED_PARAMETER(Driver);

    KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: KmdfHelloWorldEvtDeviceAdd\n"));
    status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &hDevice);
    return status;
}
like image 990
Andre Avatar asked May 30 '16 09:05

Andre


1 Answers

You need to make an EXE(testapp) that starts your driver if installation is already done. You can use below code in the application:

SC_HANDLE   schService;  
SC_HANDLE   schSCManager;

schSCManager = OpenSCManager(NULL,                   // local machine
                             NULL,                   // local database
                             SC_MANAGER_ALL_ACCESS   // access required
                             ); 

// Open the handle to the existing service.
schService = OpenService(SchSCManager,
                         DriverName, //name of the driver
                         SERVICE_ALL_ACCESS
                         );

StartService(schService,     // service identifier
                  0,              // number of arguments
                  NULL            // pointer to arguments
                  ));

You need add code according to your need. Try this.

For more info download the samples drivers and test apps provided by microsoft.

like image 64
Stubborn Avatar answered Nov 04 '22 16:11

Stubborn