Windows 7驱动程序服务启动

时间:2018-01-11 22:39:25

标签: c++ windows winapi kernel driver

我试图在Windows 7上运行驱动程序服务。首先,我的代码安装该服务,然后尝试运行它。我的问题是StartService返回错误代码2 - "系统找不到指定的文件。"。

驱动程序和代码是在Windows 10中开发的,在32位win7虚拟机上调试。但驱动程序是为Windows 7开发的。

Procmon在StartService中显示了相当奇怪的行为 - services.exe仅查询HKLM \ System \ CurrentControlSet \ services \ KMDFDriver2 \ ObjectName键,该键不存在。 .sys文件的路径位于HKLM \ System \ CurrentControlSet \ services \ KMDFDriver2 \ ImagePath中。我怀疑这可能是原因。

你能帮忙吗?

SC_HANDLE InstallDriver(LPCWSTR driverName, LPCWSTR binaryPath)
{
    SC_HANDLE scmDBHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (scmDBHandle == NULL)
    {
        DBG_TRACE("InstallDriver", "could not open handle to SCM database");
        PrintError();
        return NULL;
    }
    SC_HANDLE svcHandle = CreateService(
        scmDBHandle,
        driverName,
        driverName,
        SERVICE_ALL_ACCESS,
        SERVICE_KERNEL_DRIVER,
        SERVICE_DEMAND_START,
        SERVICE_ERROR_NORMAL,
        binaryPath,
        NULL, NULL, NULL, NULL, NULL
    );
    if (svcHandle == NULL)
    {
        DWORD lastError = GetLastError();
        if (lastError == ERROR_SERVICE_EXISTS)
        {
            DBG_TRACE("InstallDriver", "driver already installed");
            svcHandle = OpenService(scmDBHandle, driverName, SERVICE_ALL_ACCESS);
            if (svcHandle == NULL)
            {
                DBG_TRACE("InstallDriver", "could not open handle to driver");
                PrintError();
                CloseServiceHandle(scmDBHandle);
                return NULL;
            }
            CloseServiceHandle(scmDBHandle);
            return svcHandle;
        }
        DBG_TRACE("InstallDriver", "could not create handle to driver");
        PrintError();
        return NULL;
    }
    DBG_TRACE("InstallDriver", "Function returning succesfuly");
    CloseServiceHandle(scmDBHandle);
    return svcHandle;
}

BOOL LoadDriver(SC_HANDLE svcHandle)
{
    //DebugBreak();
    if (StartService(svcHandle, 0, NULL) == 0)
    {   
        if (GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
        {
            DBG_TRACE("loadDriver", "driver already running");
            return TRUE;
        }
        else
        {
            DBG_TRACE("loadDriver", "driver failed loading");
            PrintError();
            return FALSE;
        }
    }
    DBG_TRACE("loadDriver", "driver loaded succesfuly");
    return TRUE;
}


int main() {
    SC_HANDLE driver = InstallDriver(L"KMDFDriver2", L"C:\\Windows\\System32\\drivers\\KMDFDriver2.sys");
    LoadDriver(driver);
    CloseServiceHandle(driver);
}

1 个答案:

答案 0 :(得分:0)

如果您的驱动程序安装程序编译为x86并在x64 Windows上运行,则Windows文件系统和注册重定向可能存在此问题。简而言之 - %WINDIR%\SysWoW64\中的x86环境和%WINDIR%\System32中的本机x64环境有不同的目录。如果您的x86安装程序尝试打开C:\Windows\System32\drivers\KMDFDriver2.sys文件,则会获得C:\Windows\SysWOW64\drivers\KMDFDriver2.sys路径。为避免这种情况,请尝试使用Wow64DisableWow64FsRedirection()函数。

https://en.wikipedia.org/wiki/WoW64#Registry_and_file_system

https://technet.microsoft.com/ru-ru/aa384187

如果没有帮助,请检查您的UNICODE_UNICODE定义,因为您使用的DBG_TRACE使用LPCSTR,可能无法定义。 Unicode字符串将是有效的ANSI字符串,但它将被截断为第一个零字节 - 并且将只有1个字符。因此,Windows无法使用名为"K"

的二进制路径访问名为"C"的驱动程序