为什么我在StartService上收到错误代码6 ?

时间:2022-09-04 08:02:03

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.

出于我的目的,我需要为Windows编写一个内核模式驱动程序。目前我正在尝试在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.

我在Visual Studio 2012中创建了一个简单的项目,其中包含KMDF驱动程序的默认代码。我编译了带有测试签名的代码。该驱动程序被编译并签名。我还启用了测试签名,如在桌面左下角所显示的那样。

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)

在尝试将驱动程序作为服务启动时,我总是得到一个错误代码6:无效句柄错误。(从那以后,我简化了代码,只是尝试并启动它,但仍然不起作用;默认代码也不起作用)

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

基本上,我的问题和这里的问题是一样的

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

https://*.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.

函数process_error()是GetLastError()的包装器,它除了提供数字值外,还显示了错误代码的文本版本。为了解决这个问题,我已经用尽了所有的办法。谷歌搜索只显示了这个问题的一次发生,这里提出了这个问题。

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.

附加说明:驱动程序是用Visual Studio 2012 Ultimate编译的,而我的启动代码是用MinGW-W64(使用GCC)编译的。但是启动代码不应该像驱动程序那样重要。

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.

额外提示2:在思考了很长一段时间之后,我开始思考它是否是测试符号证书,因为我尝试了MSDN提供的驱动程序源代码,在成功编译之后,当我尝试启动它时,仍然得到ERROR_INVALID_HANDLE(错误代码6)。我还没有找到解决办法。

4 个解决方案

#1


8  

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!

我追踪到驱动程序的项目设置。项目中没有KMDF版本。调整下面(在驱动程序模型设置下):- KMDF版本Major = 1 - KMDF版本小= 9点击OK,重新编译,重新安装。为我工作!

#2


0  

A few thoughts:

几个想法:

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

您正在使用句柄hSCManager &句柄hService,它们应该声明为SC_HANDLE

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

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

“lpBinaryPathName [in, optional])服务二进制文件的完全限定路径。如果路径包含空格,则必须引用它,以便正确地解释它。例如,“d:\ \ myservice。应该将exe指定为“\”d:\我的共享服务。

Try using the full path to the driver

尝试使用驱动程序的完整路径

#3


0  

I had the same problem with starting my kernel driver:

我在启动内核驱动程序时遇到了同样的问题:


startservice failed 6:

startservice失败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).

结果发现,驱动程序的“classID GUID”与另一个驱动程序的“id”是相同的(通过设备管理器发现,查找事件显示不同的驱动程序名称)。

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.

使用在线生成器生成一个新的GUID并替换项目中的.inf文件中的一个(在VS中,而不是任何texteditor或一些)。在目标机器上重新构建和部署之后,一切工作正常。

Hope this helps...

希望这有助于……

#4


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

您对OpenSCManager()的调用只要求SC_MANAGER_CREATE_SERVICE权限,这对于OpenService()或StartService()来说是不够的。

#1


8  

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!

我追踪到驱动程序的项目设置。项目中没有KMDF版本。调整下面(在驱动程序模型设置下):- KMDF版本Major = 1 - KMDF版本小= 9点击OK,重新编译,重新安装。为我工作!

#2


0  

A few thoughts:

几个想法:

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

您正在使用句柄hSCManager &句柄hService,它们应该声明为SC_HANDLE

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

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

“lpBinaryPathName [in, optional])服务二进制文件的完全限定路径。如果路径包含空格,则必须引用它,以便正确地解释它。例如,“d:\ \ myservice。应该将exe指定为“\”d:\我的共享服务。

Try using the full path to the driver

尝试使用驱动程序的完整路径

#3


0  

I had the same problem with starting my kernel driver:

我在启动内核驱动程序时遇到了同样的问题:


startservice failed 6:

startservice失败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).

结果发现,驱动程序的“classID GUID”与另一个驱动程序的“id”是相同的(通过设备管理器发现,查找事件显示不同的驱动程序名称)。

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.

使用在线生成器生成一个新的GUID并替换项目中的.inf文件中的一个(在VS中,而不是任何texteditor或一些)。在目标机器上重新构建和部署之后,一切工作正常。

Hope this helps...

希望这有助于……

#4


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

您对OpenSCManager()的调用只要求SC_MANAGER_CREATE_SERVICE权限,这对于OpenService()或StartService()来说是不够的。