在工作中,我们制造自己的平板电脑。有些平板电脑有指纹生物识别功能,有些则没有。有时技术人员会忘记插入。我还没有找到一种方法来检查该设备(或任何相关设备)是否存在。
我的第一种方法是将GUID用于生物识别,即{53D29EF7-377C-4D14-864B-EB3A85769359}。我将在注册表hklm\system\currontcontrolset\control\class中进行搜索,并检查该键是否存在。
这不起作用,因为即使你从未安装过生物识别软件,Windows7似乎也提供了该密钥。它在XP中工作,但我刚刚在一个曾经有生物识别功能的设备上再次尝试,但我把它拿出来了,那个钥匙仍然存在。
这个问题最难的部分是,我必须使用Windows7、7Embedded、xp和xp embedded。
下一个想法是使用WMI,但我找不到正确的类来调用它来检查它是否存在。
然后我找到了一个biometric.dll,但它只适用于Windows7。
有时,为问题找到一个通用的解决方案并不总是那么容易。我正在用C#做这个项目,但我愿意把它转换成任何语言。
我是不是应该开始找了?
发布于 2012-05-09 23:09:30
在Joshua Drake的帮助下,他给了我一个很棒的链接,告诉我如何解决我的问题,以下是我的结果:
我修复的post代码在某种程度上是专门的,它查找特定的GUID,而只查找第一个GUID。我从一篇关于如何禁用设备的文章中改写了它,尽管这段代码并没有禁用任何东西,它只是检查是否存在。
public static bool IsDevicePresent(string guid)
{
var info = IntPtr.Zero;
var NullGuid = new Guid(guid);
try
{
info = SetupDiGetClassDevsW(ref NullGuid,null,IntPtr.Zero,DIGCF_PRESENT);
CheckError("SetupDiGetClassDevs");
var devdata = new SP_DEVINFO_DATA();
devdata.cbSize = (UInt32)Marshal.SizeOf(devdata);
// Get first device matching device criterion.
SetupDiEnumDeviceInfo(info,0,out devdata);
// if no items match filter, throw
if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
CheckError("No device found matching filter.", 0xcffff);
CheckError("SetupDiEnumDeviceInfo");
}
catch
{
return false;
}
finally
{
if (info != IntPtr.Zero)
SetupDiDestroyDeviceInfoList(info);
}
return true;
}
private static void CheckError(string message, int lasterror = -1)
{
int code = lasterror == -1 ? Marshal.GetLastWin32Error() : lasterror;
if (code != 0)
throw new ApplicationException(String.Format("Error disabling hardware device (Code {0}): {1}",code, message));
}
[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevsW([In] ref Guid ClassGuid,[MarshalAs(UnmanagedType.LPWStr)]string Enumerator,IntPtr parent,UInt32 flags);
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(IntPtr handle);
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet,UInt32 memberIndex,[Out] out SP_DEVINFO_DATA deviceInfoData);
//used to find device info from device manager
[StructLayout(LayoutKind.Sequential)]
private struct SP_DEVINFO_DATA
{
public UInt32 cbSize;
public Guid classGuid;
public UInt32 devInst;
public IntPtr reserved;
}
private const uint DIGCF_PRESENT = 2;
private const uint ERROR_INVALID_DATA = 13;
private const uint ERROR_NO_MORE_ITEMS = 259;
private const uint ERROR_ELEMENT_NOT_FOUND = 1168;这里有一个简单的单元测试来证明它在第一个设备上是有效的
[Test]
public void TestDevicePresent()
{
var bluetoothClassGuid = "e0cbf06c-cd8b-4647-bb8a-263b43f0f974";
var biometricClassGuid = "53D29EF7-377C-4D14-864B-EB3A85769359";
var cdromdrivClassGiud = "4d36e965-e325-11ce-bfc1-08002be10318";
Assert.False(Native.IsDevicePresent(bluetoothClassGuid));
Assert.False(Native.IsDevicePresent(biometricClassGuid));
Assert.True(Native.IsDevicePresent(cdromdrivClassGiud));
}https://stackoverflow.com/questions/10503565
复制相似问题