int InstallDriver(_TCHAR *InfName, _TCHAR *HardwareID)
{
WIN32_FIND_DATA FindFileData;
BOOL RebootRequired = 0; // Must be cleared.
_TCHAR *FName, *HWID;
FName = InfName;
HWID = HardwareID;
if (FindFirstFile(FName,&FindFileData)==INVALID_HANDLE_VALUE)
{
//_tprintf(TEXT(" File not found.\n"));
//_tprintf(TEXT("usage: install \n"));
return 2; // Install Failure
}
//
// Look to see if this device allready exists.
//
if (FindExistingDevice(HWID))
{
//
// No Need to Create a Device Node, just call our API.
//
if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle
HWID, // Hardware ID
FName, // FileName
INSTALLFLAG_FORCE,
&RebootRequired))
{
DisplayError(TEXT("UpdateDriverForPlugAndPlayDevices"));
return 2; // Install Failure
}
}
else
{
if (GetLastError()!= ERROR_NO_MORE_ITEMS)
{
//
// An unknown failure from FindExistingDevice()
//
//_tprintf(TEXT("(IERROR_NO_MORE_ITEMS)\n"));
//_tprintf(TEXT("(Install Failure! Code = 2)\n"));
return 2; // Install Failure
}
//
// Driver Does not exist, Create and call the API.
// HardwareID must be a multi-sz string, which argv[2] is.
//
if (!InstallRootEnumeratedDriver(HWID, // HardwareID
FName, // FileName
&RebootRequired))
{
//_tprintf(TEXT("(InstallRootEnumeratedDriver Failure! Code = 2)\n"));
return 2; // Install Failure
}
}
//
// Enumerate through all Devices.
//
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = 0;
//
// We won't know the size of the HardwareID buffer until we call
// this function. So call it with a null to begin with, and then
// use the required buffer size to Alloc the nessicary space.
// Keep calling we have success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INVALID_DATA)
{
//
// May be a Legacy Device with no HardwareID. Continue.
//
break;
}
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
//
// We need to change the buffer size.
//
if (buffer)
LocalFree(buffer);
buffer = (char *)LocalAlloc(LPTR,buffersize);
}
else
{
//
// Unknown Failure.
//
DisplayError(TEXT("GetDeviceRegistryProperty"));
goto cleanup_DeviceInfo;
}
}
if (GetLastError() == ERROR_INVALID_DATA)
continue;
//
// Compare each entry in the buffer multi-sz list with our HardwareID.
//
for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))