.NET - Diving into System Programming - Part 1[转贴]
来源:互联网 发布:梦里花落知多少小说网 编辑:程序博客网 时间:2024/04/28 03:01
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace DevClasses
{
///
/// Summary description for Class.
///
class DeviceClasses
{
? ///
? /// 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;
? }
}
}
- .NET - Diving into System Programming - Part 1[转贴]
- .NET - Diving into System Programming - Part 2[转贴]
- Diving into OpenStack Network Architecture - Part 1
- Diving into OpenStack Network Architecture - Part 2 - Basic Use Cases
- Diving into OpenStack Network Architecture - Part 3 - Routing
- Diving into Mesa
- Diving into OpenStack Network Architecture - Part 4 - Connecting to Public Network
- Diving Into Android 'M' Doze
- Diving Into Android 'M' Doze
- Diving into OpenStack Network Architecture
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.1 OVERVIEW OF THE SYSTEM-LEVEL ARCHITECTURE
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.6 SYSTEM INSTRUCTION SUMMARY
- .Net Programming Tutorial Video Collection - Part 1 by rongchaua
- Intel-x86-System-Programming-Guide, Part 1,Chapter 1 ABOUT THIS MANUAL
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.2 MODES OF OPERATION
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.4 MEMORY-MANAGEMENT REGISTERS
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.5 CONTROL REGISTERS
- Intel-x86-System-Programming-Guide, Part 1,Chapter 3.1 MEMORY MANAGEMENT OVERVIEW
- 新的生活今天开始~
- C#取计算机硬件信息
- JDBC性能技巧
- 游七宝滑雪场
- 第一篇文章,第一个blog,第一.......
- .NET - Diving into System Programming - Part 1[转贴]
- CSDN Blog开张大吉,恭喜,恭喜.
- 处理多维数据集的VB6程序
- monkey7.com
- 第一次
- It's just a test!
- 热烈祝贺 CSDN Blog 开通!!!!
- .NET - Diving into System Programming - Part 2[转贴]
- 加班的端午