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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows System Programming, Fourth Edition, now contains extensive new coverage of 64-bit programming, parallelism, multicore systems, and many other crucial topics. Johnson Hart’s robust code examples have been updated and streamlined throughout. They have been debugged and tested in both 32-bit and 64-bit versions, on single and multiprocessor systems, and under Windows 7, Vista, Server 2008, and Windows XP. To clarify program operation, sample programs are now illustrated with dozens of screenshots. Hart systematically covers Windows externals at the API level, presenting practical coverage of all the services Windows programmers need, and emphasizing how Windows functions actually behave and interact in real-world applications. Hart begins with features used in single-process applications and gradually progresses to more sophisticated functions and multithreaded environments. Topics covered include file systems, memory management, exceptions, processes, threads, synchronization, interprocess communication, Windows services, and security. New coverage in this edition includes * Leveraging parallelism and maximizing performance in multicore systems * Promoting source code portability and application interoperability across Windows, Linux, and UNIX * Using 64-bit address spaces and ensuring 64-bit/32-bit portability * Improving performance and scalability using threads, thread pools, and completion ports * Techniques to improve program reliability and performance in all systems * Windows performance-enhancing API features available starting with Windows Vista, such as slim reader/writer locks and condition variables

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值