六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

.NET – 深入系統編程 - Part 1

[摘要].NET – 深入系統編程 - Part 1By Vladimir Afanasyev介紹在我以前的文章里面,我舉了很多系統編程的例子。很多年前,我特別喜歡琢磨系統表格以及系統工具。當然,那時我還沒...
.NET – 深入系統編程 - Part 1
By Vladimir Afanasyev
介紹

在我以前的文章里面,我舉了很多系統編程的例子。很多年前,我特別喜歡琢磨系統表格以及系統工具。當然,那時我還沒有一個真正PC,只是類似于IBM360、PDP11或者microVAX,但是那種感覺不錯。這些年我依然喜歡在COM接口、DLL庫和硬盤配置上作些研究。啊哈,我一直喜歡系統編程!

目的

很多人認為C#只是一個 "child language" 。在我看來,這絕對是錯誤的!為了證明我的觀點,我準備編寫三個關于硬件設備配置的例子,展現如何使用C#和Win32 API(甚至DDK)一起工作。主要是使用P/Invoke來實現。同時C#非常完美的解決了不同平臺數據交流的問題,以及通過Windows Forms ,使我們更容易使用應用程序。

設備類
All devices in the system join in the device classes. As you can see in the below picture, the class has name and Guid (so it can be found in Registry). The class can also have a description. For example, for class "Ports" the description is "Ports (COM & LPT)". Class also has devices that are present in the configuration.
所有的設備信息都存在于設備類中。這個類擁有名字和Guid(因此它可以在注冊表中被找到)。這個類還有一些描述,比如類"Ports"的描述是"Ports (COM & LPT)"。
系統設備管理器給出了所有在PC上存在的設備類的信息:
下面,你將看到使用C#遍歷所有設備的例子,它使用了P/Invoke、DDK和SDK DLLs。

using System;
using System.Runtime.InteropServices;// P/Invoke 必需
using System.Text;


namespace DevClasses
{
/// <summary>
/// Summary description for Class.
/// </summary>
class DeviceClasses
{
/// <summary>
/// The main entry point for the application.
/// </summary>
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 //struct 也可以
{
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;
}
}
}

運行這個應用程序之后,你將看到你機器上的所有設備類。


出自:
http://www.codeproject.com/csharp/DivingSysProg1.asp

譯者的話:
這只是作者一系列文章中的第一章。沒有太多的深入講解知識,只是舉了一個例子而已。
[email protected]
PS:
原文有圖片,這里可能看不到,可以到我的主頁上看這篇文章。
http://home.ripway.com/2004-6/124912/


主站蜘蛛池模板: 日本v片免费一区二区三区 日本vs欧美一区二区三区 | 日本亚洲中午字幕乱码 | 午夜啪啪免费视频 | 四虎在线最新永久免费播放 | 色婷婷久久综合中文久久一本 | 午夜激情影院 | 日韩成人黄色片 | 色婷婷狠狠干 | 日韩在线免费 | 日韩性视频网站 | 日韩欧美亚洲乱码中文字幕 | 日产在线观看 | 午夜不卡av免费 | 一本久道久久综合多人 | 日韩在线视频线视频免费网站 | 欧美一级黄色大片 | 欧美在线区| 尹人久久久香蕉精品 | 午夜福利123| 天天干天天干天天操 | 青草网 | 青青草视频在线观看免费 | 天天综合网天天综合色不卡 | 日本高清免费在线视频 | 综合福利网站 | 青青草偷拍视频 | 伊人久久青青 | 伊人中文字幕在线观看 | 色爱区综合 | 日本午色www高清视频 | 亚洲国产成人va在线观看网址 | 亚拍一区| 午夜免费福利影院 | 三级国产精品一区二区 | 亚洲无套| 亚洲免费看片 | 亚洲欧美风情 | 亚洲欧洲日韩国产 | 天天做天天躁天天躁 | 日本天堂网在线观看 | 午夜高清视频在线观看 |