`
aijuans8
  • 浏览: 187125 次
社区版块
存档分类
最新评论

.NET - Diving into System Programming - Part 1[转贴]

 
阅读更多

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;
? }
}
}

分享到:
评论

相关推荐

    diving-into-Asp-Net-webapi

    Akhil Mittal 撰写的《*** Web API》是一本深入探讨如何创建.NET平台上的RESTful服务的实用指南。Akhil Mittal 是一位资深分析师和微软MVP(最有价值专家),他在本书中分享了他在.NET领域的深厚经验。 在本书中,...

    前端开源库-diving-squirrel

    前端开源库-diving-squirrel潜水松鼠,简单网络应用的框架。它使用了Express服务器和Jade模板系统。

    Packt.Mastering.Csharp.and.NET.Programming

    - **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

    在“Module-7-Diving-Deeper-Into-React-Component”这个模块中,我们将深入探讨React组件这一核心概念,它是构建React应用的基础。React组件是JavaScript函数或类,它们接收输入(称为“props”)并返回React元素,...

    Diving Deep Into Kubernetes Networking.pdf

    本书《Diving Deep Into Kubernetes Networking》介绍与Kubernetes相关的各种网络概念,对于操作员、开发者或决策者可能会发现有用。网络是一个复杂的话题,尤其是当涉及到像Kubernetes这样的分布式系统时。了解技术...

    HAN-OOPD-Diving-For-Treasure:OOPD的学校项目-潜水宝藏(续集)

    在这个名为“HAN-OOPD-Diving-For-Treasure”的项目中,我们可以看到这是一个针对对象导向编程设计(Object-Oriented Programming Design, OOPD)的学校项目,特别是使用Java语言来实现的。这个“续集”可能是指对...

    火车订票系统java源码-Skydiving-Booking-System:跳伞预订系统

    1 - 跳伞预订系统 截止日期:第 5 周,星期一,上午 9 点(10 月 12 日) 价值:15分 宗旨 练习如何应用系统的面向对象设计过程 获得实现具有多个交互类的面向对象程序的经验 了解有关 Java 类库的更多信息 前言 在...

    Learning C++ Functional Programming

    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...

    Hands-on with SharePoint 2010 - Deep Diving With the Experts

    The New Chart Web Part in SharePoint 2010 (Audience: All) ................................................. 19 Creating SharePoint 2010 Workflows in Visio 2010 (Audience: All) ...........................

    Objective-C for Absolute Beginners iPhone, iPad, and Mac Programming Made Easy

    - **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. - **...

    abstract-wal-interfaces:Diving into the Laravel Framework 一书的示例

    在阅读"Diving into the Laravel Framework"这本书时,你可能会学到如何编写单元测试来验证WAL接口的正确性,确保在事务处理过程中数据的完整性和一致性。Laravel的内置测试框架`PHPUnit`可以帮助你编写这些测试,并...

    Cloud Native Programming with Golang

    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 ...

    Learning.Scala.Practical.Functional.Programming.for.the.JVM

    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 ...

    diving-into-directives

    深入了解指令 看演示 官方文档: 一种主要在指令中使用控制器的有趣方法: 更多关于 bindToController: 用于构建此演示文稿的技术: 作为展示框架 用于代码高亮 用于在开发时编译 sass 并捆绑类似 livereload 的...

    Kotlin Programming By Example-Packt Publishing(2018).epub

    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

    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是用于图深度学习研究的交钥匙库。-Python开发

    图深度学习研究库DIG:Dive into Graphs是图深度学习研究的统包库。 DIG:用于图深度学习研究的总库,刘萌*,罗有志*,王丽梅*,谢耀辰*,郝昊*,桂树瑞,赵旭,余海阳,张静屯,刘毅,严克强,宝拉·奥兹特金(Bora...

    PowerShell实现统计函数嵌套深度

    Diving deeper to 1 meters Calculated depth: 1 Diving deeper to 2 meters Calculated depth: 2 ... Diving deeper to 10 meters Calculated depth: 10 ``` 这里可以看出,随着递归深度的增加,`Test-NestLevel`...

Global site tag (gtag.js) - Google Analytics