Installing a driver programmatically using INF file c++
Asked Answered
G

3

6

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.

Guardroom answered 24/6, 2011 at 19:55 Comment(2)
right click on the inf file, then click install.Leonorleonora
Guyz thanks for the reply but the drivers installation has be programmatic in C,C++.Guardroom
C
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)
Camelot answered 6/11, 2016 at 12:46 Comment(4)
Really good tutorial on how to install and start a driver from usermode codeGarnishment
Hello, i am trying to install my minifilter driver from a windows service running under SYSTEM. The problem is i'm using C#. I tried to pinvoke the methods you mentioned, although i cannot find how to pinvoke SetupInstallFileEx. Do you have any idea how can i get a driver installed from my windows service?Treehopper
Best if you use C++Camelot
StartService or starting my driver in general causes a blue screen in GenuineIntel.sys before my DriverEntry is ever called for a completely unknown reasonGarnishment
I
2

You can use InstallHinfSection.

Insurgent answered 24/6, 2011 at 20:39 Comment(1)
I gave it a quick test using a batch file but this API is not installing drivers on Windows Vista. When i plug in the device the windows pops up Found New Hardware UI RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection TI3410.Uni 128 C:\\Drivers_Test\\Testusb.infGuardroom
G
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).

Genseric answered 25/6, 2011 at 1:48 Comment(3)
+1 MSDN says this parameter is required, passing a NULL pointer will cause error 87.Canyon
you are right, the function executes correctly now but once i plug in the device "Found New Hardware" wizard still pops up. I even tried rebooting the PC and plugged the device again but same result. Is there another approach to that can used to install the drivers??Guardroom
@newdev1: I really don't know ,I just spotted the programming error. But it sounds to me as good material for a new question!Genseric

© 2022 - 2024 — McMap. All rights reserved.