c++ - 带有 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都测试过,结果是一样的。
解决方案
错误代码告诉了我们大部分事情:
该系统找不到指定的文件。
首先,检查 (pDeviceName).dll 是否位于当前目录中。
其次,使用Dependency Walker等工具检查其依赖关系,将它们移动到 Current Directory 或 System Directory 以确保系统也可以找到依赖关系。
然后尝试检查“regedit”,打开键 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\pDeviceName,或其他类似名称。检查键值“ImagePath”,路径是您第一次创建它。将 dll 移动到路径或将路径更改为 dll。
推荐阅读
- angular - 在我的以下代码中重定向到资产页面时面临问题
- html - 如何对齐表头列和表体列的宽度?
- html - Bootstrap : Unable to center contents of entire row
- android - PreferenceManager getDefaultSharedPreferences deprecated in Android Q
- swift - SwiftUI 中的内容是什么?
- c# - MVC 5 - How to update relational table using EF code first
- vscode-remote - 从用户A切换到用户B
- python - 如何编写一个或条件来以 Python 方式检查子字符串?
- ant - 如何使用ant中的scp任务一次将不同目录中的多个文件从本地机器复制到远程机器?
- javascript - 在 SCSS 中动态堆叠图像