Tech Support > Microsoft Windows > Development Resources > InstallSelectedDriver crashes application
InstallSelectedDriver crashes application
Posted by Rymfax on August 14th, 2007


Hey guys...need help again.

I'm trying to use InstallSelectedDriver() to install an HDAUDIO
driver. I use LoadLibrary() and GetProcAddress() to call it. In
fact, I even copied the microsoft example exactly from:
http://support.microsoft.com/kb/889763

InstallSelectedDriver() seems to run, but it crashes the application.
Ironically, I tried the example but I had an InfPath in there that
didn't exist and nothing happened, meaning the app didn't crash. But
of course it also didn't install anything since it was the wrong
driver path. I'm posting the function I call using below, but its
virtually the same as the microsoft example. Please help!

--- CODE BELOW ---

void InstallDeviceDriver::Install(wchar_t *devID, wchar_t *inf) {
DWORD Err = NO_ERROR;
HDEVINFO DeviceInfoSet = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA DeviceInfoData;
SP_DRVINFO_DATA DriverInfoData;
SP_DEVINSTALL_PARAMS DeviceInstallParams;
TCHAR NewDevPath[MAX_PATH];
HMODULE hNewDev = NULL;
PINSTALLSELECTEDDRIVER pInstallSelectedDriver = NULL;
DWORD Reboot;

printf("DeviceID = %ws\n", devID);
printf("INF Path = %ws\n", inf);

//
// Create an empty device information list.
//
DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
if (DeviceInfoSet == INVALID_HANDLE_VALUE) {
Err = GetLastError();
goto clean0;
}

//
// Add the device that is referenced by the device instance id
parameter
// to the device information list.
//
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiOpenDeviceInfo(DeviceInfoSet,
(PCWSTR)devID,
NULL,
0,
&DeviceInfoData)) {
Err = GetLastError();
goto clean0;
}

//
// InstallSelectedDriver works on the selected device and on the
// selected driver on that device. Therefore, set this device as
the
// selected one in the device information list.
//
if (!SetupDiSetSelectedDevice(DeviceInfoSet,
&DeviceInfoData)) {
Err = GetLastError();
goto clean0;
}

//
// You now have a SP_DEVINFO_DATA structure
// representing your device. Next, get a SP_DRVINFO_DATA
// structure to install on that device.
//
DeviceInstallParams.cbSize = sizeof(DeviceInstallParams);
if (!SetupDiGetDeviceInstallParams(DeviceInfoSet,
&DeviceInfoData,
&DeviceInstallParams)) {
Err = GetLastError();
goto clean0;
}

//
// Only build the driver list out of the passed-in INF.
// To do this, set the DI_ENUMSINGLEINF flag, and copy the
// full path of the INF into the DriverPath field of the
// DeviceInstallParams structure.
//
DeviceInstallParams.Flags |= DI_ENUMSINGLEINF;
if (FAILED(StringCchCopy(DeviceInstallParams.DriverPa th,

SIZECHARS(DeviceInstallParams.DriverPath),
inf))) {
//
// The file path that was passed in was too big.
//
Err = ERROR_INVALID_PARAMETER;
goto clean0;
}

//
// Set the DI_FLAGSEX_ALLOWEXCLUDEDDRVS flag so that you can use
// this INF even if it is marked as ExcludeFromSelect.
// ExcludeFromSelect means do not show the INF in the legacy
Add
// Hardware Wizard.
//
DeviceInstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;

if (!SetupDiSetDeviceInstallParams(DeviceInfoSet,
&DeviceInfoData,
&DeviceInstallParams)) {
Err = GetLastError();
goto clean0;
}

//
// Build up a Driver Information List from the specified INF.
// Build a compatible driver list, meaning only include the
// driver nodes that match one of the hardware or compatible Ids
of
// the device.
//
if (!SetupDiBuildDriverInfoList(DeviceInfoSet,
&DeviceInfoData,
SPDIT_COMPATDRIVER)) {
Err = GetLastError();
goto clean0;
}

//
// Pick the best driver in the list of drivers that was built.
//
if (!SetupDiCallClassInstaller(DIF_SELECTBESTCOMPATDR V,
DeviceInfoSet,
&DeviceInfoData)) {
Err = GetLastError();
goto clean0;
}

//
// Get the selected driver node.
// Note: If this list does not contain any drivers, this call
// will fail with ERROR_NO_DRIVER_SELECTED.
//
DriverInfoData.cbSize = sizeof(DriverInfoData);
if (!SetupDiGetSelectedDriver(DeviceInfoSet,
&DeviceInfoData,
&DriverInfoData)) {
Err = GetLastError();
goto clean0;
}

