using System.Runtime.InteropServices;
using System.Management;
using System;

public class HardwareInfo
{
    //取机器名
    public string GetHostName()
    {
    return System.Net.Dns.GetHostName();
    }

//取CPU编号
    public String GetCpuID()
    {
        try
        {
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();

String strCpuID = null ;
            foreach( ManagementObject mo in moc )
            {
                strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return strCpuID;
        }
        catch
        {
            return "";
        }

}//end method

//取第一块硬盘编号
    public String GetHardDiskID()
    {
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            String strHardDiskID = null ;
            foreach(ManagementObject mo in searcher.Get())
            {
                strHardDiskID = mo["SerialNumber"].ToString().Trim();
                break;
            }
            return strHardDiskID ;
        }
        catch
        {
            return "";
        }
    }//end

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

//取网卡mac
    public string GetMacAddress()
    {
        string addr="";
        try
        {
            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]);
                }
            }
        }
        catch
        {
        }
        return addr.Replace(' ', '0');
    }
}

出处:https://www.cnblogs.com/MaxWoods/archive/2008/03/11/1100666.html

C# 获取机器码的更多相关文章

  1. C#获取机器码(转)

    /// <summary>    /// 机器码    /// </summary>   public class MachineCode    {        ///   ...

  2. 【C#】获取机器码MachineCode

    需求:机器码可以用于校验用户是否用的同一台电脑登录,比如在别的机器上登录时做强制下线(踢人下线).通常在用户注册时,计算一次用户的机器码跟随注册信息一起发送给服务器. 机器码的作用看百度百科: 定义规 ...

  3. python获取机器信息脚本(网上寻找的)

    获取机器信息(待测试) # -*- coding: UTF-8 -*- import psutil import json import os import socket import struct ...

  4. 198个经典C#WinForm实例源码(超赞) 里面的例子 .sln 目录

    \-窗体技巧\QQ窗体\QQFrm.sln; \-窗体技巧\仿XP系统的任务栏菜单\仿XP系统的任务栏菜单.sln; \-窗体技巧\向窗体中拖放图片并显示\向窗体中拖放图片并显示.sln; \-窗体技 ...

  5. ASProtect注册码使用教程|ASProtect SKE(加壳脱壳工具) 2.56 汉化注册版

    ASProtect 是功能非常完善的加壳.加密保护工具.能够在对软件加壳的同时进行各种保护.如:反调试跟踪.自校验及用密钥加密保护等:还有多种限制使用措施,如:使用天数限制.次数限制及对应的注册提醒信 ...

  6. .NET破解之太乐地图下载器【非暴破】

    不知不觉,接触破解逆向已经三个月了,从当初的门外汉到现在的小白,这个过程只有经历过才知道其中的苦与乐: 有无知.困惑.痛苦.惊喜.彻悟.欣慰…… 有无助的软件脱壳,茫然的代码分析,有无趣的反复测试, ...

  7. zabbix添加邮件报警机制

    zabbix添加邮件报警机制 作者:尹正杰 还记得之前跟大家聊过的一个如何监控一个目录的话题吗?我们虽然监控出来数据了,也有数据了,但是,只是监控也没有用啊~因为我们不能24小时盯着屏幕然后 出了事情 ...

  8. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

  9. android文件和图片的处理工具类(一)

    package com.gzcivil.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileO ...

随机推荐

  1. Linux7 下重新安装YUM

    所有操作均在ROOT用户下,系统版本是Linux7.0 X86_64: 一.删除原有YUM # rpm -aq|grep yum|xargs rpm -e --nodeps 二.下载yum,注意自己的 ...

  2. css经验之谈

    //清除浮动 .clear { zoom:1; }.clear:after { content:''; display:block; clear:both; }.fl { float:left; }. ...

  3. win 10 初始环境变量

    有时用户会修改Win10系统的环境变量,改到后面原来是什么的也记不得了,想要改回去还要去别的电脑查看,这里转载下Win10 64位环境变量的默认初始值. 附:打开环境变量方法:电脑左下右键——系统—— ...

  4. ubantu 安装git

    1.安装git并配置 sudo add-apt-repository ppa:git-core/ppa sudo apt-get update sudo apt-get install git 可以使 ...

  5. 利用python库计算person相关系数

    使用numpy库,可以实现person相关系数的计算,例如对于矩阵a. a Out[235]: array([[1, 1, 2, 2, 3], [2, 2, 3, 3, 5], [1, 4, 2, 2 ...

  6. golang使用ssl自签证书通信

    证书是自签名生成的,另外lets encrypt证书免费发放,而且众多大厂都已经开始支持了,不过这只是个例子,无所谓验证有效和权威性了 服务器端 package main import ( " ...

  7. 使用Sphinx为你的python模块自动生成文档

    Sphinx是一个可以用于Python的自动文档生成工具,可以自动的把docstring转换为文档,并支持多种输出格式包括html,latex,pdf等. 安装 创建一个sphinx项目 下面的命令会 ...

  8. Ajax的简单介绍与使用

    1.什么是Ajax? Ajax(Asynchronous JavaScript and XML),简单说就是不需要刷新当前页面而实现javaScript和和后台服务器交换数据以更新网页中的部分内容. ...

  9. php操作mysql几个常用操作

    1.链接数据库 mysql_connet('数据库地址','数据库账号','数据库密码'); 2.选择数据库 mysql_select_db("数据库名"); 3.设置编码 mys ...

  10. ORA-15025 搭建DG环境,restore controlfile报错,提示oracle无法使用ASM存储

    环境说明: #主库RAC环境 #备库RAC环境,操作系统AIX 6.1 数据库版本11.2.0.3 报错说明: #主库备份控制文件,传输至备库,备库restore 报错 本篇文档,分为两大阶段:第一阶 ...