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

转载 2004年06月23日 08:41:00

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 2[转贴]

using System;using System.Text;using System.Runtime.InteropServices;namespace DevInfo{??? class Devi...
  • kongxx
  • kongxx
  • 2004年06月23日 08:48
  • 2390

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
  • 540

Week 1 Assignment - Wordnet - Princeton - Algorithms Part II

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

An Introduction to Interactive Programming in Python

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

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
  • 2911

Diving into OpenStack Network Architecture - Part 1

From https://blogs.oracle.com/ronen/entry/diving_into_openstack_network_architecture Diving int...
  • yanyun0050
  • yanyun0050
  • 2015年11月13日 16:20
  • 222

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

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression 1  Linear regression with one variable ...
  • E_pen
  • E_pen
  • 2015年02月03日 23:10
  • 3302

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
  • 112

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
  • 844

Multicore Programming OpenMP: Part 1

多核共享内存平台multicore shared-memory platform OpenMP介绍多核共享内存API OMP介绍parallel region 创建线程 同步synchronizati...
  • c602273091
  • c602273091
  • 2017年02月01日 13:15
  • 756
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:.NET - Diving into System Programming - Part 1[转贴]
举报原因:
原因补充:

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