Installing a driver programmatically using INF file c++

假如想象 提交于 2021-01-21 04:42:05

问题


Could someone here please let me know how to install 3rd party device drivers programmatically if all the required files i.e. inf file, .sys etc are provided. The minimum operating system this solution SHOULD work on is Windows2000.

I tried copying the .inf file into the Win Folder\INF folder and the sys file into the Win folder\system32\drivers but each time plug in the device, windows pops up Found New Hardware user interface which is what i am trying to avoid.

Below is something i tried but the function returns error 87 (The parameter is incorrect).

HINF HInf;                
UINT ErrorLine;            
BOOL bRes = FALSE;
PBOOL FileWasInUse = FALSE;

LPCSTR szSourceFileName = _T("C:\\Drivers_HypercomP1320\\hypvcpusb.inf");
LPCSTR szInfFileName  = _T("hypvcpusb.inf");
PVOID Context = NULL;

HInf = SetupOpenInfFile ( szSourceFileName, NULL, INF_STYLE_WIN4, &ErrorLine);          

LPCSTR  SourceFile = ("hypvcp.sys");
LPCSTR SourcePathRoot = _T("C:\\Drivers_HypercomP1320");
LPCSTR DestinationName = _T("C:\\WINDOWS\\system32\\drivers\\hypvcp.sys");

bRes = SetupInstallFileEx ( HInf, NULL, SourceFile, SourcePathRoot, DestinationName, SP_COPY_FORCE_IN_USE,
                            (PSP_FILE_CALLBACK)CopyMsgHandler, Context, FileWasInUse);   

DWORD dwVal = GetLastError();

SetupCloseInfFile(HInf);


// Callback function
UINT CopyMsgHandler (UINT Context, UINT Notification,UINT_PTR Param1, UINT_PTR Param2)
{
    UINT rtnValue = NO_ERROR;
    return rtnValue;
}

Thanks.


回答1:


You can use InstallHinfSection.




回答2:


It might be your use of

PBOOL FileWasInUse = FALSE;

. You should change it in

BOOL FileWasInUse = FALSE;

and use it in the function-call with &FileWasInUse (note the &-character).




回答3:


Yes. You start by calling

SC_HANDLE manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (manager)
{
    wprintf(L"Opened SC Manager\n");
}
else
{
    wprintf(L"Open SC Manager failed\n");
    return;
}

Then having the .inf file stored in szInfFileName you call:

HInf = SetupOpenInfFile(szInfFileName.c_str(), NULL, INF_STYLE_WIN4, &ErrorLine);

Then you call

if (SetupInstallFileEx(HInf, NULL, SourceFile, SourcePathRoot, DestinationName, SP_COPY_NEWER_OR_SAME, NULL, Context, &FileWasInUse) == NULL)

SourceFile = the driver file name (ending with .sys) SourcePathRoot = the location of the driver file (would be the path where your program runs from) DestinationName = full path of the driver to be installed (for example:

c:\windows\system32\drivers\yourdriver.sys 

Then there is the Registry. You need to add an entry for your driver under

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\

this entry (key) should have: Driver name, display name, description, ErrorControl and Group.

Next step, you start the driver using:

SC_HANDLE service = CreateService(manager,
                    DRIVER_NAME,
                    DRIVER_NAME,
                    SERVICE_ALL_ACCESS,
                    SERVICE_KERNEL_DRIVER,
                    SERVICE_AUTO_START,
                    SERVICE_ERROR_NORMAL,
                    KeyName,
                    NULL, NULL, NULL, NULL, NULL);

When KeyName is the driver's path under System32 as appeared in the Registry entry. For example:

system32\drivers\yourdriver.sys

Last step:

BOOL result = StartService(service, 0, NULL);

and cleaning up

CloseServiceHandle(manager)


来源:https://stackoverflow.com/questions/6473096/installing-a-driver-programmatically-using-inf-file-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!