首页 > 解决方案 > 带有 CreateService 的 umdf2 驱动程序

问题描述

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

我以前用 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++windowswinapidriverumdf

解决方案


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

该系统找不到指定的文件。

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

其次,使用Dependency Walker等工具检查其依赖关系,将它们移动到 Current Directory 或 System Directory 以确保系统也可以找到依赖关系。

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


推荐阅读