6

For my purposes, I need to write a kernel mode driver for Windows. Currently I am attempting to make it work under Windows 7 x64.

I created a simple project in Visual Studio 2012 with default code for a KMDF driver. I compiled the code with test-signing on. The driver was compiled and signed. I also have Test-Signing ON enabled as clearly displayed on the bottom left corner of my Desktop.

Upon trying to start the driver as a service, I always get an Error Code 6: Invalid Handle error.(I have since simplified the code to just try and start it but still did not work;default code did not work either)

Basically, I am having the same problem as the question asked here

https://stackoverflow.com/questions/12080157/startservice-error-6

unfortunately he was never answered. I tried the provided solution, but it didn't help either.

My code that tries to start the driver is

int _cdecl main(void)
{
    HANDLE hSCManager;
    HANDLE hService;
    SERVICE_STATUS ss;

    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);

    printf("Load Driver\n");

    if(hSCManager)
    {
        printf("Create Service\n");

        hService = CreateService(hSCManager, "Example", 
                             "Example Driver", 
                              SERVICE_ALL_ACCESS | SERVICE_START | DELETE | SERVICE_STOP , 
                              SERVICE_KERNEL_DRIVER,
                              SERVICE_DEMAND_START, 
                              SERVICE_ERROR_IGNORE, 
                              "\\path\\to\\driver\\KMDFDriver1.sys", 
                              NULL, NULL, NULL, NULL, NULL);

        if(!hService)
        {
            hService = OpenService(hSCManager, "Example", 
                   SERVICE_ALL_ACCESS | SERVICE_START | DELETE | SERVICE_STOP);

            if(!hService)
            {
                // If initial startup of the driver failed, it will fail here.
                process_error();
                return 0;
            }
        }

        if(hService)
        {
            printf("Start Service\n");

            if(StartService(hService, 0, NULL) == 0)
            {
              // Start service ALWAYS returns 0. Only when executed for the first time. Next time it fails on OpenService.
                process_error();
                printf("Did not start!\n");
            }
            printf("Press Enter to close service\r\n");
            getchar();
            ControlService(hService, SERVICE_CONTROL_STOP, &ss);
            DeleteService(hService);
            CloseServiceHandle(hService);   
        }

        CloseServiceHandle(hSCManager);
    }

    return 0;
}

And this is the driver code

DRIVER_INITIALIZE DriverEntry;
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#endif

NTSTATUS
DriverEntry(
    _In_ PDRIVER_OBJECT  DriverObject,
    _In_ PUNICODE_STRING RegistryPath
    )
{

    WDF_DRIVER_CONFIG config;
    NTSTATUS status;

    DbgPrint("Hello World!\n");
    WDF_DRIVER_CONFIG_INIT(&config,
                       NULL
                       );

    config.DriverInitFlags = WdfDriverInitNonPnpDriver;

    status = WdfDriverCreate(DriverObject,
                         RegistryPath,
                         WDF_NO_OBJECT_ATTRIBUTES,
                         &config,
                         WDF_NO_HANDLE
                         );

    if (!NT_SUCCESS(status)) {
        KdPrint( ("WdfDriverCreate failed with "
              "status 0x%x\n", status));
    }

    return status;
}

The function process_error() is a wrapper around GetLastError() which in addition to providing the numeric value, displays a text version of the error code. I have exhausted all options provided to me to solve this issue. A google search revealed only one occurrence of this problem, and the question was asked here.

What could the problem be?

Extra notes: The driver was compiled with Visual Studio 2012 Ultimate, while my startup code was compiled with MinGW-W64(using GCC). But the startup code shouldn't matter as much as the driver.

Extra notes 2: After wondering for a long time what could be wrong I started thinking if it's the test-sign certificate, because I tried driver source code provided from MSDN, and upon successful compilation, I still got ERROR_INVALID_HANDLE(Error Code 6) when trying to start it. I have still not found a solution.

Community
  • 1
  • 1
farmdve
  • 786
  • 3
  • 13
  • 26
  • I've got into exactly the same situation with WFP example ddproxy. It returned with error 6 on Debug version of the driver on both Win7 32 and 64 bit. Driver was test signed and test signature was allowed. I've tried to debug, but the error occurs before breakpoint in DriverEntry is reached. Temporarily solved by testing on Win8 where it seems to work. – user2225906 Mar 29 '13 at 22:33
  • I'm having the same problem except I am using "sc start ". Did you ever see the "Hello World!" print out? Win7 x64, VS2013, WDK 8.1 – philselmer May 16 '14 at 20:10

5 Answers5

10

I tracked this down to the project settings of the driver. The KMDF versions were missing from the project.

Adjust the following (under Driver Model Settings):
  -  KMDF Version Major = 1
  -  KMDF Version Minor = 9

Hit OK, recompile, and reinstall. Worked for me!

philselmer
  • 751
  • 4
  • 22
  • Well, I scrapped KMDF a long time ago, used WDM instead, but thanks. Hope this helps other people. – farmdve May 18 '14 at 18:51
  • Should be marked as answer, setting KMDF version to 1.9 helped in my case. The issue is definitely in driver itself because the same error arises when starting driver service via `sc start`. – n0ne Mar 04 '20 at 05:13
0

A few thoughts:

You're using HANDLE hSCManager && HANDLE hService, they should be declared as SC_HANDLE

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682450(v=vs.85).aspx

"lpBinaryPathName [in, optional] The fully qualified path to the service binary file. If the path contains a space, it must be quoted so that it is correctly interpreted. For example, "d:\my share\myservice.exe" should be specified as "\"d:\my share\myservice.exe\"".

Try using the full path to the driver

Polishh
  • 21
  • 1
  • 1
  • 5
  • 1
    I traced the problem to the KMDF framework. Using a WDM driver worked with my startup code. Whatever the real issue was, I know it's in the Kernel-mode driver framework. – farmdve Nov 13 '12 at 21:10
0

I had the same problem with starting my kernel driver:


startservice failed 6:

the handle is invalid

Turned out that the "classID GUID" of the driver was the same as that of an other one (found out through device manager, looking in events showed different driver names).

Used an online generator to make a new GUID and replaced the one that's in the .inf file of the project (in VS, not any texteditor or some). After a rebuild and deployment on target machine everything worked fine.

Hope this helps...

Batzie
  • 1
0

Run visual studio with admin privilege

-1

Your call to OpenSCManager() is only asking for SC_MANAGER_CREATE_SERVICE permission by itself, which is not enough for OpenService() or StartService() to succeed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I added SC_MANAGER_ALL_ACCESS and GENERIC_ALL, still absolutely same problem. I have even set the executable to be run as Administrator. – farmdve Nov 06 '12 at 19:35