遍历设备管理器的设备

源自:http://dev.youkuaiyun.com/develop/article/48/48400.shtm

None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Text;
None.gif
None.gif
None.gif
namespace  DevClasses
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// Summary description for Class.
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif class DeviceClasses
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// The main entry point for the application.
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public const int MAX_NAME_PORTS=7;
InBlock.gif  
public const int RegDisposition_OpenExisting=(0x00000001); 
InBlock.gif  
// open key only if exists
InBlock.gif
  public const int CM_REGISTRY_HARDWARE=(0x00000000);
InBlock.gif
InBlock.gif  
public const int CR_SUCCESS = (0x00000000);
InBlock.gif  
public const int CR_NO_SUCH_VALUE = (0x00000025);
InBlock.gif  
public const int CR_INVALID_DATA = (0x0000001F);
InBlock.gif  
public const int DIGCF_PRESENT = (0x00000002);
InBlock.gif  
public const int DIOCR_INSTALLER = (0x00000001);
InBlock.gif  
// MaximumAllowed access type to Reg.
InBlock.gif
  public const int MAXIMUM_ALLOWED = (0x02000000);
InBlock.gif  [StructLayout(LayoutKind.Sequential)]
InBlock.gif
InBlock.gif   
public class SP_DEVINFO_DATA
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
public int cbSize;
InBlock.gif   
public Guid ClassGuid;
InBlock.gif   
public int DevInst; // DEVINST handle
InBlock.gif
   public ulong Reserved;
ExpandedSubBlockEnd.gif  }
;
InBlock.gif
InBlock.gif
InBlock.gif  [DllImport(
"cfgmgr32.dll")]
InBlock.gif  
public static extern UInt32
InBlock.gif   CM_Open_DevNode_Key(IntPtr dnDevNode, UInt32 samDesired, 
InBlock.gif   UInt32 ulHardwareProfile,
InBlock.gif   UInt32 Disposition,IntPtr phkDevice, UInt32 ulFlags);
InBlock.gif
InBlock.gif  [DllImport(
"cfgmgr32.dll")]
InBlock.gif  
public static extern UInt32
InBlock.gif   CM_Enumerate_Classes(UInt32 ClassIndex,
ref Guid ClassGuid, UInt32 Params);
InBlock.gif
InBlock.gif  [DllImport(
"setupapi.dll")]//
InBlock.gif
  public static extern Boolean
InBlock.gif   SetupDiClassNameFromGuidA(
ref Guid ClassGuid,
InBlock.gif   StringBuilder ClassName, 
//char * ?
InBlock.gif
   UInt32 ClassNameSize, ref UInt32 RequiredSize);
InBlock.gif
InBlock.gif  [DllImport(
"setupapi.dll")]
InBlock.gif  
public static extern IntPtr
InBlock.gif   SetupDiGetClassDevsA(
ref Guid ClassGuid, UInt32 Enumerator,
InBlock.gif   IntPtr  hwndParent, UInt32 Flags);
InBlock.gif
InBlock.gif  [DllImport(
"setupapi.dll")]
InBlock.gif  
public static extern Boolean
InBlock.gif   SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
InBlock.gif   
ref SP_DEVINFO_DATA  DeviceInfoData);
InBlock.gif
InBlock.gif  [DllImport(
"setupapi.dll")]
InBlock.gif  
public static extern Boolean
InBlock.gif   SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
InBlock.gif
InBlock.gif  [DllImport(
"setupapi.dll")]
InBlock.gif  
public static extern IntPtr
InBlock.gif   SetupDiGetClassDevsA(
ref Guid ClassGuid, UInt32 samDesired,
InBlock.gif   UInt32 Flags, 
ref string hwndParent, IntPtr Reserved);
InBlock.gif
InBlock.gif  [DllImport(
"setupapi.dll")]
InBlock.gif  
public static extern IntPtr
InBlock.gif   SetupDiOpenClassRegKeyExA(
InBlock.gif   
ref Guid ClassGuid, UInt32 samDesired, int Flags, IntPtr MachineName,
InBlock.gif   UInt32 Reserved);
InBlock.gif
InBlock.gif  [DllImport(
"advapi32.dll")]
InBlock.gif  
public static extern UInt32
InBlock.gif   RegQueryValueA(IntPtr KeyClass,UInt32 SubKey,
InBlock.gif   StringBuilder ClassDescription,
ref UInt32 sizeB);
InBlock.gif
InBlock.gif
InBlock.gif  [DllImport(
"user32.dll")]
InBlock.gif  
public static extern Boolean
InBlock.gif   CharToOem(String lpszSrc, StringBuilder lpszDst);
InBlock.gif
InBlock.gif  
public static int EnumerateClasses(UInt32 ClassIndex, 
InBlock.gif   
ref StringBuilder ClassName, StringBuilder ClassDescription, ref string id,
InBlock.gif   
ref bool DevicePresent)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   Guid ClassGuid
=Guid.Empty;
InBlock.gif
InBlock.gif   IntPtr NewDeviceInfoSet;
InBlock.gif   SP_DEVINFO_DATA DeviceInfoData;
InBlock.gif   UInt32 result;
InBlock.gif   StringBuilder name
=new StringBuilder("");
InBlock.gif   
bool resNam=false;
InBlock.gif   UInt32 RequiredSize
=0;
InBlock.gif
InBlock.gif   IntPtr ptr;
InBlock.gif
InBlock.gif   result 
= CM_Enumerate_Classes(ClassIndex, ref ClassGuid,0);
InBlock.gif
InBlock.gif
InBlock.gif   ClassName
=new StringBuilder("");
InBlock.gif   DevicePresent
=false;
InBlock.gif   
//incorrect device class:
InBlock.gif
   if(result == CR_INVALID_DATA)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return -2;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
