0

I am trying to install a driver with windows C++. Registering and starting the service is working fine but I have troubles with installing the INF file. I've look over the internet and wrote some code but can't make it work. Make it short, I just need help with installing the INF file programmly.

Make it short, it doesn't install the INF file. I also checked GetLastError() after I call SetupOpenInfFile and it returns 0. Btw, is there any way to get rid of the INF file?

SC_HANDLE hSCManager;
SC_HANDLE hService;
SERVICE_STATUS ss;
SERVICE_STATUS_PROCESS status;
HINF HInf;
UINT ErrorLine;
PCWSTR szInfFileName = L"C://scanner.inf";
PVOID Context = NULL;
PBOOL FileWasInUse = NULL;

LPCSTR  SourceFile = "scanner.sys";
LPCSTR SourcePathRoot = "C://test/";
LPCSTR DestinationName ="C://scanner.sys";
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
printf("Load Driver\n");
if (hSCManager)
{
    HInf = SetupOpenInfFile(szInfFileName, NULL, INF_STYLE_WIN4, &ErrorLine);

    printf("Create Service\n");
    hService = CreateService(hSCManager, L"scanner", L"scanner Driver", SERVICE_START | DELETE | SERVICE_STOP, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, L"C://scanner.sys", NULL, NULL, NULL, NULL, NULL);
    if (!hService)
    {
        hService = OpenService(hSCManager, L"scanner", SERVICE_START | DELETE | SERVICE_STOP);
    }
    if (hService)
    {
        printf("Start Service\n");
        StartService(hService, 0, NULL);
        printf("Press Enter to close service\r\n");
        getchar();
        ControlService(hService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&status);
        CloseServiceHandle(hService);
        DeleteService(hService);
    }
    CloseServiceHandle(hSCManager);
}
  • 1
    You need to link to `Setupapi.lib`. – Captain Obvlious Jan 11 '17 at 18:11
  • @CaptainObvlious - Thanks for help. Now code is compiling and I can run it, but it doesn't install the INF file. I also checked GetLastError() after I call SetupOpenInfFile and it returns 0. Kindly reopen it. – user3449011 Jan 11 '17 at 18:37
  • Your current problem has been solved and you now have a new one. That calls for a new question not reopening an obvious dupe and changing it. – Captain Obvlious Jan 11 '17 at 18:39

0 Answers0