0

Can I launch umdf2 driver using CreateService and StartService APIs in Windows 10? I am looking for any running sample that I could refer.

I have done it with WDM driver previously, but currently I failed to do it with umdf2 driver. Here is the code

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 refers to the driver name. Code execution fails with error 2:

StartService Error : 2

I tested this in both Win7 and Win10 and the result is same.

miradham
  • 2,285
  • 16
  • 26

1 Answers1

0

The error code has told us the most of things:

The system cannot find the file specified.

First, check the (pDeviceName).dll is located in Current Directory.

Second, check its dependencies with tools like Dependency Walker, move them to Current Directory or System Directory to make sure the system can also find the dependencies.

Then try to check the "regedit", Open the key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\pDeviceName, or other similar names. Check the Key value "ImagePath", the path was the first time you have create it. Move dll to the Path or Change the Path to dll.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30
  • pDeviceName.dll is indeed located in Current Directory. Tried to copy it into System32 and SysWow64. Both gives same result. FYI pDeviceName.dll is very simple umdf2 driver built for testing purpose which does not have any dependency – miradham Jan 10 '19 at 06:23
  • @miradham how do you check the Directory? debug mode or just run test program? which Directory have you put the dll in? Debug folder or the .sln folder? In my test, if the dll is in debug folder ,then use debug mode, Current Directory was in solution Path and return error 2. – Drake Wu Jan 10 '19 at 07:14
  • I just made sure that the dll and my test program exe is in the same directory. The directory is outside of solution path and I copied both dll and exe. Can you confirm that you could launch it with no errors? – miradham Jan 10 '19 at 07:36
  • Try to check the "regedit", Open the key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\pDeviceName, or other similar names. Check the Key value "ImagePath", the path was the first time you have create it. Move dll to the Path or Change the Path to dll. – Drake Wu Jan 10 '19 at 08:44
  • I have checked `ImagePath` and it contains unexpected question mark i.e. `\??\restofthepath`, not sure if it is normal or not... but removing questions marks result in getting 123 error from `StartService`. No success so far – miradham Jan 10 '19 at 08:57
  • It is normal, do not removing it. – Drake Wu Jan 10 '19 at 09:06
  • SO is not allowing me to comment further. [Lets chat for more details](https://chat.stackoverflow.com/rooms/186455/discussion-between-miradham-and-drake-wu) – miradham Jan 10 '19 at 09:58