[访问系统] Api_Win32_Mac类工具包 (转载)

点击下载 Api_Win32_Mac.zip

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace BaseFunction
{class API{[DllImport("kernel32")]//内存public static extern void GlobalMemoryStatus(ref  MEMORY_INFO meminfo);[StructLayout(LayoutKind.Sequential)]public struct CPU_INFO{public uint dwOemId;public uint dwPageSize;public uint lpMinimumApplicationAddress;public uint lpMaximumApplicationAddress;public uint dwActiveProcessorMask;public uint dwNumberOfProcessors;public uint dwProcessorType;public uint dwAllocationGranularity;public uint dwProcessorLevel;public uint dwProcessorRevision;}//定义内存的信息结构  
        [StructLayout(LayoutKind.Sequential)]public struct MEMORY_INFO{public uint dwLength;public uint dwMemoryLoad;public uint dwTotalPhys;public uint dwAvailPhys;public uint dwTotalPageFile;public uint dwAvailPageFile;public uint dwTotalVirtual;public uint dwAvailVirtual;}  [DllImport("kernel32",EntryPoint="GetComputerName",ExactSpelling=false,SetLastError=true)]//计算机名称public static extern bool GetComputerName([MarshalAs(UnmanagedType.LPArray)]byte[] IpBuffer,[MarshalAs(UnmanagedType.LPArray)]Int32[] nSize);[DllImport("advapi32", EntryPoint = "GetUserName", ExactSpelling = false, SetLastError = true)]//计算机用户名public static extern bool GetUserName([MarshalAs(UnmanagedType.LPArray)]byte[] IpBuffer, [MarshalAs(UnmanagedType.LPArray)]Int32[] nSize);[DllImport("Iphlpapi.dll")]public static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);[DllImport("Ws2_32.dll")]public static extern Int32 inet_addr(string ip);}
}

Win32类

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;namespace BaseFunction
{/// <summary>/// 执行需要调用 <b>Win32</b> API 的操作辅助类。/// </summary>
    [SuppressUnmanagedCodeSecurity()]public static partial class Win32{#region 方法/// <summary>/// 执行获取当前运行的操作系统版本。/// </summary>/// <returns><see cref="Platform"/> 的值之一,他表示当前运行的操作系统版本。</returns>private static Platform GetCurrentPlatform(){OperatingSystem os = Environment.OSVersion;Platform pt;switch(os.Platform){case (PlatformID.Win32Windows): // Win95, Win98 or Meswitch(os.Version.Minor){case (0): // 95pt = Platform.Windows95;break;case (10): // 98if(os.Version.Revision.ToString() == "2222A")pt = Platform.Windows982ndEdition;elsept = Platform.Windows98;break;case (90): // winmept = Platform.WindowsME;break;default: // Unknownpt = Platform.UnKnown;break;}break;case (PlatformID.Win32NT): //Win2k or Xp or 2003switch(os.Version.Major){case (3):pt = Platform.WindowsNT351;break;case (4):pt = Platform.WindowsNT40;break;case (5):if(os.Version.Minor == 0)pt = Platform.Windows2000;else if(os.Version.Minor == 1)pt = Platform.WindowsXP;else if(os.Version.Minor == 2)pt = Platform.Windows2003;elsept = Platform.UnKnown;break;case (6):pt = Platform.WindowsVista;break;default:pt = Platform.UnKnown;break;}break;case (PlatformID.WinCE): // WinCEpt = Platform.WindowsCE;break;case (PlatformID.Win32S):case (PlatformID.Unix):default:pt = Platform.UnKnown;break;}return pt;}/// <summary>/// 表示操作系统平台。/// </summary>private enum Platform : byte{/// <summary>/// Windows 95 操作系统./// </summary>
            Windows95,/// <summary>/// Windows 98 操作系统./// </summary>
            Windows98,/// <summary>/// Windows 98 第二版操作系统./// </summary>
            Windows982ndEdition,/// <summary>/// Windows ME 操作系统./// </summary>
            WindowsME,/// <summary>/// Windows NT 3.51 操作系统./// </summary>
            WindowsNT351,/// <summary>/// Windows NT 4.0 操作系统./// </summary>
            WindowsNT40,/// <summary>/// Windows 2000 操作系统./// </summary>
            Windows2000,/// <summary>/// Windows XP 操作系统./// </summary>
            WindowsXP,/// <summary>/// Windows 2003 操作系统./// </summary>
            Windows2003,/// <summary>/// Windows Vista 操作系统./// </summary>
            WindowsVista,/// <summary>/// Windows CE 操作系统./// </summary>
            WindowsCE,/// <summary>/// 操作系统版本未知。/// </summary>
            UnKnown}/// <summary>/// 表示IDE设备错误状态代码的常量与数值的对应。/// </summary>/// <remarks>其数值与常量定义在 <b>WinIoCtl.h</b> 文件中。</remarks>private enum DriverError : byte{/// <summary>/// 设备无错误。/// </summary>SMART_NO_ERROR = 0, // No error/// <summary>/// 设备IDE控制器错误。/// </summary>SMART_IDE_ERROR = 1, // Error from IDE controller/// <summary>/// 无效的命令标记。/// </summary>SMART_INVALID_FLAG = 2, // Invalid command flag/// <summary>/// 无效的命令数据。/// </summary>SMART_INVALID_COMMAND = 3, // Invalid command byte/// <summary>/// 缓冲区无效(如缓冲区为空或地址错误)。/// </summary>SMART_INVALID_BUFFER = 4, // Bad buffer (null, invalid addr..)/// <summary>/// 设备编号错误。/// </summary>SMART_INVALID_DRIVE = 5, // Drive number not valid/// <summary>/// IOCTL错误。/// </summary>SMART_INVALID_IOCTL = 6, // Invalid IOCTL/// <summary>/// 无法锁定用户的缓冲区。/// </summary>SMART_ERROR_NO_MEM = 7, // Could not lock user's buffer/// <summary>/// 无效的IDE注册命令。/// </summary>SMART_INVALID_REGISTER = 8, // Some IDE Register not valid/// <summary>/// 无效的命令设置。/// </summary>SMART_NOT_SUPPORTED = 9, // Invalid cmd flag set/// <summary>/// 指定要查找的设别索引号无效。/// </summary>SMART_NO_IDE_DEVICE = 10}public static void ChangeByteOrder(byte[] charArray){byte temp;for(int i = 0; i < charArray.Length; i += 2){temp = charArray;charArray = charArray[i + 1];charArray[i + 1] = temp;}}/// <summary>/// 根据指定的设备信息生成设备的详细信息。/// </summary>/// <param name="phdinfo">一个 <see cref="IdSector"/></param>/// <returns></returns>private static HDiskInfo GetHardDiskInfo(IdSector phdinfo){HDiskInfo hdd = new HDiskInfo();hdd.ModuleNumber = Encoding.ASCII.GetString(phdinfo.sModelNumber).Trim();hdd.Firmware = Encoding.ASCII.GetString(phdinfo.sFirmwareRev).Trim();hdd.SerialNumber = Encoding.ASCII.GetString(phdinfo.sSerialNumber).Trim();hdd.Capacity = phdinfo.ulTotalAddressableSectors / 2 / 1024;hdd.BufferSize = phdinfo.wBufferSize / 1024;return hdd;}/// <summary>/// 获取在NT平台下指定序列号的硬盘信息。/// </summary>/// <param name="driveIndex">物理磁盘的数量。</param>/// <returns></returns>private static HDiskInfo GetHddInfoNT(byte driveIndex){GetVersionOutParams vers = new GetVersionOutParams();SendCmdInParams inParam = new SendCmdInParams();SendCmdOutParams outParam = new SendCmdOutParams();uint bytesReturned = 0;// 使用 Win2000 或 Xp下的方法获取硬件信息// 获取设备的句柄。IntPtr hDevice =CreateFile(string.Format(@"\\.\PhysicalDrive{0}", driveIndex), GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);// 开始检查if(hDevice == IntPtr.Zero)throw new UnauthorizedAccessException("执行 Win32 API 函数 CreateFile 失败。");if(0 == DeviceIoControl(hDevice, SMART_GET_VERSION, IntPtr.Zero, 0, ref vers,(uint)Marshal.SizeOf(vers),ref bytesReturned,IntPtr.Zero)){CloseHandle(hDevice);throw new IOException(string.Format(Resources.Win32_DeviceIoControlErr, "SMART_GET_VERSION"));}// 检测IDE控制命令是否支持if(0 == (vers.fCapabilities & 1)){CloseHandle(hDevice);throw new IOException(Resources.Win32_DeviceIoControlNotSupport);}// Identify the IDE drivesif(0 != (driveIndex & 1))inParam.irDriveRegs.bDriveHeadReg = 0xb0;elseinParam.irDriveRegs.bDriveHeadReg = 0xa0;if(0 != (vers.fCapabilities & (16 >> driveIndex))){// We don't detect a ATAPI device.
                CloseHandle(hDevice);throw new IOException(Resources.Win32_DeviceIoControlNotSupport);}elseinParam.irDriveRegs.bCommandReg = 0xec;inParam.bDriveNumber = driveIndex;inParam.irDriveRegs.bSectorCountReg = 1;inParam.irDriveRegs.bSectorNumberReg = 1;inParam.cBufferSize = 512;if(0 == DeviceIoControl(hDevice,SMART_RCV_DRIVE_DATA,ref inParam,(uint)Marshal.SizeOf(inParam),ref outParam,(uint)Marshal.SizeOf(outParam),ref bytesReturned,IntPtr.Zero)){CloseHandle(hDevice);throw new IOException(string.Format(Resources.Win32_DeviceIoControlErr, "SMART_RCV_DRIVE_DATA"));}CloseHandle(hDevice);ChangeByteOrder(outParam.bBuffer.sModelNumber);ChangeByteOrder(outParam.bBuffer.sSerialNumber);ChangeByteOrder(outParam.bBuffer.sFirmwareRev);return GetHardDiskInfo(outParam.bBuffer);}/// <summary>/// /// </summary>/// <param name="driveIndex"></param>/// <returns></returns>private static HDiskInfo GetHddInfo9X(byte driveIndex){GetVersionOutParams vers = new GetVersionOutParams();SendCmdInParams inParam = new SendCmdInParams();SendCmdOutParams outParam = new SendCmdOutParams();uint bytesReturned = 0;IntPtr hDevice = CreateFile(@"\\.\Smartvsd", 0, 0, IntPtr.Zero, CREATE_NEW, 0, IntPtr.Zero);if(hDevice == IntPtr.Zero)throw new UnauthorizedAccessException("打开 smartvsd.vxd 文件失败。");if(0 == DeviceIoControl(hDevice, SMART_GET_VERSION, IntPtr.Zero, 0, ref vers, (uint)Marshal.SizeOf(vers), ref bytesReturned, IntPtr.Zero)){CloseHandle(hDevice);throw new IOException(string.Format(Resources.Win32_DeviceIoControlErr, "SMART_GET_VERSION"));}// 如果 IDE 的鉴定命令不被识别或失败if(0 == (vers.fCapabilities & 1)){CloseHandle(hDevice);throw new IOException(Resources.Win32_DeviceIoControlNotSupport);}if(0 != (driveIndex & 1))inParam.irDriveRegs.bDriveHeadReg = 0xb0;elseinParam.irDriveRegs.bDriveHeadReg = 0xa0;if(0 != (vers.fCapabilities & (16 >> driveIndex))){// 检测出IDE为ATAPI类型,无法处理
                CloseHandle(hDevice);throw new IOException(Resources.Win32_DeviceIoControlNotSupport);}elseinParam.irDriveRegs.bCommandReg = 0xec;inParam.bDriveNumber = driveIndex;inParam.irDriveRegs.bSectorCountReg = 1;inParam.irDriveRegs.bSectorNumberReg = 1;inParam.cBufferSize = BUFFER_SIZE;if(0 == DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, ref inParam, (uint)Marshal.SizeOf(inParam), ref outParam, (uint)Marshal.SizeOf(outParam), ref bytesReturned, IntPtr.Zero)){CloseHandle(hDevice);throw new IOException(string.Format(Resources.Win32_DeviceIoControlErr, "SMART_RCV_DRIVE_DATA"));}// 关闭文件句柄
            CloseHandle(hDevice);ChangeByteOrder(outParam.bBuffer.sModelNumber);ChangeByteOrder(outParam.bBuffer.sSerialNumber);ChangeByteOrder(outParam.bBuffer.sFirmwareRev);return GetHardDiskInfo(outParam.bBuffer);}#endregion#region Win32/// <summary>/// 取得指定窗口的系统菜单的句柄。/// </summary>/// <param name="hwnd">指向要获取系统菜单窗口的 <see cref="IntPtr"/> 句柄。</param>/// <param name="bRevert">获取系统菜单的方式。设置为 <b>true</b>,表示接收原始的系统菜单,否则设置为 <b>false</b></param>/// <returns>指向要获取的系统菜单的 <see cref="IntPtr"/> 句柄。</returns>[DllImport("user32.dll", SetLastError = true)]private static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);/// <summary>/// 获取指定的菜单中条目(菜单项)的数量。/// </summary>/// <param name="hMenu">指向要获取菜单项数量的系统菜单的 <see cref="IntPtr"/> 句柄。</param>/// <returns>菜单中的条目数量</returns>[DllImport("user32.dll", SetLastError = true)]private static extern int GetMenuItemCount(IntPtr hMenu);/// <summary>/// 删除指定的菜单条目。/// </summary>/// <param name="hMenu">指向要移除的菜单的 <see cref="IntPtr"/></param>/// <param name="uPosition">欲改变的菜单条目的标识符。</param>/// <param name="uFlags"></param>/// <returns>非零表示成功,零表示失败。</returns>/// <remarks>/// 如果在 <paramref name="uFlags"/> 中使用了<see cref="MF_BYPOSITION"/> ,则在 <paramref name="uPosition"/> 参数表示菜单项的索引;/// 如果在 <paramref name="uFlags"/> 中使用了 <b>MF_BYCOMMAND</b>,则在 <paramref name="uPosition"/> 中使用菜单项的ID。/// </remarks>[DllImport("user32.dll", SetLastError = true)]private static extern int RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);/// <summary>/// 关闭一个指定的指针对象指向的设备。。/// </summary>/// <param name="hObject">要关闭的句柄 <see cref="IntPtr"/> 对象。</param>/// <returns>成功返回 <b>0</b> ,不成功返回非零值。</returns>[DllImport("kernel32.dll", SetLastError = true)]private static extern int CloseHandle(IntPtr hObject);/// <summary>/// 执行打开/建立资源的功能。/// </summary>/// <param name="lpFileName">指定要打开的设备或文件的名称。</param>/// <param name="dwDesiredAccess">/// <para>Win32 常量,用于控制对设备的读访问、写访问或读/写访问的常数。内容如下表:/// <p><list type="table">/// <listheader>/// <term>名称</term>/// <description>说明</description>/// </listheader>/// <item>/// <term>GENERIC_READ</term><description>指定对设备进行读取访问。</description>/// </item>/// <item>/// <term>GENERIC_WRITE</term><description>指定对设备进行写访问。</description>/// </item>/// <item><term><b>0</b></term><description>如果值为零,则表示只允许获取与一个设备有关的信息。</description></item>/// </list></p>/// </para>/// </param>/// <param name="dwShareMode">指定打开设备时的文件共享模式</param>/// <param name="lpSecurityAttributes"></param>/// <param name="dwCreationDisposition">Win32 常量,指定操作系统打开文件的方式。内容如下表:/// <para><p>/// <list type="table">/// <listheader><term>名称</term><description>说明</description></listheader>/// <item>/// <term>CREATE_NEW</term>/// <description>指定操作系统应创建新文件。如果文件存在,则抛出 <see cref="IOException"/> 异常。</description>/// </item>/// <item><term>CREATE_ALWAYS</term><description>指定操作系统应创建新文件。如果文件已存在,它将被改写。</description></item>/// </list>/// </p></para>/// </param>/// <param name="dwFlagsAndAttributes"></param>/// <param name="hTemplateFile"></param>/// <returns>使用函数打开的设备的句柄。</returns>/// <remarks>/// 本函数可以执行打开或建立文件、文件流、目录/文件夹、物理磁盘、卷、系统控制的缓冲区、磁带设备、/// 通信资源、邮件系统和命名管道。/// </remarks>[DllImport("kernel32.dll", SetLastError = true)]private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode,IntPtr lpSecurityAttributes, uint dwCreationDisposition,uint dwFlagsAndAttributes, IntPtr hTemplateFile);/// <summary>/// 对设备执行指定的操作。/// </summary>/// <param name="hDevice">要执行操作的设备句柄。</param>/// <param name="dwIoControlCode">Win32 API 常数,输入的是以 <b>FSCTL_</b> 为前缀的常数,定义在 /// <b>WinIoCtl.h</b> 文件内,执行此重载方法必须输入 <b>SMART_GET_VERSION</b></param>/// <param name="lpInBuffer">当参数为指针时,默认的输入值是 <b>0</b></param>/// <param name="nInBufferSize">输入缓冲区的字节数量。</param>/// <param name="lpOutBuffer">一个 <b>GetVersionOutParams</b> ,表示执行函数后输出的设备检查。</param>/// <param name="nOutBufferSize">输出缓冲区的字节数量。</param>/// <param name="lpBytesReturned">实际装载到输出缓冲区的字节数量。</param>/// <param name="lpOverlapped">同步操作控制,一般不使用,默认值为 <b>0</b></param>/// <returns>非零表示成功,零表示失败。</returns>[DllImport("kernel32.dll", SetLastError = true)]private static extern int DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer,uint nInBufferSize, ref GetVersionOutParams lpOutBuffer,uint nOutBufferSize, ref uint lpBytesReturned,[Out] IntPtr lpOverlapped);/// <summary>/// 对设备执行指定的操作。/// </summary>/// <param name="hDevice">要执行操作的设备句柄。</param>/// <param name="dwIoControlCode">Win32 API 常数,输入的是以 <b>FSCTL_</b> 为前缀的常数,定义在 /// <b>WinIoCtl.h</b> 文件内,执行此重载方法必须输入 <b>SMART_SEND_DRIVE_COMMAND</b><b>SMART_RCV_DRIVE_DATA</b></param>/// <param name="lpInBuffer">一个 <b>SendCmdInParams</b> 结构,它保存向系统发送的查询要求具体命令的数据结构。</param>/// <param name="nInBufferSize">输入缓冲区的字节数量。</param>/// <param name="lpOutBuffer">一个 <b>SendCmdOutParams</b> 结构,它保存系统根据命令返回的设备相信信息二进制数据。</param>/// <param name="nOutBufferSize">输出缓冲区的字节数量。</param>/// <param name="lpBytesReturned">实际装载到输出缓冲区的字节数量。</param>/// <param name="lpOverlapped">同步操作控制,一般不使用,默认值为 <b>0</b></param>/// <returns>非零表示成功,零表示失败。</returns>[DllImport("kernel32.dll", SetLastError = true)]private static extern int DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, ref SendCmdInParams lpInBuffer,uint nInBufferSize, ref SendCmdOutParams lpOutBuffer,uint nOutBufferSize, ref uint lpBytesReturned,[Out] IntPtr lpOverlapped);#endregion#region 结构/// <summary>/// 保存当前计算机 IDE 设备(硬盘)的硬件信息的结构。/// </summary>
        [Serializable]public struct HDiskInfo{/// <summary>/// 硬盘型号。/// </summary>public string ModuleNumber;/// <summary>/// 硬盘的固件版本。/// </summary>public string Firmware;/// <summary>/// 硬盘序列号。/// </summary>public string SerialNumber;/// <summary>/// 硬盘容量,以M为单位。/// </summary>public uint Capacity;/// <summary>/// 设备缓存大小(以M为单位)。/// </summary>public int BufferSize;}/// <summary>/// 表示使用 <b>DeviceIoControl</b> 函数时保存返回的驱动器硬件信息的结构/// </summary>/// <remarks>>此数据结构定义在 <b>WinIoCtl.h</b> 文件名为 <b>_GETVERSIONINPARAMS</b> 结构中。</remarks>[StructLayout(LayoutKind.Sequential, Pack = 1)]private struct GetVersionOutParams{/// <summary>/// IDE设备的二进制硬件版本。/// </summary>public byte bVersion;/// <summary>/// IDE设备的二进制修订版本。/// </summary>public byte bRevision;/// <summary>/// 此值操作系统没有使用,使用此数据结构时被设置为 <b>0</b>/// </summary>public byte bReserved;/// <summary>/// IDE设备的二进制映射。/// </summary>public byte bIDEDeviceMap;/// <summary>/// IDE设备的二进制容量数据。/// </summary>public uint fCapabilities;/// <summary>/// 保留内容,不使用。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]public uint[] dwReserved; // For future use.
        }/// <summary>/// 一个数据结构,表示使用 <b>DeviceIoControl</b> 函数时发送到操作系统中的命令数据结构 <b>SendCmdInParams</b> 的成员结构。/// 它表示要获取磁盘设备性能参数的具体定义规则。/// </summary>/// <seealso cref="SendCmdInParams"/>/// <remarks>此数据结构定义在 <b>WinIoCtl.h</b> 文件名为 <b>_IDEREGS</b> 中。</remarks>[StructLayout(LayoutKind.Sequential, Pack = 1)]private struct IdeRegs{/// <summary>/// 发送到操作系统的注册命令,此为系统的 <b>SMART Command</b>/// </summary>public byte bFeaturesReg;/// <summary>/// 获取IDE设备扇区数。/// </summary>public byte bSectorCountReg;/// <summary>/// 获取IDE设备编号。/// </summary>public byte bSectorNumberReg;/// <summary>/// 获取IDE设备低端柱面值。/// </summary>public byte bCylLowReg;/// <summary>/// 获取IDE设备高端柱面值。/// </summary>public byte bCylHighReg;/// <summary>/// 获取IDE设备的头信息。/// </summary>public byte bDriveHeadReg;/// <summary>/// 获取IDE设备的真正命令。/// </summary>public byte bCommandReg;/// <summary>/// 保留内容,此值应设置为 <b>0</b>/// </summary>public byte bReserved;}/// <summary>/// 保存执行 <b>DeviceIoControl</b> 函数时向系统提交的执行操作命令。/// </summary>/// <seealso cref="SendCmdInParams"/>/// <remarks>此数据结构定义在 <b>WinIoCtl.h</b> 文件名为 <b>_SENDCMDINPARAMS</b> 中。</remarks>[StructLayout(LayoutKind.Sequential, Pack = 1)]private struct SendCmdInParams{/// <summary>/// 输出的数据缓冲大小。/// </summary>public uint cBufferSize;/// <summary>/// 保存向系统发送的磁盘设备命令的数据结构。/// </summary>public IdeRegs irDriveRegs;/// <summary>/// 希望系统控制的物理磁盘的编号。/// </summary>public byte bDriveNumber;/// <summary>/// 保留的数据,不使用。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]public byte[] bReserved;/// <summary>/// 保留的数据,不使用。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]public uint[] dwReserved;/// <summary>/// 保存当前 <b>SendCmdInParams</b> 结构填充数据后的大小。/// </summary>public byte bBuffer;}/// <summary>/// 当执行 <b>DeviceIoControl</b> 函数后系统返回的 <b>SendCmdOutParams</b> 结构中/// 保存磁盘设备当前错误信息的数据结构。/// </summary>/// <seealso cref="SendCmdInParams"/>/// <remarks>/// 此数据结构定义在 <b>WinIoCtl.h</b> 文件名为 <b>_DRIVERSTATUS</b> 中。/// <para>/// 错误代码如下表:<br />/// <list type="table">/// <listheader>/// <term>名称</term>/// <description>说明</description>/// <item><term>SMART_NO_ERROR = 0</term>/// <description>没有错误。</description></item>/// <item><term>SMART_IDE_ERROR = 1</term>/// <description>IDE控制器错误</description></item>/// <item><term>SMART_INVALID_FLAG = 2</term>/// <description>发送的命令标记无效。</description></item>/// <item><term>SMART_INVALID_COMMAND = 3</term>/// <description>发送的二进制命令无效。</description></item>/// <item><term>SMART_INVALID_BUFFER = 4</term>/// <description>二进制缓存无效(缓存为空或者无效地址)。</description></item>/// <item><term>SMART_INVALID_DRIVE = 5</term>/// <description>物理驱动器编号无效。</description></item>/// <item><term>SMART_INVALID_IOCTL = 6</term>/// <description>无效的IOCTL。</description></item>/// <item><term>SMART_ERROR_NO_MEM =  7</term>/// <description>使用的缓冲区无法锁定。</description></item>/// <item><term>SMART_INVALID_REGISTER = 8</term>/// <description>IDE注册命令无效。</description></item>/// <item><term>SMART_NOT_SUPPORTED = 9</term>/// <description>命令标记设置无效。</description></item>/// <item><term>SMART_NO_IDE_DEVICE = 10</term>/// <description>发送的物理驱动器索引超过限制。</description></item>/// </list>/// </para>/// </remarks>[StructLayout(LayoutKind.Sequential, Pack = 1)]private struct DriverStatus{/// <summary>/// 如果检查的IDE设备发生错误,保存的错误代码,<b>0</b> 表示没有错误。/// </summary>public byte bDriverError;/// <summary>/// IDE设备被注册的错误内容。/// </summary>public byte bIDEStatus;/// <summary>/// 保留的数据,不使用。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]public byte[] bReserved;/// <summary>/// 保留的数据,不使用。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]public uint[] dwReserved;}/// <summary>/// 表示当执行 <b>DeviceIoControl</b> 函数后保存系统根据查询命令返回的磁盘设备信息的数据结构。/// </summary>[StructLayout(LayoutKind.Sequential, Pack = 1)]private struct SendCmdOutParams{/// <summary>/// 表示所有二进制信息的缓存大小。/// </summary>public uint cBufferSize;/// <summary>/// 表示查询到设备的错误信息状态。/// </summary>public DriverStatus DriverStatus;/// <summary>/// 表示系统返回的设备硬件信息的二进制数据结构。/// </summary>public IdSector bBuffer;}/// <summary>/// 当执行 <b>DeviceIoControl</b> 函数后系统返回的 <b>SendCmdOutParams</b> 结构中/// 保存磁盘设备的硬件信息的数据结构。/// </summary>/// <seealso cref="SendCmdInParams"/>[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 512)]private struct IdSector{/// <summary>/// 设备通用配置信息。/// </summary>public ushort wGenConfig;/// <summary>/// 设备的柱面数。/// </summary>public ushort wNumCyls;/// <summary>/// 保留内容,不使用。/// </summary>public ushort wReserved;/// <summary>/// 设备的磁头数目。/// </summary>public ushort wNumHeads;/// <summary>/// 设备的磁道数目。/// </summary>public ushort wBytesPerTrack;/// <summary>/// 设备的扇区数目。/// </summary>public ushort wBytesPerSector;/// <summary>/// 设备厂商设定的扇区磁道数目。/// </summary>public ushort wSectorsPerTrack;/// <summary>/// 设备的出品厂商名称。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]public ushort[] wVendorUnique;/// <summary>/// 设备出品厂商的全球唯一编码。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]public byte[] sSerialNumber;/// <summary>/// 设备的缓存类型。/// </summary>public ushort wBufferType;/// <summary>/// 设备缓存容量(单位是byte)。/// </summary>public ushort wBufferSize;/// <summary>/// 设备的错误检查和纠正(ECC)数据的大小。/// </summary>public ushort wECCSize;/// <summary>/// 设备的固件版本。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]public byte[] sFirmwareRev;/// <summary>/// 设备的型号。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]public byte[] sModelNumber;/// <summary>/// 设备厂商名称的扩展内容(如果有)。/// </summary>public ushort wMoreVendorUnique;/// <summary>/// 设备双指令输入输出模式。/// </summary>public ushort wDoubleWordIO;/// <summary>/// 设备的容量大小(单位Byte)。/// </summary>public ushort wCapabilities;/// <summary>/// 第一个保留的内容,不使用。/// </summary>public ushort wReserved1;/// <summary>/// 设备的PIO模式巡道时间。/// </summary>public ushort wPIOTiming;/// <summary>/// 设备DMA 模式巡道时间。/// </summary>public ushort wDMATiming;/// <summary>/// 设备的总线类型,如SCSI,IDE等。/// </summary>public ushort wBS;/// <summary>/// 设备的当前柱面数量。/// </summary>public ushort wNumCurrentCyls;/// <summary>/// 设备当前磁头数量。/// </summary>public ushort wNumCurrentHeads;/// <summary>/// 设备的当前扇区的磁道数量。/// </summary>public ushort wNumCurrentSectorsPerTrack;/// <summary>/// 设备的当前扇区容量(单位byte)。/// </summary>public uint ulCurrentSectorCapacity;/// <summary>/// 多扇区读写模式支持。/// </summary>public ushort wMultSectorStuff;/// <summary>/// 用户是否可自定义扇区地址(LBA模式)支持。/// </summary>public uint ulTotalAddressableSectors;/// <summary>/// 单指令DMA模式。/// </summary>public ushort wSingleWordDMA;/// <summary>/// 多指令DMA模式。/// </summary>public ushort wMultiWordDMA;/// <summary>/// 保留内容,不使用。/// </summary>[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]public byte[] bReserved;}/// <summary>/// /// </summary>
        [StructLayout(LayoutKind.Sequential)]public struct RECT{/// <summary>/// /// </summary>/// <param name="left"></param>/// <param name="top"></param>/// <param name="right"></param>/// <param name="bottom"></param>public RECT(int left, int top, int right, int bottom){this.left = left;this.top = top;this.right = right;this.bottom = bottom;}/// <summary>/// /// </summary>/// <param name="r"></param>public RECT(Rectangle r){left = r.Left;top = r.Top;right = r.Right;bottom = r.Bottom;}/// <summary>/// /// </summary>public int left;/// <summary>/// /// </summary>public int top;/// <summary>/// /// </summary>public int right;/// <summary>/// /// </summary>public int bottom;/// <summary>/// /// </summary>/// <param name="x"></param>/// <param name="y"></param>/// <param name="width"></param>/// <param name="height"></param>/// <returns></returns>public static RECT FromXYWH(int x, int y, int width, int height){return new RECT(x, y, x + width, y + height);}/// <summary>/// /// </summary>public Size Size{get { return new Size(right - left, bottom - top); }}}/// <summary>/// /// </summary>
        [StructLayout(LayoutKind.Sequential)]public sealed class SCROLLINFO{public SCROLLINFO(){this.cbSize = Marshal.SizeOf(typeof(SCROLLINFO));}public SCROLLINFO(int mask, int min, int max, int page, int pos){this.cbSize = Marshal.SizeOf(typeof(SCROLLINFO));this.fMask = mask;this.nMin = min;this.nMax = max;this.nPage = page;this.nPos = pos;}public int cbSize;public int fMask;public int nMin;public int nMax;public int nPage;public int nPos;public int nTrackPos;}#endregion#region 常量/// <summary>/// Win32 API 常数,指示在使用 <see cref="RemoveMenu"/> 函数时指定使用索引数而不是使用ID。/// </summary>private const int MF_BYPOSITION = 0x00000400;private const uint FILE_SHARE_READ = 0x00000001;private const uint FILE_SHARE_WRITE = 0x00000002;private const uint FILE_SHARE_DELETE = 0x00000004;private const uint SMART_GET_VERSION = 0x00074080; // SMART_GET_VERSIONprivate const uint SMART_SEND_DRIVE_COMMAND = 0x0007c084; // SMART_SEND_DRIVE_COMMANDprivate const uint SMART_RCV_DRIVE_DATA = 0x0007c088; // SMART_RCV_DRIVE_DATAprivate const uint GENERIC_READ = 0x80000000;private const uint GENERIC_WRITE = 0x40000000;private const uint CREATE_NEW = 1;private const uint OPEN_EXISTING = 3;private const uint BUFFER_SIZE = 512;private static readonly Platform currentOs;#endregion}
}

