C
Christian Schwarz
Hello,
powering on and off process of the PCMCIA expansion cards (WLAN, GSM/GPRS
and WLAN/GSM/GPRS adapters) I got my hands off, needs several seconds (3 -
15). Waiting a fixed time span (i.e. 15 seconds) after switch power on or
off isn't satisfying. So I browsed the registry and found the
HKLM\Drivers\Active subkey which has an entry for each active driver.
Now I'm using the WaitDriverActive() after switching power on and the
WaitDriverInactive() method after switching power off to search for my
particular adapter using the device PNP-ID as identifier. This works pretty
good.
The problem I'm seeing is the waiting for the driver's deactivation. When my
IsDriverActive() method opens an HKLM\Drivers\Active\xxx subkey for reading
the PNP-ID, this subkey is blocked, isn't it? Supposed it is blocked, the
system (most probably the device manager) cannot remove the
HKLM\Drivers\Active\xxx subkey after driver's deactivation. This leads to
several hundreds more or less empty HKLM\Drivers\Active\xxx subkeys after
some power on/power off cycles.
Is there a way to read a registry key's value without blocking the whole
subkey? What problems may arise with my method and several hundreds
HKLM\Drivers\Active\xxx subkeys? At which point the device manager deletes
unneeded subkeys?
Many thanks, Christian
public bool IsDriverActive(string devicePnpId)
{
try
{
if (devicePnpId != null)
{
using (RegistryKey activeKey =
Registry.LocalMachine.OpenSubKey(@"Drivers\Active"))
{
string[] subKeys =
activeKey.GetSubKeyNames();
for (int index = (subKeys.Length - 1);
(index >= 0); index--)
{
try
{
using (RegistryKey deviceKey
= activeKey.OpenSubKey(subKeys[index]))
{
string pnpId =
deviceKey.GetValue("PnpId", null) as string;
if ((pnpId != null)
&& (pnpId == devicePnpId))
return true;
}
}
catch
{
}
}
}
}
}
catch
{
}
return false;
}
public bool WaitDriverActive(string devicePnpId, TimeSpan timeout)
{
DateTime stopWaiting = DateTime.Now.Add(timeout);
while (DateTime.Now < stopWaiting)
{
if (this.IsDriverActive(devicePnpId))
return true;
Thread.Sleep(1000);
}
return false;
}
public bool WaitDriverInactive(string devicePnpId, TimeSpan timeout)
{
DateTime stopWaiting = DateTime.Now.Add(timeout);
while (DateTime.Now < stopWaiting)
{
if (!this.IsDriverActive(devicePnpId))
return true;
Thread.Sleep(1000);
}
return false;
}
powering on and off process of the PCMCIA expansion cards (WLAN, GSM/GPRS
and WLAN/GSM/GPRS adapters) I got my hands off, needs several seconds (3 -
15). Waiting a fixed time span (i.e. 15 seconds) after switch power on or
off isn't satisfying. So I browsed the registry and found the
HKLM\Drivers\Active subkey which has an entry for each active driver.
Now I'm using the WaitDriverActive() after switching power on and the
WaitDriverInactive() method after switching power off to search for my
particular adapter using the device PNP-ID as identifier. This works pretty
good.
The problem I'm seeing is the waiting for the driver's deactivation. When my
IsDriverActive() method opens an HKLM\Drivers\Active\xxx subkey for reading
the PNP-ID, this subkey is blocked, isn't it? Supposed it is blocked, the
system (most probably the device manager) cannot remove the
HKLM\Drivers\Active\xxx subkey after driver's deactivation. This leads to
several hundreds more or less empty HKLM\Drivers\Active\xxx subkeys after
some power on/power off cycles.
Is there a way to read a registry key's value without blocking the whole
subkey? What problems may arise with my method and several hundreds
HKLM\Drivers\Active\xxx subkeys? At which point the device manager deletes
unneeded subkeys?
Many thanks, Christian
public bool IsDriverActive(string devicePnpId)
{
try
{
if (devicePnpId != null)
{
using (RegistryKey activeKey =
Registry.LocalMachine.OpenSubKey(@"Drivers\Active"))
{
string[] subKeys =
activeKey.GetSubKeyNames();
for (int index = (subKeys.Length - 1);
(index >= 0); index--)
{
try
{
using (RegistryKey deviceKey
= activeKey.OpenSubKey(subKeys[index]))
{
string pnpId =
deviceKey.GetValue("PnpId", null) as string;
if ((pnpId != null)
&& (pnpId == devicePnpId))
return true;
}
}
catch
{
}
}
}
}
}
catch
{
}
return false;
}
public bool WaitDriverActive(string devicePnpId, TimeSpan timeout)
{
DateTime stopWaiting = DateTime.Now.Add(timeout);
while (DateTime.Now < stopWaiting)
{
if (this.IsDriverActive(devicePnpId))
return true;
Thread.Sleep(1000);
}
return false;
}
public bool WaitDriverInactive(string devicePnpId, TimeSpan timeout)
{
DateTime stopWaiting = DateTime.Now.Add(timeout);
while (DateTime.Now < stopWaiting)
{
if (!this.IsDriverActive(devicePnpId))
return true;
Thread.Sleep(1000);
}
return false;
}