【问题标题】:umdf2 driver with CreateService带有 CreateService 的 umdf2 驱动程序
【发布时间】:2019-01-10 02:26:08
【问题描述】:

我能否在 Windows 10 中使用 CreateServiceStartService API 启动 umdf2 驱动程序?我正在寻找我可以参考的任何运行示例。

我之前使用 WDM 驱动程序完成过,但目前我无法使用 umdf2 驱动程序完成。这是代码

WCHAR strPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, strPath);
std::wstring binaryPath(strPath);
binaryPath += L"\\" + pDeviceName + L".dll";

std::string logPath(binaryPath.begin(), binaryPath.end());
cout << "Load Path : " << logPath << endl;

SC_HANDLE hManager, hService;
hManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!hManager) {
    DWORD err = GetLastError();
    if (err == ERROR_ACCESS_DENIED) {
        cout << "OPenSCManager Access denied - run administration access" << endl;
    } else {
        cout << "OPenSCManager Error : " << err << endl;
    }
    return;
}

hService = CreateService(hManager, pDeviceName.c_str(), pDeviceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,
    SERVICE_ERROR_NORMAL, binaryPath.c_str(), NULL, NULL, NULL, NULL, NULL);
if (!hService) {
    hService = OpenService(hManager, pDeviceName.c_str(), SERVICE_ALL_ACCESS);
    if (!hService) {
        CloseServiceHandle(hManager);
        return;
    }
}

if (!StartService(hService, 0, NULL)) {
    DWORD err = GetLastError();
    cout << "StartService Error : " << err << endl;
    if (err == ERROR_SERVICE_ALREADY_RUNNING) {
        cout << "Already running" << endl;
    }
}

CloseServiceHandle(hManager);
CloseServiceHandle(hService);

pDeviceName 指的是驱动程序名称。代码执行失败,错误 2:

StartService Error : 2

我在Win7和Win10都测试过,结果是一样的。

【问题讨论】:

    标签: c++ windows winapi driver umdf


    【解决方案1】:

    错误代码告诉了我们大部分事情:

    系统找不到指定的文件。

    首先,检查 (pDeviceName).dll 是否位于当前目录。

    其次,使用Dependency Walker等工具检查其依赖关系,将它们移动到当前目录或系统目录,以确保系统也能找到依赖关系。

    然后尝试检查“regedit”,打开键 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\pDeviceName,或其他类似名称。检查键值“ImagePath”,路径是您第一次创建它。将 dll 移动到路径或将路径更改为 dll。

    【讨论】:

    • pDeviceName.dll 确实位于当前目录中。试图将其复制到 System32 和 SysWow64 中。两者都给出相同的结果。 FYI pDeviceName.dll 是一个非常简单的 umdf2 驱动程序,用于测试目的,没有任何依赖关系
    • @miradham 你如何查看目录?调试模式还是只运行测试程序?你把dll放在哪个目录下?调试文件夹还是 .sln 文件夹?在我的测试中,如果 dll 在调试文件夹中,则使用调试模式,当前目录在解决方案路径中并返回错误 2。
    • 我只是确保 dll 和我的测试程序 exe 在同一个目录中。该目录位于解决方案路径之外,我复制了 dll 和 exe。你能确认你可以无错误地启动它吗?
    • 尝试检查“regedit”,打开键 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\pDeviceName 或其他类似名称。检查键值“ImagePath”,路径是您第一次创建它。将 dll 移动到路径或将路径更改为 dll。
    • 我检查了ImagePath,它包含意外的问号,即\??\restofthepath,不确定它是否正常...但是删除问号会导致StartService 出现123 错误。目前没有成功
    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 2018-10-20
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多