计算机信息类

using System;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;namespace BaseFunction
{///<summary>///计算机信息类///</summary>public  class ComputerInfo{private  string CpuID;private string MacAddress;private string DiskID;private string IpAddress;private string LoginUserName;private string ComputerName;private string SystemType;private string TotalPhysicalMemory; //单位:M private static ComputerInfo _instance;internal static ComputerInfo Instance(){if (_instance == null)_instance = new ComputerInfo();return _instance;}internal ComputerInfo(){CpuID = GetCpuID();MacAddress = GetMacAddress();DiskID = GetDiskID();IpAddress = GetIPAddress();LoginUserName = GetUserName();SystemType = GetSystemType();TotalPhysicalMemory = GetTotalPhysicalMemory();ComputerName = GetComputerName();}/// <summary>/// 浏览器客户端  获取网卡MAC地址MD5加密串  杨旭东/// </summary>/// <returns></returns>public static string GetClientMac(){try{string clientIP =System.Web.HttpContext.Current.Request.UserHostAddress.Trim();Int32 idest = API.inet_addr(clientIP);Int64 macInfo = new Int64();Int32 length = 6;int res = API.SendARP(idest, 0, ref macInfo, ref length);string mac_src = macInfo.ToString("X");if (!string.IsNullOrEmpty(mac_src) && !"0".Equals(mac_src)){while (mac_src.Length < 12){mac_src = mac_src.Insert(0, "0");}string mac_dest = string.Empty;for (int i = 0; i < 11; i++){if (i % 2 == 0){if (i == 10)mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));elsemac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));}}return mac_dest;}}catch{return "0";}return "0";}/// <summary>/// 获取CPU序列号代码 /// </summary>/// <returns></returns>public  static  string GetCpuID(){try{//获取CPU序列号代码 string cpuInfo = "";//cpu序列号 ManagementClass mc = new ManagementClass("Win32_Processor");ManagementObjectCollection moc = mc.GetInstances();foreach (ManagementObject mo in moc){cpuInfo = mo.Properties["ProcessorId"].Value.ToString();}moc = null;mc = null;return cpuInfo;}catch{return "unknow";}finally{}}/// <summary>/// 获取网卡硬件地址 /// </summary>/// <returns></returns>public static string GetMacAddress(){try{//获取网卡硬件地址 return Mac.GetMacAddress();}catch{return "unknow";}finally{}}/// <summary>/// 获取IP地址(IPv4)/// </summary>/// <returns></returns>public static string GetIPAddress(){try{IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());foreach (IPAddress ip in arrIPAddresses){if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))//IPv4
                    {return ip.ToString();}}return "unknow";}catch{return "unknow";}finally{}}/// <summary>/// 获取硬盘ID /// </summary>/// <returns></returns>public static string GetDiskID(){try{return Win32.GetHddInformation().ModuleNumber;}catch{return "unknow";}finally{}}///<summary>///操作系统的登录用户名 ///</summary>///<returns></returns>public static string GetUserName(){try{byte[] userName = new byte[30];Int32[] length = new Int32[1];length[0] = 30;//限定用户名长度
                API.GetUserName(userName, length);return System.Text.Encoding.ASCII.GetString(userName);}catch{return "unknow";}finally{}}///<summary>/// PC类型 ///</summary>///<returns></returns>public static string GetSystemType(){try{string st = "";ManagementClass mc = new ManagementClass("Win32_ComputerSystem");ManagementObjectCollection moc = mc.GetInstances();foreach (ManagementObject mo in moc){st = mo["SystemType"].ToString();}moc = null;mc = null;return st;}catch{return "unknow";}finally{}}///<summary>///物理内存 ///</summary>///<returns></returns>public static string GetTotalPhysicalMemory(){try{API.MEMORY_INFO memoryInfo = new API.MEMORY_INFO();API.GlobalMemoryStatus(ref memoryInfo);return memoryInfo.dwTotalPhys.ToString();}catch{return "unknow";}finally{}}///<summary>/// 获取计算机名称///</summary>///<returns></returns>public static string GetComputerName(){try{byte[] computerName = new byte[30];Int32[] length = new Int32[1];length[0] = 30;//限定计算机名长度
                API.GetComputerName(computerName,length);return System.Text.Encoding.ASCII.GetString(computerName);}catch{return "unknow";}finally{}}}
}

 

