In my deployment process, it had looked like I was going to need to detect some changes in hardware and then perform a reboot.
I researched how to do this but it turns ou that I don’t need this code. Into the cave it goes.
You can of course run the “Add New Hardware” wizard manually. Here’s the command line to do just that:
“C:\WINDOWS\system32\rundll32.exe” C:\WINDOWS\system32\shell32.dll,Control_RunDLL “C:\WINDOWS\system32\hdwwiz.cpl”,Detect Hardware
However, what if you want to automate the process.
The information for how to do this is relatively scarce even though there is a technet page about it. Strangely enough the first thing I found was an NSIS script for doing this through that open source instalation program. The strange thing about it is that it was on a WinAMP website (link).
Here’s that code:
[code]
Function ScanForNewHW
SetPluginUnload alwaysoff
StrCpy $1 “”
System::Call ‘setupapi::CM_Locate_DevNodeA(*i .r0, t r1, i r2) i .r3’
System::Call ‘setupapi::CM_Reenumerate_DevNode(i r0, i r4) i .r5’
SetPluginUnload manual
System::Free 0
FunctionEnd
[/code]
Armed with the DLL name, the second thing I found was an Install Shield script (link) that allowed it to be done:
[code]
function ScanForHardwareChanges()
NUMBER devInst, myreturn;
begin
if(UseDLL(WINSYSDIR ^ “cfgmgr32.dll”) != 0)then
MessageBox(“Didn’t load Dll”, SEVERE);
return FALSE;
endif;
myreturn = CM_Locate_DevNodeA(&devInst, “\0”, 0);
myreturn = CM_Reenumerate_DevNode(devInst, 0);
UnUseDLL(WINSYSDIR ^ “cfgmgr32.dll”);
return TRUE;
end;
[/code]
Armed with the DLL name and a possible procedure name, I was able to track down the Microsoft support page about it (link). That page provided a C routine for calling the code. Here it is:
[C]
BOOL ScanForHardwareChanges()
{
DEVINST devInst;
CONFIGRET status;
//
// Get the root devnode.
//
status = CM_Locate_DevNode(&devInst, NULL, CM_LOCATE_DEVNODE_NORMAL);
if (status != CR_SUCCESS) {
printf(“CM_Locate_DevNode failed: %x\n”, status);
return FALSE;
}
status = CM_Reenumerate_DevNode(devInst, 0);
if (status != CR_SUCCESS) {
printf(“CM_Reenumerate_DevNode failed: %x\n”, status));
return FALSE;
}
return TRUE;
}
[/c]
However, I wanted to do this in Delphi. With the correct constant names, I was able to find two references to this routine. The Delphi JEDI project has a provides a routine for loading the DLL that allows these calls to be made and either someone (link) translated Microsoft’s code into a routine for scanning for the hardware or there was a, now gone, JEDI demo project that included this routine. Either way, the French site was the first one I’d found that scanned for new hardware with Delphi.
Here is that code:
[delphi]
procedure SomeProcedure;
// First you need to load the module.
LoadConfigManagerApi;
// Then call a translation of the MS routine
ScanForHardwareChanges;
end;
// Here’s the translation of the ScanForHardwareChanges
function ScanForHardwareChanges: boolean;
var
dev: DEVINST;
status: CONFIGRET;
begin
status := CM_Locate_DevNode(dev, ”, CM_LOCATE_DEVNODE_NORMAL);
if (status <> CR_SUCCESS) then
begin
result := FALSE;
exit;
end;
status := CM_Reenumerate_DevNode(dev, 0);
if (status <> CR_SUCCESS) then
begin
result := FALSE;
exit;
end;
Result := TRUE;
end;
[/delphi]
That routine was picked up on a Russian site (link) and modified to be independent of the JEDI files. However, both of these routines include way more information than is needed.
The process is really simple.
1. Load the DLL
2. Get the location of the two methods you need.
3. Call them (using the appropriate constants
4. Unload everything.
I’ve written my own Delphi routine that does all that and has no extra baggage dragged (drug?) along for the ride..
My all-in-one solution:
[delphi]
{******************************************************************************
ScanForHardwareChanges
by Brian Layman at TheCodeCave.com
******************************************************************************}
function ScanForHardwareChanges: Boolean;
const
CFGMGR32_DLL = ‘cfgmgr32.dll’;
CM_LOCATE_DEVNODE_NAME = ‘CM_Locate_DevNodeA’;
CM_REENUMERATE_DEVNODE_NAME = ‘CM_Reenumerate_DevNode’;
CM_LOCATE_DEVNODE_NORMAL = $00000000;
CR_SUCCESS = $00000000;
var
DeviceNode: DWord;
HCfgMgr: THandle;
CM_Locate_DevNode: function(var dnDevInst: DWord; pDeviceID: PAnsiChar;
ulFlags: ULONG): DWord; stdcall;
CM_Reenumerate_DevNode: function(dnDevInst: DWord; ulFlags: ULong): DWord; stdcall;
begin // ScanForHardwareChanges
Result := FALSE;
HCfgMgr := LoadLibrary(CFGMGR32_DLL);
if (HCfgMgr < 32)
then MessageDlg('Error: could not find Configuration Manager DLL', mtError, [mbOk], 0)
else begin
try
CM_Locate_DevNode := GetProcAddress(HCfgMgr, CM_LOCATE_DEVNODE_NAME);
CM_Reenumerate_DevNode := GetProcAddress(HCfgMgr, CM_REENUMERATE_DEVNODE_NAME);
if (CM_Locate_DevNode(DeviceNode, NIL, CM_LOCATE_DEVNODE_NORMAL) = CR_SUCCESS)
then Result := (CM_Reenumerate_DevNode(DeviceNode, 0) = CR_SUCCESS);
finally // wrap up
FreeLibrary(HCfgMgr);
end; // try/finally
end;
end; // ScanForHardwareChanges
[/delphi]
As a bonus, here it is combined into a project that scans for new hardware and then reboots the computer.
Continue reading Trigger a hardware detection scan from Delphi, InstallShield, C++, script or Run prompt
