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

转载 2004年06月23日 08:48:00
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 ");
??????????? 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);
??????? }

??????? }
??? }
}

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

using System;using System.Runtime.InteropServices;using System.Text;namespace DevClasses{/// /// Sum...
  • kongxx
  • kongxx
  • 2004年06月23日 08:41
  • 1536

Algorithm Part I:Programming Assignment(2)

问题描述: Programming Assignment 2: Randomized Queues and Deques Write a generic data type for a d...
  • yao_wust
  • yao_wust
  • 2014年09月22日 21:55
  • 2862

Stanford - Algorithms: Design and Analysis, Part 2 - Week 2 Assignment: Clustering

本周作业是有关Clustering的。。 第一题如下: Question 1 In this programming problem and the next you'll co...
  • u012290414
  • u012290414
  • 2015年04月22日 12:05
  • 1215

An Introduction to Interactive Programming in Python (Part 1) - Week 0

An Introduction to Interactive Programming in Python (Part 1) - Week 0 - Statements, expressions, va...
  • xiewen99
  • xiewen99
  • 2016年09月22日 00:16
  • 538

An Introduction to Interactive Programming in Python

这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲。生动有趣,...
  • mutiantong
  • mutiantong
  • 2014年11月06日 09:58
  • 931

Week 1 Assignment - Wordnet - Princeton - Algorithms Part II

题注 本来想4月2日把手头事情干完了以后回来写一写Princeton大学《Algorithm Part II》的Programming Assignment的,不过今天点开自己的博客,发现有个朋友“...
  • u013590498
  • u013590498
  • 2014年03月30日 20:44
  • 6902

斯坦福大学机器学习公开课---Programming Exercise 2: Logistic Regression

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression---Matlab实现 1 Logistic Regression In this p...
  • E_pen
  • E_pen
  • 2015年02月03日 21:15
  • 2707

Meta Programming System

The Meta Programming System (MPS) is a new programming environment which makes it easy to define you...
  • redvalley
  • redvalley
  • 2005年12月03日 00:14
  • 841

Codeforces Round #453 (Div. 2) C

C. Hashing Trees time limit per test 2 seconds memory limit per test 256 megabytes ...
  • ACM_e
  • ACM_e
  • 2017年12月21日 00:07
  • 110

R Programming: Part 2 - Programming with R

R Programming course notes. This part basically includes control structures, functions, scoping rule...
  • niwatori1217
  • niwatori1217
  • 2016年07月13日 00:24
  • 345
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:.NET - Diving into System Programming - Part 2[转贴]
举报原因:
原因补充:

(最多只允许输入30个字)