我可以在 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都测试过,结果是一样的。