引言
在我之前出的文章中,有一篇获取cpu序列号的文章,文章中我探讨了三种方法,下面我给出获取MAC地址的三种方法,供大家一起学习探讨。其优缺点跟之前CPU的一样,这里不再赘述。经大家测试,第二种、第三种方法有一定局限性,经查资料:wmic命令需要两个先决条件a. 启动Windows Management Instrumentation服务,开放TCP135端口。b. 本地安全策略的“网络访问: 本地帐户的共享和安全模式”应设为“经典-本地用户以自己的身份验证”。
代码分享
MacAddress:=GetMacAddress()
MsgBox, %MacAddress%
;;2.获取网卡硬件地址
;~ ;方法一
;~ GetMacAddress()
;~ {
;~ RunWait, %ComSpec% /c getmac /NH | clip,,hide
;~ RegExMatch(clipboard, ".*?([0-9A-Z].{16})(?!\w\\Device)", mac)
;~ return %mac1%
;~ }
;~ ;方法二
;~ GetMacAddress()
;~ {
;~ NetworkConfiguration:=ComObjGet("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
;~ for mo in NetworkConfiguration
;~ {
;~ if(mo.IPEnabled <> 0)
;~ return mo.MacAddress
;~ }
;~ }
;方法三
GetMacAddress()
{
NetworkConfiguration := StdoutToVar_CreateProcess("getmac")
RegExMatch(NetworkConfiguration, ".*?([0-9A-Z].{16})(?!\w\\Device)", mac)
return %mac1%
}
; Im Original gilt "CP0"; zu CharSet CP850/CP858 vgl.: https://goo.gl/Y8xUYu , http://goo.gl/cMtc6i , https://goo.gl/ssCplI , https://goo.gl/s2P1jK
StdoutToVar_CreateProcess(sCmd, sEncoding:="CP858", sDir:="", ByRef nExitCode:=0) {
DllCall( "CreatePipe", PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 )
DllCall( "SetHandleInformation", Ptr,hStdOutWr, UInt,1, UInt,1 )
VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24, 0 )
siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 )
NumPut( siSz, si, 0, "UInt" )
NumPut( 0x100, si, (A_PtrSize == 4) ? 44 : 60, "UInt" )
NumPut( hStdInRd, si, (A_PtrSize == 4) ? 56 : 80, "Ptr" )
NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 60 : 88, "Ptr" )
NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 64 : 96, "Ptr" )
if ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000
, Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) )
return ""
, DllCall( "CloseHandle", Ptr,hStdOutWr )
, DllCall( "CloseHandle", Ptr,hStdOutRd )
DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before Reading the stdout.
VarSetCapacity(sTemp, 4095)
while ( DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,4095, PtrP,nSize, Ptr,0 ) )
sOutput .= StrGet(&sTemp, nSize, sEncoding)
DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), UIntP,nExitCode )
DllCall( "CloseHandle", Ptr,NumGet(pi,0) )
DllCall( "CloseHandle", Ptr,NumGet(pi,A_PtrSize) )
DllCall( "CloseHandle", Ptr,hStdOutRd )
return sOutput
}


方法一不能显示正确的MAC地址。我电脑装了vmware,用方法一显示的是vmware的虚拟网卡MAC地址,而不是联网的以太网的有线网卡的MAC地址