delphi 生成网卡MAC地址

生成MAC地址的小工具:

{*------------------------------------------------
  生成mac地址
  @author
  @version  2015.7.2
  2015.10.22修改
  步长最大长度为256,必须为2的n次方
  长度不超过5位数
-------------------------------------------------}
unit frmSaveMac;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, math;

type
  TSaveMacForm = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    SaveDialog1: TSaveDialog;
    SaveButton: TButton;
    ExitButton: TButton;
    Edit7: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Edit8: TEdit;
    Memo1: TMemo;
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    btn_clear: TButton;
    btn_show: TButton;
    Memo2: TMemo;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure ExitButtonClick(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Edit7KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure SaveButtonClick(Sender: TObject);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btn_clearClick(Sender: TObject);
    procedure btn_showClick(Sender: TObject);
  private
    { Private declarations }
    procedure CheckIsEmpty;
    function  CheckStepOrLen: Boolean;
    procedure ShowData();
    Procedure SaveData();
  public
    { Public declarations }
  end;

var
  SaveMacForm: TSaveMacForm;

implementation

{$R *.dfm}
{*------------------------------------------------
  文本框只能输入十六进制数、删除键和回车键
  @param
  @param
-------------------------------------------------}
procedure TSaveMacForm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in [‘0‘..‘9‘, ‘A‘..‘F‘, ‘a‘..‘f‘, #8, #13]) then
  begin
    Key := #0;
    Application.MessageBox (‘只能输入十六进制的数字!‘, ‘提示消息‘, MB_OK + MB_ICONERROR);
  end;
end;

procedure TSaveMacForm.ExitButtonClick(Sender: TObject);
begin
  Close;
end;

{*------------------------------------------------
  文本框按键事件
  @param
  @param
-------------------------------------------------}
procedure TSaveMacForm.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i,j : integer;
begin
  /// 输入最大长度
  for i := 0 to self.ComponentCount -1 do begin         // 循环每个Edit控件 设置最大长度为2   2015.10.22修改
    if self.Components[i] is TEdit then begin
      (self.Components[i] as TEdit).MaxLength := 2;
    end;
    if Sender = Edit7 then Edit7.MaxLength := 3;
    if Sender = Edit8 then Edit8.MaxLength := 5;       // 长度不超过5位数 2015.10.22修改
  end;

  /// 将光标移到最后
  for j := 0 to ComponentCount - 1 do begin
    if Components[j] is TEdit then begin
      (Components[j] as TEdit).SelStart := Length((Components[j] as TEdit).Text);
    end;
  end;

  /// 按回车触发
  if key = 13 then
  begin
    if (sender = Edit1) and (length(Edit1.Text) >= 2) then
      Edit2.SetFocus;
    if (Sender = Edit2) and (length(Edit2.Text) >= 2) then
      Edit3.SetFocus;
    if (Sender = Edit3) and (length(Edit3.Text) >= 2) then
      Edit4.SetFocus;
    if (Sender = Edit4) and (length(Edit4.Text) >= 2) then
      Edit5.SetFocus;
    if (Sender = Edit5) and (length(Edit5.Text) >= 2) then
      Edit6.SetFocus;
    if (Sender = Edit6) then
      Edit7.SetFocus ;
    if (Sender = Edit7) then
      Edit8.SetFocus;
  end;
  ExitButton.Enabled := True;
  SaveButton.Enabled := True;
end;

{*------------------------------------------------
  步长只能输入0到9的数字
  @param
  @param
-------------------------------------------------}
procedure TSaveMacForm.Edit7KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in [‘0‘..‘9‘, #8]) then
    Key := #0;
end;

procedure TSaveMacForm.FormCreate(Sender: TObject);
var
  j: integer;
  C: Double;
begin
  SaveButton.Enabled := False;
  ExitButton.Enabled := False;
  RadioButton1.Checked := True;    /// 默认输出格式

  memo2.Lines.Add(‘256以内的2的n次方:‘);
  for J := 1 to 8 do
  begin
    C := power(2, J);         /// 2的I次方
    memo2.Lines.Add(FloatToStr(C));
  end;
end;

procedure TSaveMacForm.CheckIsEmpty;
begin
  if (Edit1.Text = ‘‘) or (Edit2.Text = ‘‘) or (Edit3.Text = ‘‘) or (Edit4.Text = ‘‘) or (Edit5.Text = ‘‘) or (Edit6.Text = ‘‘) then
  begin
    Application.MessageBox(‘输入框必须是12个16进制的数!‘, ‘提示‘, mb_OK + MB_ICONSTOP);
    exit;
  end;

  if (length(Edit1.Text )< 2) or (length(Edit1.Text) < 2) or (length(Edit1.Text )< 2) or (length(Edit1.Text) < 2) or (length(Edit1.Text) < 2) or (length(Edit1.Text) < 2) then
  begin
    Application.MessageBox(‘每个输入框必须是2个数字!‘, ‘提示‘, MB_OK + MB_ICONERROR);
    exit;
  end;

  if (Edit7.Text = ‘‘) then
  begin
    Application.MessageBox(‘步长不能为空!‘, ‘提示‘, mb_OK);
    Edit7.SetFocus;
    exit;
  end;
  if (Edit8.Text = ‘‘) then
  begin
    Application.MessageBox(‘长度不能为空!‘, ‘提示‘, mb_OK);
    Edit8.SetFocus;
    exit;
  end;
end;

function TSaveMacForm.CheckStepOrLen: Boolean;
var
  step, L, code, J : integer;
  isnot: Boolean;
  C: Extended;
begin
  result := False;
   if Edit7.Text <> ‘‘ then begin       // 2015/10/24 增加,之前没判断,若为空时 会出现系统错误
    step := StrToInt(Edit7.Text);
    L := StrToInt(Edit8.Text);
    if step > 256 then
    begin
      Application.MessageBox(‘步长最大长度为256,请重新输入!‘, ‘提示‘, mb_OK);
      Edit7.SetFocus;
      exit;
    end;
  end;

  // 判断长度
  if L >= 100000 then
  begin
    Application.MessageBox(‘长度太长,请重新输入!‘, ‘提示‘, mb_OK);
    Edit8.SetFocus;
    exit;
  end;

  for J := 1 to 8 do
  begin
    C := power(2, J);         /// 2的I次方
    if (step = C) or (step = 1) then
    begin
      result := True;
    end;
  end;

   /// 判断步长是否满足条件
  if result = False then
  begin
    Application.MessageBox(‘步长只能为2的n次方,请重新输入‘, ‘提示消息‘, mb_OK + MB_ICONERROR);
    Edit7.SetFocus;
    exit;
  end;
end;

procedure TSaveMacForm.btn_showClick(Sender: TObject);
begin
  ShowData();
end;

procedure TSaveMacForm.ShowData;
var
  SaveFile: string;
  value: Integer;
  temp, S: string;
  I, M1, M2, M3, M4, M5, M6, J : Integer;
  C: Extended;
begin
  CheckIsEmpty;      // 判断是否为空

 if CheckStepOrLen = True then begin
    Self.Memo1.Lines.Clear;
    M1 := StrToInt(‘$‘ + Edit1.Text);         /// 直接把edit里的数据变成16进制 (是由十进制To十六进制)
    M2 := StrToInt(‘$‘ + Edit2.Text);
    M3 := StrToInt(‘$‘ + Edit3.Text);
    M4 := StrToInt(‘$‘ + Edit4.Text);
    M5 := StrToInt(‘$‘ + Edit5.Text);
    M6 := StrToInt(‘$‘ + Edit6.Text);
    if RadioButton1.Checked then
      Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));    /// 将十六进制To十进制
    if RadioButton2.Checked then
      Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + ‘:‘ + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));
    if RadioButton3.Checked then
      Self.Memo1.Lines.Add(IntToHex(M1, 2) + ‘-‘ + IntToHex(M2, 2) + ‘-‘ +IntToHex(M3, 2) + ‘-‘ + IntToHex(M4, 2) + ‘-‘ + IntToHex(M5, 2) + ‘-‘ + IntToHex(M6, 2));
    for I := 1 to StrToInt(Edit8.Text) do
    begin
      temp := ‘0‘ ;
      if M6 < 16  then                                 /// 小于F,则加个0
      begin
        M6 := StrToInt(‘$‘ + IntToStr(M6));
        M6 := StrToInt(temp + IntToHex(m6, 2));
      end;
      if M5 < 16 then
      begin
        M5 := StrToInt(‘$‘ + IntToStr(M5));
        M5 := StrToInt(temp + IntToHex(m5, 2));
      end;
      if M4 < 16 then
      begin
        M4 := StrToInt(‘$‘ + IntToStr(M4));
        M4 := StrToInt(temp + IntToHex(m4, 2));
      end;
      if M3 < 16 then
      begin
        M3 := StrToInt(‘$‘ + IntToStr(M3));
        M3 := StrToInt(temp + IntToHex(m3, 2));
      end;
      if M2 < 16 then
      begin
        M2 := StrToInt(‘$‘ + IntToStr(M2));
        M2 := StrToInt(temp + IntToHex(m2, 2));
      end;
      if M1 < 16 then
      begin
        M1 := StrToInt(‘$‘ + IntToStr(M1));
        M1 := StrToInt(temp + IntToHex(m1, 2));
      end;
      M6 := M6 + StrToInt(Edit7.Text);              /// 加步长
      if M6 >= 256 then                            /// 若大于等于FF则进位
      begin
        M5 := M5 + 1;
        M6 := M6 - 256;
      end;
      if M5 >= 256 then
      begin
        M4 := M4 + 1;
        M5 := M5 - 256;
      end;
      if M4 >= 256 then
      begin
        M3 := M3 + 1;
        M4 := M4 - 256;
      end;
      if M3 >= 256 then
      begin
        M2 := M2 + 1;
        M3 := M3 - 256 ;
      end;
      if M2 >= 256 then
      begin
        M1 := M1 + 1;
        M2 := M2 - 256;
      end;
      if M1 >= 256 then
        Application.MessageBox(‘超出范围‘, ‘提示消息‘, mb_OK + MB_ICONERROR);
      if RadioButton1.Checked then
        Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));
      if RadioButton2.Checked then
        Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + ‘:‘ + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));
      if RadioButton3.Checked then
        Self.Memo1.Lines.Add(IntToHex(M1, 2) + ‘-‘ + IntToHex(M2, 2) + ‘-‘ +IntToHex(M3, 2) + ‘-‘ + IntToHex(M4, 2) + ‘-‘ + IntToHex(M5, 2) + ‘-‘ + IntToHex(M6, 2));
    end;
 end;
