using System;
using System.Runtime.InteropServices;
using System.Text;
namespace DevClasses
{
/// <summary></summary>
/// Summary description for Class.
///
class DeviceClasses
{
? /// <summary></summary>
? /// The main entry point for the application.
? ///
? public const int MAX_NAME_PORTS=7;
? public const int RegDisposition_OpenExisting=(0x00000001);
??? // open key only if exists
? public const int CM_REGISTRY_HARDWARE=(0x00000000);
? public const int CR_SUCCESS = (0x00000000);
? public const int CR_NO_SUCH_VALUE = (0x00000025);
? public const int CR_INVALID_DATA = (0x0000001F);
? public const int DIGCF_PRESENT = (0x00000002);
? public const int DIOCR_INSTALLER = (0x00000001);
// MaximumAllowed access type to Reg.
? public const int MAXIMUM_ALLOWED = (0x02000000);
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst; // DEVINST handle
public ulong Reserved;
};
? [DllImport("cfgmgr32.dll")]
? public static extern UInt32
? CM_Open_DevNode_Key(IntPtr dnDevNode, UInt32 samDesired,
???????? UInt32 ulHardwareProfile,
???????? UInt32 Disposition,IntPtr phkDevice, UInt32 ulFlags);
? [DllImport("cfgmgr32.dll")]
? public static extern UInt32
? CM_Enumerate_Classes(UInt32 ClassIndex,ref Guid ClassGuid, UInt32 Params);
? [DllImport("setupapi.dll")]//
? public static extern Boolean
?? SetupDiClassNameFromGuidA(ref Guid ClassGuid,
??????????? StringBuilder ClassName, //char * ?
?? UInt32 ClassNameSize, ref UInt32 RequiredSize);
? [DllImport("setupapi.dll")]
? public static extern IntPtr
?? SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator,
?? IntPtr? hwndParent, UInt32 Flags);
? [DllImport("setupapi.dll")]
? public static extern Boolean
?? SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
?? ref SP_DEVINFO_DATA? DeviceInfoData);
? [DllImport("setupapi.dll")]
? public static extern Boolean
?? SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
? [DllImport("setupapi.dll")]
? public static extern IntPtr
?? SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 samDesired,
?? UInt32 Flags, ref string hwndParent, IntPtr Reserved);
? [DllImport("setupapi.dll")]
? public static extern IntPtr
? SetupDiOpenClassRegKeyExA(
? ref Guid ClassGuid, UInt32 samDesired, int Flags, IntPtr MachineName,
? UInt32 Reserved);
? [DllImport("advapi32.dll")]
? public static extern UInt32
? RegQueryValueA(IntPtr KeyClass,UInt32 SubKey,
???????? StringBuilder ClassDescription,ref UInt32 sizeB);
? [DllImport("user32.dll")]
? public static extern Boolean
? CharToOem(String lpszSrc, StringBuilder lpszDst);
? public static int EnumerateClasses(UInt32 ClassIndex,
?? ref StringBuilder ClassName, StringBuilder ClassDescription,
??????? ref bool DevicePresent)
? {
?? Guid ClassGuid=Guid.Empty;
?? IntPtr NewDeviceInfoSet;
?? SP_DEVINFO_DATA DeviceInfoData;
?? UInt32 result;
?? StringBuilder name=new StringBuilder("");
?? bool resNam=false;
?? UInt32 RequiredSize=0;
?? IntPtr ptr;
?? result = CM_Enumerate_Classes(ClassIndex, ref ClassGuid,0);
??? ClassName=new StringBuilder("");
??? DevicePresent=false;
?? //incorrect device class:
?? if(result == CR_INVALID_DATA)
?? {
??? return -2;
?? }
? //device class is absent
?? if(result == CR_NO_SUCH_VALUE)
?? {
??? return -1;
?? }
? //bad param. - fatal error
?? if(result != CR_SUCCESS)
?? {
??? return -3;
?? }
?? name.Capacity=0;
?? resNam=SetupDiClassNameFromGuidA(ref ClassGuid,name,RequiredSize,
???????? ref RequiredSize);
?? if(RequiredSize > 0)
??? {
??? name.Capacity=(int)RequiredSize;
??? resNam=SetupDiClassNameFromGuidA(ref ClassGuid,name,
?????????? RequiredSize,ref RequiredSize);
??? }
?? NewDeviceInfoSet=SetupDiGetClassDevsA(
??? ref ClassGuid,
??? 0,
??? IntPtr.Zero,
??? DIGCF_PRESENT);
?? if(NewDeviceInfoSet.ToInt32() == -1)
??? {? DevicePresent=false;
????? ClassName=name;
????? return 0;}
?? IntPtr KeyClass=SetupDiOpenClassRegKeyExA(
??? ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER,IntPtr.Zero,0);
?? if(KeyClass.ToInt32() == -1)
??? {? DevicePresent=false;
????? ClassName=name;
????? return 0;}
?? UInt32 sizeB=1000;
?? String abcd="";
?? StringBuilder CD=new StringBuilder("");
?? ClassDescription.Capacity=1000;
?
?? UInt32 res=RegQueryValueA(KeyClass,0,ClassDescription,ref sizeB);
?? if(res != 0)ClassDescription=new StringBuilder("");
?? SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
??? ClassName=name;
??? DevicePresent=true;
?? return 0;
? }
? [STAThread]
? static void Main(string[] args)
? {
?? StringBuilder classes=new StringBuilder("");
?? StringBuilder classesDescr=new StringBuilder("");
?? StringBuilder classesDescrOEM=new StringBuilder("");
?? classesDescrOEM.Capacity=1000;
?? Boolean DevExist=false;
?? UInt32 i=0;
?? while(true)
?? {
?? int res=EnumerateClasses(i,ref classes,classesDescr,ref DevExist);
?? if(res == -1)break;
?? ++i;
?? if(res 1 || !DevExist)continue;
?? Console.WriteLine("ClassName={0}, Description={1}",classes,classesDescr);
?? }
?? return;
? }
}
}
分享到:
相关推荐
国外大牛、微软MVP所著的Asp.Net方面的WebApi书籍
前端开源库-diving-squirrel潜水松鼠,简单网络应用的框架。它使用了Express服务器和Jade模板系统。
Module-7-Diving-Deeper-Into-React-Component
HAN-OOPD-Diving-For-Treasure:OOPD的学校项目-潜水宝藏(续集)
1 - 跳伞预订系统 截止日期:第 5 周,星期一,上午 9 点(10 月 12 日) 价值:15分 宗旨 练习如何应用系统的面向对象设计过程 获得实现具有多个交互类的面向对象程序的经验 了解有关 Java 类库的更多信息 前言 在...
as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication...
抽象wal接口 Diving into the Laravel Framework 一书的示例。 阅读书中的解释。
The New Chart Web Part in SharePoint 2010 (Audience: All) ................................................. 19 Creating SharePoint 2010 Workflows in Visio 2010 (Audience: All) ...........................
You have a great idea for...Diving into Objective-C More Data Comparison Creating User Interfaces Storing Information Protocols and Delegates Memory, Addresses, and Pointers Debugging Programs with Xcode
Diving Deep Into Kubernetes Networking,对k8s网络深入介绍,掌握k8s网络原理
You’ll start with Scala's core types and syntax before diving into higher-order functions and immutable data structures. Author Jason Swartz demonstrates why Scala’s concise and expressive syntax ...
深入了解指令 看演示 官方文档: 一种主要在指令中使用控制器的有趣方法: 更多关于 bindToController: 用于构建此演示文稿的技术: 作为展示框架 用于代码高亮 用于在开发时编译 sass 并捆绑类似 livereload 的...
Best Programming iOS 11 book Diving Deep into Views, View Controllers,
Get to know about event-driven architectures by diving into message queues such as Kafka, Rabbitmq, and AWS SQS. Understand key modern database technologies such as MongoDB, and Amazon’s DynamoDB ...
Java developers in particular may be able to get away with diving right into the more advanced chapters of the book. Regardless of which category of user you fall into, rest assured that there is ...
Java 9 Programming By Example by Peter Verhas English | 26 Apr. 2017 | ASIN: B01KOG6SWI | 504 Pages | AZW3 | 4.1 MB Key Features We bridge the gap between “learning” and “doing” by providing ...
Diving deep into Cycles you will learn Cycle’s node-based material system, how to set-up a 3D scene for rendering, how to create a natural and man-made materials as well as the correct organization ...
You’ll start with a quick overview of the framework and then explore the core components by diving into complete examples, engaging illustrations, and crisp, straightforward explanations. You’ll ...
We then cover more simple examples to build your foundation before diving to some complex data structure problems that will solidify your Java 9 skills. With a special focus on modularity and HTTP 2.0...