有一段程序的几个语句不太理解
本帖最后由 solox 于 2009-8-7 23:54 编辑Dim $MAC = Chr(13)
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$colNicConfigs = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
If IsObj($colNicConfigs) Then
For $objItem In $colNicConfigs
$objNic = $objWMIService.Get ("Win32_NetworkAdapter.DeviceID=" & $objItem.Index)
$MAC = $MAC & Chr(10) & $objNic.MACAddress
Next
$MAC = StringReplace($MAC,Chr(13)&Chr(10),"")
Else
SetError(1)
EndIf
MsgBox(0,"MAC",$MAC)
MsgBox(0, "共替换的数量为:", @extended)
这是一个读取本机MAC地址的程序,其中有两个语句不太了解作用
1、第一句的$MAC = Chr(13),为什么要先把$MAC设定成一个回车呢?
2、$MAC = StringReplace($MAC,Chr(13)&Chr(10),""),这一句的作用何在,调试发现它根本没替换任何字符呀? 我的理解是For...In...Next需求( 列举对象集合或数组中的元素),在存在多块网卡时,每块网卡的MAC用软回车分离"CHR(10)",按源代码应该是这样:
chr(13)+chr(10)+网卡1+chr(10)+网卡2+chr(10)+网卡3+chr(10)+.........+chr(10)+网卡n
而 StringReplace目的是消除这个串头部的软硬回车符。
这段代码完全可以改成这样(直接加硬回车):Dim $MAC = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
If IsObj($colNicConfigs) Then
For $objItem In $colNicConfigs
$objNic = $objWMIService.Get("Win32_NetworkAdapter.DeviceID=" & $objItem.Index)
$MAC = $MAC & $objNic.MACAddress&@CRLF
Next
$MAC = StringTrimRight($MAC,1)
Else
SetError(1)
EndIf
MsgBox(0, "MAC", $MAC)
MsgBox(0, "共替换的数量为:", @extended) 受教了,非常感谢
页:
[1]