end;

procedure TSaveMacForm.SaveData;
var
  SaveFile: string;
begin
  if CheckStepOrLen = True then begin
    /// 保存
    SaveDialog1.Filter := ‘*.txt|*.txt‘;  //‘txtFile( *.txt)|*.txt‘;     /// txt格式
    SaveDialog1.DefaultExt := ‘*.txt‘;                                   /// 默认格式
    SaveDialog1.Title := ‘保存MAC地址‘;
    if SaveDialog1.Execute then
    begin
      SaveFile := SaveDialog1.FileName;
      Memo1.Lines.SaveToFile(SaveFile);                                  /// 保存memo里数据
    end;
  end;
end;

procedure TSaveMacForm.SaveButtonClick(Sender: TObject);
begin
  ShowData();
end;

procedure TSaveMacForm.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i: integer;
begin
  if length(Edit1.Text)  >= 2 then
    Edit2.SetFocus;
  if length(Edit2.Text ) >= 2 then
    Edit3.SetFocus;
  if length(Edit3.Text ) >= 2 then
    Edit4.SetFocus;
  if length(Edit4.Text ) >= 2 then
    Edit5.SetFocus;
  if Length(Edit5.Text ) >= 2 then
    Edit6.SetFocus;
 (* for i := 0 to ComponentCount - 1 do begin
    if Components[i] is TEdit then
      if Length((Components[i] as TEdit).Text) >= 2 then
  end;*)
