安装驱动程序inf和sys

时间:2017-01-11 19:26:57

标签: c++ windows driver

我正在尝试使用Windows C ++安装我的驱动程序。注册和启动服务工作正常但我在安装INF文件时遇到了麻烦。我查看了互联网并编写了一些代码,但无法使其正常工作。

简而言之,它不会安装INF文件。我在调用GetLastError()后检查SetupOpenInfFile并返回0。

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://";
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);
PCWSTR  SourceFile2 = L"scanner.sys";
PCWSTR SourcePathRoot2 = L"C://";
PCWSTR DestinationName2 = L"C://scanner.sys";

SetupInstallFileEx(HInf, NULL, SourceFile2, SourcePathRoot2,  DestinationName2, SP_COPY_FORCE_IN_USE, (PSP_FILE_CALLBACK)CopyMsgHandler, Context, &FileWasInUse);    
// no error here
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);
    // I get error here - The system cannot find the file specified.

    printf("Press Enter to close service\r\n");
    getchar();
    ControlService(hService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&status);
    CloseServiceHandle(hService);
    DeleteService(hService);
}
CloseServiceHandle(hSCManager);
}

0 个答案:

没有答案