Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing a driver programmatically using INF file c++

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.

like image 483
newdev1 Avatar asked Jun 24 '11 19:06

newdev1


People also ask

How do I install an INF driver?

Right-Click InstallIn Windows Explorer, select and hold (or right-click) the INF file name. A shortcut menu will appear. Select Install.

How do I install an INF file on a flash drive?

Right-click on the . inf file in C:\Windows\inf and click Install. This process creates a PNF file for your device. You are now ready to install your USB device.

What is INF in driver?

A setup information (INF) file is a text file in a driver package that contains all of the information that device installation components use to install a driver package on a device. Windows uses INF files to install the following components for a device: One or more drivers that support the device.


1 Answers

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)
like image 52
Michael Haephrati Avatar answered Oct 09 '22 12:10

Michael Haephrati