朋友mingal急问我有关获取远程网卡MAC地址的ASP.net实现。我一开始以为是获取本机MAC地址, 说了几种方法给他。由于他还需要获取服务器(本机)相关信息, 如硬盘序列号、CPU信息等。于是介绍了个WMI方法给他: using System.Management; string strMac = string.Empty; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { strMac += mo["MacAddress"].ToString() + ""; } }
// This program shows how to use the IPAddress class to obtain a server // IP addressess and related information.using System; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions;namespace Mssc.Services.ConnectionManagement { class TestIPAddress { /** * The IPAddresses method obtains the selected server IP address information. * It then displays the type of address family supported by the server and its * IP address in standard and byte format. **/ private static void IPAddresses(string server) { try { System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
// Get server related information. IPHostEntry heserver = Dns.Resolve(server); // Loop on the AddressList foreach (IPAddress curAdd in heserver.AddressList) { // Display the type of address family supported by the server. If the // server is IPv6-enabled this value is: InternNetworkV6. If the server // is also IPv4-enabled there will be an additional value of InterNetwork. Console.WriteLine("AddressFamily: " + curAdd.AddressFamily.ToString());
// Display the ScopeId property in case of IPV6 addresses. if(curAdd.AddressFamily.ToString() == ProtocolFamily.InterNetworkV6.ToString()) Console.WriteLine("Scope Id: " + curAdd.ScopeId.ToString()); // Display the server IP address in the standard format. In // IPv4 the format will be dotted-quad notation, in IPv6 it will be // in in colon-hexadecimal notation. Console.WriteLine("Address: " + curAdd.ToString());
// Display the server IP address in byte format. Console.Write("AddressBytes: "); Byte[] bytes = curAdd.GetAddressBytes(); for (int i = 0; i < bytes.Length; i++) { Console.Write(bytes[i]); } Console.WriteLine("\r\n"); } } catch (Exception e) { Console.WriteLine("[DoResolve] Exception: " + e.ToString()); } }
// This IPAddressAdditionalInfo displays additional server address information. private static void IPAddressAdditionalInfo() { try { // Display the flags that show if the server supports IPv4 or IPv6 // address schemas. Console.WriteLine("\r\nSupportsIPv4: " + Socket.SupportsIPv4); Console.WriteLine("SupportsIPv6: " + Socket.SupportsIPv6); if (Socket.SupportsIPv6) { // Display the server Any address. This IP address indicates that the server // should listen for client activity on all network interfaces. Console.WriteLine("\r\nIPv6Any: " + IPAddress.IPv6Any.ToString()); // Display the server loopback address. Console.WriteLine("IPv6Loopback: " + IPAddress.IPv6Loopback.ToString());
// Used during autoconfiguration first phase. Console.WriteLine("IPv6None: " + IPAddress.IPv6None.ToString());
mac地址: using System; using System.Collections; using System.Management;namespace ArLi.CommonPrj { public class GetOfMACAddr { public GetOfMACAddr() { } /// <summary>得到网卡 MAC 地址</summary> /// <returns>返回地址数组,字符类型</returns> /* 例: ArrayList myAddr = ArLi.CommonPrj.GetOfMACAddr.GetIt(); for (int i=0; i<myAddr.Count; i++) MessageBox.Show(myAddr[i].ToString());
*/ public static ArrayList GetIt() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); ArrayList strArr = new ArrayList();
//网卡数 foreach(ManagementObject mo in moc) { if((bool)mo["IPEnabled"]) { strArr.Add(mo["MacAddress"].ToString().Replace(":","")); } mo.Dispose(); } return strArr; } } }
C#读硬盘序列号的原代码 ------2002-12-25 11:42:11 public class IDE { [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] internal struct IDSECTOR { public ushort wGenConfig; public ushort wNumCyls; public ushort wReserved; public ushort wNumHeads; public ushort wBytesPerTrack; public ushort wBytesPerSector; public ushort wSectorsPerTrack; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=3)] public ushort [] wVendorUnique; [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=20)] public string sSerialNumber; public ushort wBufferType; public ushort wBufferSize; public ushort wECCSize; [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=8)] public string sFirmwareRev; [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=40)] public string sModelNumber; public ushort wMoreVendorUnique; public ushort wDoubleWordIO; public ushort wCapabilities; public ushort wReserved1; public ushort wPIOTiming; public ushort wDMATiming; public ushort wBS; public ushort wNumCurrentCyls; public ushort wNumCurrentHeads; public ushort wNumCurrentSectorsPerTrack; public uint ulCurrentSectorCapacity; public ushort wMultSectorStuff; public uint ulTotalAddressableSectors; public ushort wSingleWordDMA; public ushort wMultiWordDMA; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=128 )] public byte [] bReserved; } [StructLayout(LayoutKind.Sequential)] internal struct DRIVERSTATUS { public byte bDriverError; public byte bIDEStatus; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=2 )] public byte [] bReserved; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=2 )] public uint [] dwReserved; } [StructLayout(LayoutKind.Sequential)] internal struct SENDCMDOUTPARAMS { public uint cBufferSize; public DRIVERSTATUS DriverStatus; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=513 )] public byte [] bBuffer; } [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] internal struct SRB_IO_CONTROL { public uint HeaderLength; [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=8 )] public string Signature; public uint Timeout; public uint ControlCode; public uint ReturnCode; public uint Length; } [StructLayout(LayoutKind.Sequential)] internal struct IDEREGS { public byte bFeaturesReg; public byte bSectorCountReg; public byte bSectorNumberReg; public byte bCylLowReg; public byte bCylHighReg; public byte bDriveHeadReg; public byte bCommandReg; public byte bReserved; } [StructLayout(LayoutKind.Sequential)] internal struct SENDCMDINPARAMS { public uint cBufferSize; public IDEREGS irDriveRegs; public byte bDriveNumber; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=3 )] public byte [] bReserved; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=4 )] public uint [] dwReserved; public byte bBuffer; } [StructLayout(LayoutKind.Sequential)] internal struct GETVERSIONOUTPARAMS { public byte bVersion; public byte bRevision; public byte bReserved; public byte bIDEDeviceMap; public uint fCapabilities; [ MarshalAs( UnmanagedType.ByValArray, SizeConst=4 )] public uint [] dwReserved; // For future use. } [DllImport("kernel32.dll")] private static extern int CloseHandle(uint hObject); [DllImport("kernel32.dll")] private static extern int DeviceIoControl(uint hDevice, uint dwIoControlCode, ref SENDCMDINPARAMS lpInBuffer, int nInBufferSize, ref SENDCMDOUTPARAMS lpOutBuffer, int nOutBufferSize, ref uint lpbytesReturned, int lpOverlapped); [DllImport("kernel32.dll")] private static extern int DeviceIoControl(uint hDevice, uint dwIoControlCode, int lpInBuffer, int nInBufferSize, ref GETVERSIONOUTPARAMS lpOutBuffer, int nOutBufferSize, ref uint lpbytesReturned, int lpOverlapped); [DllImport("kernel32.dll")] private static extern uint CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, int lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, int hTemplateFile); private const uint GENERIC_READ = 0x80000000; private const uint GENERIC_WRITE = 0x40000000; private const uint FILE_SHARE_READ = 0x00000001; private const uint FILE_SHARE_WRITE = 0x00000002; private const uint OPEN_EXISTING = 3; private const uint INVALID_HANDLE_VALUE = 0xffffffff; private const uint DFP_GET_VERSION = 0x00074080; private const int IDE_ATAPI_IDENTIFY = 0xA1; // Returns ID sector for ATAPI. private const int IDE_ATA_IDENTIFY = 0xEC; // Returns ID sector for ATA. private const int IDENTIFY_BUFFER_SIZE = 512; private const uint DFP_RECEIVE_DRIVE_DATA = 0x0007c088; public static string Read(byte drive) { OperatingSystem os = Environment.OSVersion; if (os.Platform != PlatformID.Win32NT) throw new NotSupportedException("仅支持WindowsNT/2000/XP"); //我没有NT4,请哪位大大测试一下NT4下能不能用 //if (os.Version.Major < 5) throw new NotSupportedException("仅支持WindowsNT/2000/XP"); string driveName = "\\\\.\\PhysicalDrive" + drive.ToString(); uint device = CreateFile(driveName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); if (device == INVALID_HANDLE_VALUE) return ""; GETVERSIONOUTPARAMS verPara = new GETVERSIONOUTPARAMS(); uint bytRv = 0; if (0 != DeviceIoControl(device, DFP_GET_VERSION, 0, 0, ref verPara, Marshal.SizeOf(verPara), ref bytRv, 0)) { if (verPara.bIDEDeviceMap > 0) { byte bIDCmd = (byte)(((verPara.bIDEDeviceMap >> drive & 0x10) != 0) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY); SENDCMDINPARAMS scip = new SENDCMDINPARAMS(); SENDCMDOUTPARAMS scop = new SENDCMDOUTPARAMS(); scip.cBufferSize = IDENTIFY_BUFFER_SIZE; scip.irDriveRegs.bFeaturesReg = 0; scip.irDriveRegs.bSectorCountReg = 1; scip.irDriveRegs.bCylLowReg = 0; scip.irDriveRegs.bCylHighReg = 0; scip.irDriveRegs.bDriveHeadReg = (byte)(0xA0 | ((drive & 1) << 4)); scip.irDriveRegs.bCommandReg = bIDCmd; scip.bDriveNumber = drive; if (0 != DeviceIoControl(device, DFP_RECEIVE_DRIVE_DATA, ref scip, Marshal.SizeOf(scip), ref scop, Marshal.SizeOf(scop), ref bytRv, 0)) { StringBuilder s = new StringBuilder(); for (int i = 20; i < 40; i += 2) { s.Append((char)(scop.bBuffer[i+1])); s.Append((char)scop.bBuffer[i]); } CloseHandle(device); return s.ToString().Trim(); } } } CloseHandle(device); return ""; } } ----------------------www.wenhui.org
用C#取网卡MAC地址的类源码
未知 aspxcn.com 2002-09-19
在下公布这段代码,目的是与大家探讨有无更好的实现方法(如.NET存在直接调用?)。 在下愿与大家共同挖掘C#的精妙之处,一起揭示C#的神秘面纱。 如有不妥,可尽情表达。 1、 [code] public enum NCBCONST { NCBNAMSZ =16, /* absolute length of a net name */ MAX_LANA =254, /* lana's in range 0 to MAX_LANA inclusive */ NCBENUM =0x37, /* NCB ENUMERATE LANA NUMBERS */ NRC_GOODRET =0x00, /* good return */ NCBRESET =0x32, /* NCB RESET */ NCBASTAT =0x33, /* NCB ADAPTER STATUS */ NUM_NAMEBUF =30, /* Number of NAME's BUFFER */ } [StructLayout(LayoutKind.Sequential)] public struct ADAPTER_STATUS { [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)] public byte[] adapter_address; public byte rev_major; public byte reserved0; public byte adapter_type; public byte rev_minor; public ushort duration; public ushort frmr_recv; public ushort frmr_xmit; public ushort iframe_recv_err; public ushort xmit_aborts; public uint xmit_success; public uint recv_success; public ushort iframe_xmit_err; public ushort recv_buff_unavail; public ushort t1_timeouts; public ushort ti_timeouts; public uint reserved1; public ushort free_ncbs; public ushort max_cfg_ncbs; public ushort max_ncbs; public ushort xmit_buf_unavail; public ushort max_dgram_size; public ushort pending_sess; public ushort max_cfg_sess; public ushort max_sess; public ushort max_sess_pkt_size; public ushort name_count; } [StructLayout(LayoutKind.Sequential)] public struct NAME_BUFFER { [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)] public byte[] name; public byte name_num; public byte name_flags; } [StructLayout(LayoutKind.Sequential)] public struct NCB { public byte ncb_command; public byte ncb_retcode; public byte ncb_lsn; public byte ncb_num; public IntPtr ncb_buffer; public ushort ncb_length; [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)] public byte[] ncb_callname; [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)] public byte[] ncb_name; public byte ncb_rto; public byte ncb_sto; public IntPtr ncb_post; public byte ncb_lana_num; public byte ncb_cmd_cplt; [MarshalAs(UnmanagedType.ByValArray, SizeConst=10)] public byte[] ncb_reserve; public IntPtr ncb_event; } [StructLayout(LayoutKind.Sequential)] public struct LANA_ENUM { public byte length; [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.MAX_LANA)] public byte[] lana; } [StructLayout(LayoutKind.Auto)] public struct ASTAT { public ADAPTER_STATUS adapt; [MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NUM_NAMEBUF)] public NAME_BUFFER[] NameBuff; } public class Win32API { [DllImport("NETAPI32.DLL")] public static extern char Netbios(ref NCB ncb); } [/code] 2、 [code] public string GetMacAddress() { string addr=""; int cb; ASTAT adapter; NCB Ncb=new NCB(); char uRetCode; LANA_ENUM lenum; Ncb.ncb_command = (byte)NCBCONST.NCBENUM; cb = Marshal.SizeOf(typeof(LANA_ENUM)); Ncb.ncb_buffer = Marshal.AllocHGlobal(cb); Ncb.ncb_length = (ushort)cb; uRetCode = Win32API.Netbios(ref Ncb); lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM)); Marshal.FreeHGlobal(Ncb.ncb_buffer); if(uRetCode != (short)NCBCONST.NRC_GOODRET) return ""; for(int i=0; i < lenum.length ;i++) { Ncb.ncb_command = (byte)NCBCONST.NCBRESET; Ncb.ncb_lana_num = lenum.lana[i]; uRetCode = Win32API.Netbios(ref Ncb); if(uRetCode != (short)NCBCONST.NRC_GOODRET) return ""; Ncb.ncb_command = (byte)NCBCONST.NCBASTAT; Ncb.ncb_lana_num = lenum.lana[i]; Ncb.ncb_callname[0]=(byte)'*'; cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER))*(int)NCBCONST.NUM_NAMEBUF; Ncb.ncb_buffer = Marshal.AllocHGlobal(cb); Ncb.ncb_length = (ushort)cb; uRetCode = Win32API.Netbios(ref Ncb); adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS)); Marshal.FreeHGlobal(Ncb.ncb_buffer); if (uRetCode == (short)NCBCONST.NRC_GOODRET) { if(i>0) addr += ":"; addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}", adapter.adapt.adapter_address[0], adapter.adapt.adapter_address[1], adapter.adapt.adapter_address[2], adapter.adapt.adapter_address[3], adapter.adapt.adapter_address[4], adapter.adapt.adapter_address[5]); } } return addr.Replace(' ', '0'); } [/code]
朋友mingal急问我有关获取远程网卡MAC地址的ASP.net实现。我一开始以为是获取本机MAC地址,
说了几种方法给他。由于他还需要获取服务器(本机)相关信息,
如硬盘序列号、CPU信息等。于是介绍了个WMI方法给他:
using System.Management;
string strMac = string.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
strMac += mo["MacAddress"].ToString() + "";
}
}
后来才知道他要的是局域网浏览用户的网卡的MAC地址,那可犯难了。
后来找到了相关实现代码,大致是使用地址转换协议进行广播查询的,主要是SendArp这个API:
DWORD SendARP(
IPAddr DestIP, // 目的IP 地址
IPAddr SrcIP, // 源IP地址,可选参数,把它填成0不会有问题
PULONG pMacAddr, // 返回的物理地址
PULONG PhyAddrLen // 物理地址的长度
);
在C#中实现为:
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
private IntPtr getRemoteMAC(string localIP, string remoteIP)
{
Int32 ldest= inet_addr(remoteIP); //目的地的ip
Int32 lhost= inet_addr(localIP); //本地服务器的ip
try
{
Byte[] macinfo=new Byte[6];
IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr;
int ii=SendARP(ldest,lhost, ref mac, ref len);
return mac;
}
catch(Exception err)
{
}
return IntPtr.Zero;
}
可是当把得到的IntPtr类型的MAC地址转换为十六进制时,出现了令人不解的一幕。比如我的网卡MAC地址为00-50-BA-29-22-1A,可是转换后的十六进制却是29BA5000。显然是要每两位反过来排序,但是为什么却缺少了
22-1A ?按道理说得到的结果应该是1A2229BA5000。另外,Arp只能获得同一个网段的,不能跨网段!郁闷ing……,有什么更好的方法???
Tuesday, December 23, 2003 3:01 PM
反馈
# 回复: 获取远程网卡MAC地址 12/23/2003 10:55 PM backend
不应该用IntPtr的。通过它只能访问到Int32(在32位平台上)大小的内容。
下面修改过的C#代码已验证通过(未将网络字节顺序转换为主机字节顺序): [DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32
length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip); static private Int64 getRemoteMAC(string localIP, string remoteIP)
{
Int32 ldest= inet_addr(remoteIP); //目的地的ip
Int32 lhost= inet_addr(localIP); //本地服务器的ip try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0;
} 至于第二个问题,在标准网络协议下,ARP包是不可能跨网段传输的,故想通
过ARP协议是无法查询跨网段设备MAC地址的。
// IP addressess and related information.using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;namespace Mssc.Services.ConnectionManagement
{ class TestIPAddress
{ /**
* The IPAddresses method obtains the selected server IP address information.
* It then displays the type of address family supported by the server and its
* IP address in standard and byte format.
**/
private static void IPAddresses(string server)
{
try
{
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
// Get server related information.
IPHostEntry heserver = Dns.Resolve(server); // Loop on the AddressList
foreach (IPAddress curAdd in heserver.AddressList)
{
// Display the type of address family supported by the server. If the
// server is IPv6-enabled this value is: InternNetworkV6. If the server
// is also IPv4-enabled there will be an additional value of InterNetwork.
Console.WriteLine("AddressFamily: " + curAdd.AddressFamily.ToString());
// Display the ScopeId property in case of IPV6 addresses.
if(curAdd.AddressFamily.ToString() == ProtocolFamily.InterNetworkV6.ToString())
Console.WriteLine("Scope Id: " + curAdd.ScopeId.ToString());
// Display the server IP address in the standard format. In
// IPv4 the format will be dotted-quad notation, in IPv6 it will be
// in in colon-hexadecimal notation.
Console.WriteLine("Address: " + curAdd.ToString());
// Display the server IP address in byte format.
Console.Write("AddressBytes: "); Byte[] bytes = curAdd.GetAddressBytes();
for (int i = 0; i < bytes.Length; i++)
{
Console.Write(bytes[i]);
} Console.WriteLine("\r\n"); } }
catch (Exception e)
{
Console.WriteLine("[DoResolve] Exception: " + e.ToString());
}
}
private static void IPAddressAdditionalInfo()
{
try
{
// Display the flags that show if the server supports IPv4 or IPv6
// address schemas.
Console.WriteLine("\r\nSupportsIPv4: " + Socket.SupportsIPv4);
Console.WriteLine("SupportsIPv6: " + Socket.SupportsIPv6); if (Socket.SupportsIPv6)
{
// Display the server Any address. This IP address indicates that the server
// should listen for client activity on all network interfaces.
Console.WriteLine("\r\nIPv6Any: " + IPAddress.IPv6Any.ToString()); // Display the server loopback address.
Console.WriteLine("IPv6Loopback: " + IPAddress.IPv6Loopback.ToString());
// Used during autoconfiguration first phase.
Console.WriteLine("IPv6None: " + IPAddress.IPv6None.ToString());
Console.WriteLine("IsLoopback(IPv6Loopback): " + IPAddress.IsLoopback(IPAddress.IPv6Loopback));
}
Console.WriteLine("IsLoopback(Loopback): " + IPAddress.IsLoopback(IPAddress.Loopback));
}
catch (Exception e)
{
Console.WriteLine("[IPAddresses] Exception: " + e.ToString());
}
}
http://www.hotop100.com/bbsxp/ShowPost.asp?id=42
http://dotnet.aspx.cc/ShowDetail.aspx?id=6977HLMY-ELPN-4KIR-BI89-7YS2LNENT5HR
using System;
using System.Collections;
using System.Management;namespace ArLi.CommonPrj
{
public class GetOfMACAddr
{
public GetOfMACAddr()
{
} /// <summary>得到网卡 MAC 地址</summary>
/// <returns>返回地址数组,字符类型</returns>
/* 例: ArrayList myAddr = ArLi.CommonPrj.GetOfMACAddr.GetIt();
for (int i=0; i<myAddr.Count; i++) MessageBox.Show(myAddr[i].ToString());
*/
public static ArrayList GetIt() {
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
ArrayList strArr = new ArrayList();
//网卡数
foreach(ManagementObject mo in moc) {
if((bool)mo["IPEnabled"]) {
strArr.Add(mo["MacAddress"].ToString().Replace(":",""));
}
mo.Dispose();
} return strArr;
}
}
}
------2002-12-25 11:42:11 public class IDE
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
internal struct IDSECTOR
{
public ushort wGenConfig;
public ushort wNumCyls;
public ushort wReserved;
public ushort wNumHeads;
public ushort wBytesPerTrack;
public ushort wBytesPerSector;
public ushort wSectorsPerTrack;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=3)]
public ushort [] wVendorUnique;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=20)]
public string sSerialNumber;
public ushort wBufferType;
public ushort wBufferSize;
public ushort wECCSize;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=8)]
public string sFirmwareRev;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=40)]
public string sModelNumber;
public ushort wMoreVendorUnique;
public ushort wDoubleWordIO;
public ushort wCapabilities;
public ushort wReserved1;
public ushort wPIOTiming;
public ushort wDMATiming;
public ushort wBS;
public ushort wNumCurrentCyls;
public ushort wNumCurrentHeads;
public ushort wNumCurrentSectorsPerTrack;
public uint ulCurrentSectorCapacity;
public ushort wMultSectorStuff;
public uint ulTotalAddressableSectors;
public ushort wSingleWordDMA;
public ushort wMultiWordDMA;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=128 )]
public byte [] bReserved;
} [StructLayout(LayoutKind.Sequential)]
internal struct DRIVERSTATUS
{
public byte bDriverError;
public byte bIDEStatus;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=2 )]
public byte [] bReserved;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=2 )]
public uint [] dwReserved;
} [StructLayout(LayoutKind.Sequential)]
internal struct SENDCMDOUTPARAMS
{
public uint cBufferSize;
public DRIVERSTATUS DriverStatus;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=513 )]
public byte [] bBuffer;
} [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
internal struct SRB_IO_CONTROL
{
public uint HeaderLength;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=8 )]
public string Signature;
public uint Timeout;
public uint ControlCode;
public uint ReturnCode;
public uint Length;
} [StructLayout(LayoutKind.Sequential)]
internal struct IDEREGS
{
public byte bFeaturesReg;
public byte bSectorCountReg;
public byte bSectorNumberReg;
public byte bCylLowReg;
public byte bCylHighReg;
public byte bDriveHeadReg;
public byte bCommandReg;
public byte bReserved;
} [StructLayout(LayoutKind.Sequential)]
internal struct SENDCMDINPARAMS
{
public uint cBufferSize;
public IDEREGS irDriveRegs;
public byte bDriveNumber;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=3 )]
public byte [] bReserved;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=4 )]
public uint [] dwReserved;
public byte bBuffer;
} [StructLayout(LayoutKind.Sequential)]
internal struct GETVERSIONOUTPARAMS
{
public byte bVersion;
public byte bRevision;
public byte bReserved;
public byte bIDEDeviceMap;
public uint fCapabilities;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=4 )]
public uint [] dwReserved; // For future use.
} [DllImport("kernel32.dll")]
private static extern int CloseHandle(uint hObject); [DllImport("kernel32.dll")]
private static extern int DeviceIoControl(uint hDevice,
uint dwIoControlCode,
ref SENDCMDINPARAMS lpInBuffer,
int nInBufferSize,
ref SENDCMDOUTPARAMS lpOutBuffer,
int nOutBufferSize,
ref uint lpbytesReturned,
int lpOverlapped); [DllImport("kernel32.dll")]
private static extern int DeviceIoControl(uint hDevice,
uint dwIoControlCode,
int lpInBuffer,
int nInBufferSize,
ref GETVERSIONOUTPARAMS lpOutBuffer,
int nOutBufferSize,
ref uint lpbytesReturned,
int lpOverlapped); [DllImport("kernel32.dll")]
private static extern uint CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
int lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
int hTemplateFile); private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
private const uint INVALID_HANDLE_VALUE = 0xffffffff;
private const uint DFP_GET_VERSION = 0x00074080;
private const int IDE_ATAPI_IDENTIFY = 0xA1; // Returns ID sector for ATAPI.
private const int IDE_ATA_IDENTIFY = 0xEC; // Returns ID sector for ATA.
private const int IDENTIFY_BUFFER_SIZE = 512;
private const uint DFP_RECEIVE_DRIVE_DATA = 0x0007c088; public static string Read(byte drive)
{
OperatingSystem os = Environment.OSVersion;
if (os.Platform != PlatformID.Win32NT) throw new NotSupportedException("仅支持WindowsNT/2000/XP");
//我没有NT4,请哪位大大测试一下NT4下能不能用
//if (os.Version.Major < 5) throw new NotSupportedException("仅支持WindowsNT/2000/XP"); string driveName = "\\\\.\\PhysicalDrive" + drive.ToString();
uint device = CreateFile(driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0, OPEN_EXISTING, 0, 0);
if (device == INVALID_HANDLE_VALUE) return "";
GETVERSIONOUTPARAMS verPara = new GETVERSIONOUTPARAMS();
uint bytRv = 0; if (0 != DeviceIoControl(device, DFP_GET_VERSION,
0, 0, ref verPara, Marshal.SizeOf(verPara),
ref bytRv, 0))
{
if (verPara.bIDEDeviceMap > 0)
{
byte bIDCmd = (byte)(((verPara.bIDEDeviceMap >> drive & 0x10) != 0) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY);
SENDCMDINPARAMS scip = new SENDCMDINPARAMS();
SENDCMDOUTPARAMS scop = new SENDCMDOUTPARAMS(); scip.cBufferSize = IDENTIFY_BUFFER_SIZE;
scip.irDriveRegs.bFeaturesReg = 0;
scip.irDriveRegs.bSectorCountReg = 1;
scip.irDriveRegs.bCylLowReg = 0;
scip.irDriveRegs.bCylHighReg = 0;
scip.irDriveRegs.bDriveHeadReg = (byte)(0xA0 | ((drive & 1) << 4));
scip.irDriveRegs.bCommandReg = bIDCmd;
scip.bDriveNumber = drive; if (0 != DeviceIoControl(device, DFP_RECEIVE_DRIVE_DATA,
ref scip, Marshal.SizeOf(scip), ref scop,
Marshal.SizeOf(scop), ref bytRv, 0))
{
StringBuilder s = new StringBuilder();
for (int i = 20; i < 40; i += 2)
{
s.Append((char)(scop.bBuffer[i+1]));
s.Append((char)scop.bBuffer[i]);
}
CloseHandle(device);
return s.ToString().Trim();
}
}
}
CloseHandle(device);
return "";
}
}
----------------------www.wenhui.org
未知 aspxcn.com 2002-09-19
在下公布这段代码,目的是与大家探讨有无更好的实现方法(如.NET存在直接调用?)。
在下愿与大家共同挖掘C#的精妙之处,一起揭示C#的神秘面纱。 如有不妥,可尽情表达。
1、
[code]
public enum NCBCONST
{
NCBNAMSZ =16, /* absolute length of a net name */
MAX_LANA =254, /* lana's in range 0 to MAX_LANA inclusive */
NCBENUM =0x37, /* NCB ENUMERATE LANA NUMBERS */
NRC_GOODRET =0x00, /* good return */
NCBRESET =0x32, /* NCB RESET */
NCBASTAT =0x33, /* NCB ADAPTER STATUS */
NUM_NAMEBUF =30, /* Number of NAME's BUFFER */
} [StructLayout(LayoutKind.Sequential)]
public struct ADAPTER_STATUS
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public byte[] adapter_address;
public byte rev_major;
public byte reserved0;
public byte adapter_type;
public byte rev_minor;
public ushort duration;
public ushort frmr_recv;
public ushort frmr_xmit;
public ushort iframe_recv_err;
public ushort xmit_aborts;
public uint xmit_success;
public uint recv_success;
public ushort iframe_xmit_err;
public ushort recv_buff_unavail;
public ushort t1_timeouts;
public ushort ti_timeouts;
public uint reserved1;
public ushort free_ncbs;
public ushort max_cfg_ncbs;
public ushort max_ncbs;
public ushort xmit_buf_unavail;
public ushort max_dgram_size;
public ushort pending_sess;
public ushort max_cfg_sess;
public ushort max_sess;
public ushort max_sess_pkt_size;
public ushort name_count;
} [StructLayout(LayoutKind.Sequential)]
public struct NAME_BUFFER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
public byte[] name;
public byte name_num;
public byte name_flags;
} [StructLayout(LayoutKind.Sequential)]
public struct NCB
{
public byte ncb_command;
public byte ncb_retcode;
public byte ncb_lsn;
public byte ncb_num;
public IntPtr ncb_buffer;
public ushort ncb_length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
public byte[] ncb_callname;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
public byte[] ncb_name;
public byte ncb_rto;
public byte ncb_sto;
public IntPtr ncb_post;
public byte ncb_lana_num;
public byte ncb_cmd_cplt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public byte[] ncb_reserve;
public IntPtr ncb_event;
} [StructLayout(LayoutKind.Sequential)]
public struct LANA_ENUM
{
public byte length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.MAX_LANA)]
public byte[] lana;
} [StructLayout(LayoutKind.Auto)]
public struct ASTAT
{
public ADAPTER_STATUS adapt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NUM_NAMEBUF)]
public NAME_BUFFER[] NameBuff;
}
public class Win32API
{
[DllImport("NETAPI32.DLL")]
public static extern char Netbios(ref NCB ncb);
}
[/code] 2、
[code]
public string GetMacAddress()
{
string addr="";
int cb;
ASTAT adapter;
NCB Ncb=new NCB();
char uRetCode;
LANA_ENUM lenum; Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
cb = Marshal.SizeOf(typeof(LANA_ENUM));
Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
Ncb.ncb_length = (ushort)cb;
uRetCode = Win32API.Netbios(ref Ncb);
lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
Marshal.FreeHGlobal(Ncb.ncb_buffer);
if(uRetCode != (short)NCBCONST.NRC_GOODRET)
return ""; for(int i=0; i < lenum.length ;i++)
{
Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Win32API.Netbios(ref Ncb);
if(uRetCode != (short)NCBCONST.NRC_GOODRET)
return ""; Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
Ncb.ncb_callname[0]=(byte)'*';
cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER))*(int)NCBCONST.NUM_NAMEBUF;
Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
Ncb.ncb_length = (ushort)cb;
uRetCode = Win32API.Netbios(ref Ncb);
adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
Marshal.FreeHGlobal(Ncb.ncb_buffer); if (uRetCode == (short)NCBCONST.NRC_GOODRET)
{
if(i>0)
addr += ":";
addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
adapter.adapt.adapter_address[0],
adapter.adapt.adapter_address[1],
adapter.adapt.adapter_address[2],
adapter.adapt.adapter_address[3],
adapter.adapt.adapter_address[4],
adapter.adapt.adapter_address[5]);
}
}
return addr.Replace(' ', '0');
}
[/code]
微软 aspxcn.com 2002-09-30
如果要想获得远程的地址,需要用sendarp这个函数来实现。具体的代码如下:
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);Int32 ldest= inet_addr("157.60.68.163");//目的地的ip
Int32 lhost= inet_addr("157.60.68.33");//本地的iptry
{
Byte[] macinfo=new Byte[6];
Int32 length=6;
IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr(6);
int ii=SendARP(ldest,lhost, ref mac, ref len);
Console.WriteLine("Mac Add:"+mac);
Console.WriteLine("length:"+len);
}
catch(Exception err)
{
Console.WriteLine(err);
}