转载于:https://www.cnblogs.com/lizeyan/p/3628262.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407971.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

constexpr 函数

constexpr 函数 — C 20 constexpr double pi 3.14;constexpr允许你在编译时使用典型的C函数语法进行编程,但这并不意味之constexpr只和编译期有关 constexpr函数可以在编译期运行,也可以在运行时运行 但在以下情况constexpr函数必须在编译期运行: constexpr函数在编译的上…

constexpr和consteval --- C++ 20

constexpr和consteval — C 20 标准库容器和算法库对constexpr 的应用 C20 中大量的算法和容器可以使用constexpr,这意味着你甚至可以再编译期vector<int>进行排序 Algorithms library - cppreference.com 如下: #include <iostream> #include <ranges>…

函数模板(参考《C++ Templates 英文版第二版》)

函数编程(参考《C Templates 英文版第二版》) Chapter 1 函数模板 1.1 一窥函数模板 template<class T> T max(T a, T b) {return b < a ? a : b; }#include "max1.hpp" #include <iostream> #include <string> #include <format>int…

Curiously Recurring Template Pattern奇怪的模板递归 --- C++20

Curiously Recurring Template Pattern 奇怪的模板递归 — C20 我们都知道C有静态多态和动态多态,动态多态通过虚函数表实现,他的缺点就是对效率产生一点点影响 可以用CRTP解决这个问题 我们先举一个动态多态的例子: #include <iostream> using namespace std;class …