end;

procedure TSaveMacForm.btn_clearClick(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to self.ComponentCount -1 do begin
    if self.Components[i] is TEdit then begin
      (self.Components[i] as TEdit).Text := ‘‘;
    end;
  end;
  Edit1.SetFocus;
end;

end.
时间: 2024-10-14 12:42:27

delphi 生成网卡MAC地址的相关文章

【转载】取得系统中网卡MAC地址的三种方法

From:http://blog.csdn.net/zhangting1987/article/details/2732135 网卡地址这个概念有点混淆不清.因为实际上有两个地址,mac地址和物理地址,一般说网卡地址我是指物理地址,不知道别人怎么看?物理地址指的是网卡上的存放地址的ROM里的地址,mac地址是这块卡工作的时候用的地址,一般情况下这两个地址是一样的,所以很多人都混用了,甚至不知道有区别  -_-# 网卡工作的时候,一个以太网帧60到1514(不包括crc),帧的crc是网卡自动加的

java获取当前计算机网卡MAC地址

package com.yjm.testmac; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class MacTest { public static void main(String[] args)

Python 获取 网卡 MAC 地址

/*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: * 记录一下Python如何获取网卡MAC地址,主要用于数据唯一性保存. * * 2016-10-14 深圳 南山平山村 曾剑锋 **********************************************************************/ 一.参考文档: pyth

在Mac OSX中获取网卡MAC地址

今天在CocoaChina上看到有人发帖在询问如何在Mac OSX下获得网卡的物理地址,恰好最近在学习IOKit的一些知识,所以通过IOKit的接口肯定是可以很方便的拿到MAC地址信息的,但不知道还有没有其它更方便的API了,所以我姑且贴出一段通过IOKit去获得网卡MAC地址的代码: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 - (void)applicationDid

Java/JSP获得客户端网卡MAC地址的三种方法解析

java/jsp获得客户端(IE)网卡MAC地址的方法大概有三种. 1.通过命令方式,在客户端执行Ipconfig 等等.(java/jsp) 2.通过ActiveX的方法.(jsp) 3.通过向137的端口发送查询指令的方法.(java/jsp) 简介: 第一种方法,在真正用到时候不知道为什么获得mac地址的指令阻塞了,不往下继续执行.而且速度是这三种方法中最慢的一种. 代码如下: String sip=""; String smac=""; sip = requ

C++ 硬件信息 获取网卡MAC地址

获取计算机网卡MAC地址的方法很多,这里采用的是命令行的形式获取,可以获取有线网卡与无线网卡的MAC地址,很简单,直接 上代码. [1]头文件 #if !defined(AFX_95644697_C78F_4DD6_885B_3D7C40B2D55C__INCLUDED_) #define AFX_95644697_C78F_4DD6_885B_3D7C40B2D55C__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER

云主机centos系统修改网卡Mac地址

云主机 在文件/etc/sysconfig/network-scripts/ifcfg-eth0 中增加一行类似如下 ??MACADDR=00:04:AC:33:1C:3B ??答:MAC地址是网卡的物理地址,其实在Linux下也可更改MAC地址: ??1)闭网卡设备 ??/sbin/ifconfig eth0 down ??2)修改MAC地址 ??/sbin/ifconfig eth0 hw ether MAC地址 ??3)重启网卡 ??/sbin/ifconfig eth0 up ??Red

电脑网卡MAC地址的更改

在Windows10系统修改网卡物理地址(MAC)的方法(一)中介绍了: ?在网卡属性的高级设置中修改网卡的物理地址: ?用第三方软件修改网卡的物理地址,但有些网卡的物理地址用方法?.?是更改不了的. 下面介绍方法?在注册表编辑器中新建NetworkAddress字符串值更改网卡物理地址的方法,解决使用一般方法不能更改网卡MAC地址的问题. (本文在Windows10-14393版系统下撰写) 工具/原料 硬件:电脑 操作系统:Windows10 方法/步骤 1 Windows10系统,使用一般

(转)通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网上也有相关的代码,通过实际调试,也发现确实可以通过WMI来获取这些信息.前两天,在网上突然搜到一位大牛写的比较完整的程序,为了以后使用方便,就转载记录一下.同时,也会在大牛的代码中增加一些自己的注释,都是自己在实际使用过程中遇到的问题. #define _WIN32_DCOM #include "s