//device class is absent
InBlock.gif
   if(result == CR_NO_SUCH_VALUE)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return -1;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
//bad param. - fatal error
InBlock.gif
   if(result != CR_SUCCESS)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return -3;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif
InBlock.gif   name.Capacity
=0;
InBlock.gif   resNam
=SetupDiClassNameFromGuidA(ref ClassGuid,name,RequiredSize,ref RequiredSize);
InBlock.gif   
if(RequiredSize > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    name.Capacity
=(int)RequiredSize;
InBlock.gif    resNam
=SetupDiClassNameFromGuidA(ref ClassGuid,name,RequiredSize,ref RequiredSize);
InBlock.gif
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   NewDeviceInfoSet
=SetupDiGetClassDevsA(ref ClassGuid,0,IntPtr.Zero,DIGCF_PRESENT);
InBlock.gif
InBlock.gif   
if(NewDeviceInfoSet.ToInt32() == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     DevicePresent
=false;
InBlock.gif    ClassName
=name;
InBlock.gif    id
=ClassGuid.ToString();
ExpandedSubBlockEnd.gif    
return 0;}

InBlock.gif
InBlock.gif   IntPtr KeyClass
=SetupDiOpenClassRegKeyExA(
InBlock.gif    
ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER,IntPtr.Zero,0);
InBlock.gif   
if(KeyClass.ToInt32() == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     DevicePresent
=false;
InBlock.gif    ClassName
=name;
InBlock.gif    id
=ClassGuid.ToString();
ExpandedSubBlockEnd.gif    
return 0;}

InBlock.gif
InBlock.gif
InBlock.gif   UInt32 sizeB
=1000;
InBlock.gif    
InBlock.gif   StringBuilder CD
=new StringBuilder("");
InBlock.gif   ClassDescription.Capacity
=1000;
InBlock.gif  
InBlock.gif   UInt32 res
=RegQueryValueA(KeyClass,0,ClassDescription,ref sizeB);
InBlock.gif
InBlock.gif
InBlock.gif   
if(res != 0)ClassDescription=new StringBuilder("");
InBlock.gif   SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
InBlock.gif   ClassName
=name;
InBlock.gif   id
=ClassGuid.ToString();
InBlock.gif   DevicePresent
=true;
InBlock.gif
InBlock.gif   
return 0;
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [STAThread]
InBlock.gif  
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   StringBuilder classes
=new StringBuilder("");
InBlock.gif   StringBuilder classesDescr
=new StringBuilder("");
InBlock.gif
InBlock.gif   StringBuilder classesDescrOEM
=new StringBuilder("");
InBlock.gif   classesDescrOEM.Capacity
=1000;
InBlock.gif   Boolean DevExist
=false;
InBlock.gif   UInt32 i
=0;
InBlock.gif   
string id="";
InBlock.gif   
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif
InBlock.gif    
int res=EnumerateClasses(i,ref classes,classesDescr,ref   id ,ref DevExist);
InBlock.gif    
if(res == -1)break;
InBlock.gif    
++i;
InBlock.gif    
if(res < -1 || !DevExist)continue;
InBlock.gif    Console.WriteLine(
"ClassName={0}, Description={1},id={2}",classes,classesDescr,id);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
return;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/wxmxiaoming/articles/480862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值