PROJECT #0 - C++ PRIMER [CMU 15-445645]笔记

PROJECT #0 - C PRIMER [CMU 15-445/645]笔记 这是数据库领域的一门课程, 由卡内基梅隆大学副教授Andy Pavlo授课, 目前在网上有授课视频资料、实验以及配套的在线测评环境 (限时开放至2021年12月31日) 环境: wsl2 Clion Project #0 - C Primer 还是很简单的,主要目的是让…

简单JS实现对表的行的增删

这段代码非常的简单&#xff0c;仅仅作为自己的一个小小的记录&#xff01; ok&#xff0c;先上一个简单的图例&#xff0c;效果如下&#xff08;注意&#xff1a;这只是一个简单的例子&#xff0c;不过可以根据这个简单的例子&#xff0c;变化出更为复杂的效果&#xff09;&am…

Yii权限管理工具Srbac使用小结

一、关于Srbac Srbac是基于Yii 框架的 RBAC&#xff08;基于角色的访问控制&#xff09; 插件模块&#xff0c;用于帮助Yii开发人员更方便地进行权限控制&#xff0c;在实际应用过程中也比较方便。 二、安装配置Srbac 在Yii的官方网站的Extensions中已经收录了Srbac插件&#x…

概念concept和requires ---C++ 20

