Why am I getting Error Code 6 on StartService?

…衆ロ難τιáo~ 提交于 2019-12-10 14:54:05

问题


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.


回答1:


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!




回答2:


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




回答3:


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...




回答4:


Run visual studio with admin privilege




回答5:


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.



来源:https://stackoverflow.com/questions/13254487/why-am-i-getting-error-code-6-on-startservice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!