using System;
using System.Text;
using System.Runtime.InteropServices;
namespace DevInfo
{
??? class DeviceInfo
??? {
??????? public const int DIGCF_PRESENT??? = (0x00000002);
??????? public const int MAX_DEV_LEN = 1000;
??????? public const int SPDRP_FRIENDLYNAME = (0x0000000C);?
????????? // FriendlyName (R/W)
??????? public const int SPDRP_DEVICEDESC = (0x00000000);???
????????? // DeviceDesc (R/W)
??????? [StructLayout(LayoutKind.Sequential)]
??????????? public class SP_DEVINFO_DATA
??????????????? {
???????????????? public int cbSize;
???????????????? public Guid? ClassGuid;
???????????????? public int DevInst;??? // DEVINST handle
???????????????? public ulong Reserved;
??????????????? };
??????? [DllImport("setupapi.dll")]//
??????? public static extern Boolean
????????? SetupDiClassGuidsFromNameA(string ClassN, ref Guid guids,
??????????? UInt32 ClassNameSize, ref UInt32 ReqSize);
??????? [DllImport("setupapi.dll")]
??????? public static extern IntPtr??????????????? //result HDEVINFO
????????? SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator,
??????????? IntPtr???? hwndParent,? UInt32 Flags);
??????? [DllImport("setupapi.dll")]
??????? public static extern Boolean
????????? SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
??????????? SP_DEVINFO_DATA???? DeviceInfoData);
??????? [DllImport("setupapi.dll")]
??????? public static extern Boolean
????????? SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
??????? [DllImport("setupapi.dll")]
??????? public static extern Boolean
????????? SetupDiGetDeviceRegistryPropertyA(IntPtr DeviceInfoSet,
????????? SP_DEVINFO_DATA???? DeviceInfoData, UInt32 Property,
????????? UInt32?? PropertyRegDataType, StringBuilder? PropertyBuffer,
????????? UInt32 PropertyBufferSize, IntPtr RequiredSize);
??????? public static int EnumerateDevices(UInt32 DeviceIndex,
??????????????? string ClassName,
??????????????? StringBuilder DeviceName)
??????? {
???????? UInt32 RequiredSize = 0;
???????? Guid guid=Guid.Empty;
???????? Guid[] guids=new Guid[1];
???????? IntPtr NewDeviceInfoSet;
???????? SP_DEVINFO_DATA DeviceInfoData= new SP_DEVINFO_DATA();
???????? bool res=SetupDiClassGuidsFromNameA(ClassName,
??????????????????? ref guids[0],RequiredSize,
??????????????????? ref RequiredSize);
???????? if(RequiredSize==0)
?????????????? {
??????????????? //incorrect class name:
??????????????? DeviceName=new StringBuilder("");
??????????????? return -2;
?????????????? }
???????? if(!res)
????????? {
?????????? guids=new Guid[RequiredSize];
?????????? res=SetupDiClassGuidsFromNameA(ClassName,ref guids[0],RequiredSize,
??????????????? ref RequiredSize);
?????????? if(!res || RequiredSize==0)
?????????????? {
?????????? //incorrect class name:
??????????????? DeviceName=new StringBuilder("");
??????????????? return -2;
?????????????? }
????????? }
???????? //get device info set for our device class
???????? NewDeviceInfoSet=SetupDiGetClassDevsA(ref guids[0],0,IntPtr.Zero,
???????????????????? DIGCF_PRESENT);
???????? if( NewDeviceInfoSet.ToInt32() == -1 )
???????? if(!res)
?????????????? {
????????? //device information is unavailable:
??????????????? DeviceName=new StringBuilder("");
??????????????? return -3;
?????????????? }
??????????? DeviceInfoData.cbSize = 28;
??????????? //is devices exist for class
??????????? DeviceInfoData.DevInst=0;
??????????? DeviceInfoData.ClassGuid=System.Guid.Empty;
??????????? DeviceInfoData.Reserved=0;
??????????? res=SetupDiEnumDeviceInfo(NewDeviceInfoSet,
?????????????????? DeviceIndex,DeviceInfoData);
??????????? if(!res) {
???????? //no such device:
??????????????? SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
??????????????? DeviceName=new StringBuilder("");
??????????????? return -1;
??????????? }
??????? DeviceName.Capacity=MAX_DEV_LEN;
??????? if(!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
????????? DeviceInfoData,
??????? SPDRP_FRIENDLYNAME,0,DeviceName,MAX_DEV_LEN,IntPtr.Zero) )
??????? {
???????? res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
????????? DeviceInfoData,SPDRP_DEVICEDESC,0,DeviceName,MAX_DEV_LEN,
??????????? IntPtr.Zero);
???????? if(!res){
???????? //incorrect device name:
??????????????? SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
??????????????? DeviceName=new StringBuilder("");
??????????????? return -4;
??????????? }
??????? }
???????? return 0;
??????? }
??????? [STAThread]
??????? static void Main(string[] args)
??????? {
???????? StringBuilder devices=new StringBuilder("");
???????? UInt32 Index=0;
???????? int result=0;
???????? if(args.Length != 1)
????????? {
??????????? Console.WriteLine("command line format:");
??????????? Console.WriteLine("DevInfo <classname>"</classname>);
??????????? return;
????????? }
???????? while(true)
????????? {
??????????? result=EnumerateDevices(Index, args[0], devices);
??????????? Index++;
??????????? if(result == -2)
??????????????????? {
???????????????????? Console.WriteLine("Incorrect name of Class = {0}",
?????????????????????? args[0]);
???????????????????? break;
??????????????????? }
??????????? if(result == -1)break;
??????????? if(result == 0)Console.WriteLine("Device{0} is {1}",
????????????? Index, devices);
??????? }
??????? }
??? }
}
分享到:
相关推荐
Akhil Mittal 撰写的《*** Web API》是一本深入探讨如何创建.NET平台上的RESTful服务的实用指南。Akhil Mittal 是一位资深分析师和微软MVP(最有价值专家),他在本书中分享了他在.NET领域的深厚经验。 在本书中,...
前端开源库-diving-squirrel潜水松鼠,简单网络应用的框架。它使用了Express服务器和Jade模板系统。
- **Annotated Reminder of Important Computing Terms**: Before diving deep into the CLR and .NET, it's crucial to understand fundamental computing concepts like context, threads safety, state, ...
在“Module-7-Diving-Deeper-Into-React-Component”这个模块中,我们将深入探讨React组件这一核心概念,它是构建React应用的基础。React组件是JavaScript函数或类,它们接收输入(称为“props”)并返回React元素,...
本书《Diving Deep Into Kubernetes Networking》介绍与Kubernetes相关的各种网络概念,对于操作员、开发者或决策者可能会发现有用。网络是一个复杂的话题,尤其是当涉及到像Kubernetes这样的分布式系统时。了解技术...
在这个名为“HAN-OOPD-Diving-For-Treasure”的项目中,我们可以看到这是一个针对对象导向编程设计(Object-Oriented Programming Design, OOPD)的学校项目,特别是使用Java语言来实现的。这个“续集”可能是指对...
火车订票系统java源码作业 1 - 跳伞预订系统 截止日期:第 5 周,星期一,上午 9 点(10 月 ...练习如何应用系统的面向对象设计过程 获得实现具有多个交互类的面向对象程序的经验 ...在本作业中,您将设计并实现一个原型...
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...
SharePoint 2010 * Windows PowerShell = Disaster Recovery2 (Audience: IT Admins) ...... 38 SharePoint 2010’s New Granular Content Restore Capability (Audience: IT Admins) ........ 49 SharePoint 2010’...
- **Diving into Objective-C**: Explores more advanced topics in Objective-C, such as protocols and delegates. - **More Data Comparison**: Teaches how to compare and process data efficiently. - **...
在阅读"Diving into the Laravel Framework"这本书时,你可能会学到如何编写单元测试来验证WAL接口的正确性,确保在事务处理过程中数据的完整性和一致性。Laravel的内置测试框架`PHPUnit`可以帮助你编写这些测试,并...
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 ...
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 的...
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 ...
图深度学习研究库DIG:Dive into Graphs是图深度学习研究的统包库。 DIG:用于图深度学习研究的总库,刘萌*,罗有志*,王丽梅*,谢耀辰*,郝昊*,桂树瑞,赵旭,余海阳,张静屯,刘毅,严克强,宝拉·奥兹特金(Bora...
Diving deeper to 2 meters Calculated depth: 2 ... Diving deeper to 10 meters Calculated depth: 10 ``` 这里可以看出,随着递归深度的增加,`Test-NestLevel`返回的嵌套深度也在增加。 #### 绝对深度与相对...