//
// At this point, you have a valid SP_DEVINFO_DATA structure and a
// valid SP_DRVINFO_DATA structure.
// Load newdev.dll, GetProcAddress(InstallSelectedDriver), and
call
// that API.
//
// To be more secure, make sure to load the newdev.dll file from
the
// system 32 directory.
//
if (GetSystemDirectory(NewDevPath, SIZECHARS(NewDevPath)) == 0) {
Err = GetLastError();
goto clean0;
}

if (FAILED(StringCchCat(NewDevPath, SIZECHARS(NewDevPath), TEXT("\
\NEWDEV.DLL")))) {
Err = ERROR_INSUFFICIENT_BUFFER;
goto clean0;
}

hNewDev = LoadLibrary(NewDevPath);
if (!hNewDev) {
Err = GetLastError();
goto clean0;
}

pInstallSelectedDriver =
(PINSTALLSELECTEDDRIVER)GetProcAddress(hNewDev,
"InstallSelectedDriver");
if (!pInstallSelectedDriver) {
Err = GetLastError();
goto clean0;
}

//
// Call pInstallSelectedDriver to install the selected driver on
// the selected device.
//
pInstallSelectedDriver(NULL,
DeviceInfoSet,
NULL,
TRUE,
&Reboot);

if (Reboot & (DI_NEEDREBOOT | DI_NEEDRESTART)) {
//
// A reboot is required.
//
}

clean0:
if (hNewDev) {
FreeLibrary(hNewDev);
printf("Library Freed.");
hNewDev = NULL;
}

if (DeviceInfoSet != INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
printf("DeviceInfoSet destroyed.");
DeviceInfoSet = INVALID_HANDLE_VALUE;
}

printf("InstallDriver returned 0x%X\n", Err);
}

Posted by Rymfax on August 14th, 2007


On Aug 14, 12:36 pm, Rymfax <cwal...@bigbangllc.com> wrote:
Slight Update. I ran the app on my Dev machine so I could try to see
the error that was causing the crash. It is as follows:

A buffer overrun has occurred in InstallDriverTest.exe which has
corrupted the program's internal state. Press Break to debug the
program or Continue to terminate the program.

For more details please see Help topic 'How to debug Buffer Overrun
Issues'.


How can I track down what is causing the Buffer Overrun?


Posted by chris.aseltine@gmail.com on August 14th, 2007


On Aug 14, 12:36 pm, Rymfax <cwal...@bigbangllc.com> wrote:

There is a bug in the sample. Don't pass NULL for the third
parameter. I believe it's something like "LPCWSTR lpszReserved" or
something like that. Just give it a pointer to an empty (but valid/
allocated) buffer. I believe this only happens on Win2k.


Posted by Rymfax on August 14th, 2007


On Aug 14, 1:12 pm, chris.aselt...@gmail.com wrote:
I'm encountering this problem on WinXP SP2. Would I passing L"" be
enough, or do I need to pass something else? My C++ is so-so at
best. Any help you can give with examples would be VERY welcome.


Posted by chris.aseltine@gmail.com on August 14th, 2007


On Aug 14, 1:23 pm, Rymfax <cwal...@bigbangllc.com> wrote:

Yeah, L"" might work. Why not just try it? Also, have you stepped
through this in the debugger to understand which call is failing?

Also, are you sure you picked your device instance ID, etc. correctly?


Posted by Rymfax on August 14th, 2007


On Aug 14, 1:35 pm, chris.aselt...@gmail.com wrote:
Chris,

First off, thank you for your help. I am very greatful. To answer
your questions:

1) I did set through the program. The funny thing is, it doesn't
crash on any function. It crashes once it reaches the end of the
Install() function. Does that make sense? To re-iterate, I have a
main() which creates and instance of the InstallDeviceDriver class and
then calls the Install() function on that class. When the code
reaches the end of the Install() function, it crashes. It never
returns to the main().

2) I'm pretty sure I have the right Device Instance ID. I'm using
CM_Get_Device_ID() to get it. I'm not sure what else I can check. I
assumed that the Device Instance ID and the INFPath are correct
because it does do the install, it just crashes at the end of the
function. It's very frustrating because I can't understand what would
cause the Buffer Overun error that I'm getting. I did try running the
application with the InstallSelectedDriver() call commented out. I do
not get the crash, but of course nothing happens either.

Any more thoughts on this? I'm stuck and I'm not sure where to go
from here. There aren't many help search returns on
InstallSelectedDriver in google.



Posted by Maxim S. Shatskih on August 14th, 2007


Yes.

Stack corruption, return address overwritten.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


Posted by Rymfax on August 15th, 2007


On Aug 14, 4:57 pm, "Maxim S. Shatskih" <ma...@storagecraft.com>
wrote:
Thanks for your input Maxim. What would cause this and what can I do
about it? Is this a known bug with using InstallSelectedDriver?



Similar Posts