概念concept和requires —C 20 concept concept简化了模板编程的难度 我们可以使用**concept定义模板形参的约束条件T** 模板实力替换T后必须满足std::is_integral_v<C>;为true 例子: requires关键字可以直接约束模板形参T 如下: template <class C> concept …

向DataGridView中添加新的一行数据,可以添加到最后一行或作为第一行

我的开发环境&#xff1a;Microsoft Visual Studio .net 2005 这个程序是Windows Forms Application 新建一个Windows Forms Application项目&#xff0c;打开Form1&#xff0c;在窗体上放一个DataGridView控件和Button,在DataGridView的Columns中添加两列&#xff0c;Name分别…

DataGridView添加一行数据、全选、取消全选、清空数据、删除选中行

.net 2005下的Windows Form Application,一个DataGridView控件和4个Button&#xff0c;界面设置如下&#xff1a; 代码如下&#xff0c;有注解&#xff0c;相信大家都看得明白&#xff1a; using System;using System.Collections.Generic;using System.ComponentModel;using S…

类型萃取类型检查 Type-Traits LibraryType Checks --- C++20

类型萃取:类型检查 Type-Traits Library:Type Checks — C20 Type-Traits library 在C11的时候就已经发布,但依然随着C版本在不断更新 类型检查 Type Checks 每种类型就是十四种主要类型之一 主要类型 template <class T> struct is_void; template <class T>…

模板元编程 Template Metaprogramming--- C++ 20

模板元编程(一) Template Metaprogramming— C 20 在编译期进行类型操作 举个例子: std::move在概念上应该这样实现(实际并不是这么做的): static_cast<std::remove_reference<decltype(arg)>::type&&>(arg);意义上,std::move首先获取它的参数arg,推断…