获取Mac地址通过Com获取.ahk

; 创建WMI服务的实例
wmi := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2")

; 执行WMI查询以获取已启用的网络适配器配置信息
for obj in wmi.ExecQuery("Select MACAddress from Win32_NetworkAdapterConfiguration Where IPEnabled=True")
  MACAddresses .= obj.MACAddress . "`n"

; 显示MAC地址
if (MACAddresses = "")
  MsgBox, 找不到活动的网络适配器.
else
  MsgBox, % "活动网络适配器的MAC地址:`n" . MACAddresses

 

获取Mac地址.ahk

for i, v in GetMacAddress()
  MsgBox % v

; 获取计算机的MAC地址列表

GetMacAddress(delimiter := ":", case := False) {
  if (DllCall("iphlpapi.dll\GetAdaptersInfo", "ptr", 0, "uint*", size) = 111) && !(VarSetCapacity(buf, size, 0))
    throw Exception("Memory allocation failed for IP_ADAPTER_INFO struct", -1)
  if (DllCall("iphlpapi.dll\GetAdaptersInfo", "ptr", &buf, "uint*", size) != 0)
    throw Exception("GetAdaptersInfo failed with error: " A_LastError, -1)
  addr := &buf, MAC_ADDRESS := []
  while (addr) {
    loop % NumGet(addr+0, 396 + A_PtrSize, "uint")
      mac .= Format("{:02" (case ? "X" : "x") "}", NumGet(addr+0, 400 + A_PtrSize + A_Index - 1, "uchar")) "" delimiter ""
    MAC_ADDRESS[A_Index] := SubStr(mac, 1, -1), mac := ""
    addr := NumGet(addr+0, "uptr")
  }
  return MAC_ADDRESS
}

 

获取Mac在用网卡地址.ahk

#NoEnv
#SingleInstance force
MsgBox % GetMac()

; cardroot:="SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}"
; cardaddressnew:=GenerateMacAddress()
; cardline:=cardroot "\" GetMacIndex()
; ;MsgBox %cardline% `n%cardaddressnew%
; RegWrite, REG_SZ,HKEY_LOCAL_MACHINE,%cardline%,NetworkAddress,%cardaddressnew%
; Sleep 1000
; RunWait %comspec% /c netsh interface set interface 本地连接 disable,,hide
; Sleep 1000
; RunWait %comspec% /c netsh interface set interface 本地连接 enable,,hide
; 
; Sleep 5000
; MsgBox % GetMac()
return


For mo in ComObjGet("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
  if (mo.IPEnabled!=0 && Mac:=mo.MacAddress)  ; 单行熔断获取在用Mac
    Break

MsgBox % Mac


GetMac() {
  For mo in ComObjGet("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
    if (mo.IPEnabled != 0)
      Return mo.MacAddress
}

GetMacIndex() {
  NetworkConfiguration:=ComObjGet("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
  for mo in NetworkConfiguration {		 ;是这个意思,但是不很靠谱,如我的电脑里面虽然只经过无线上网,但是装了vm后,vm的网卡也是-1,所以未必准确定位,可自行改进
   ;我建议用网卡的“描述” 跟 mo.Description 进行比较,可准确定位
    if (mo.Description == "Broadcom NetLink (TM) Gigabit Ethernet" )
      return SubStr( "00" mo.Index ,-4)
  }
}
GenerateMacAddress() ;来自https://autohotkey.com/board/topic/92743-mac-address-generator/page-2
{
  HexNum := "0123456789ABCDEF"
  StringSplit, Num, HexNum
  loop, 13
  {
    Counter++
    Random, Rand, 1, 16
    MacAddress .= Num%Rand% (!Mod(Counter,2) ? Separator : "")
  }
  return SubStr(MacAddress,1,-1)
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。