前言,筆者由於前段時間要做一個MIS系統的開發,由於該MIS為一個非免費軟件,故在完成該系統開發時相應的注冊模塊也須開發,由於為B/S結構的系統,所以在注冊特征碼的選擇上我選擇了獨一無二的網卡Mac地址。閒話少說,下面切入正題。由於該系統屬商業軟件,故以下代碼中僅包含代碼思路,及簡單的實現代碼,具體大家可根據代碼自由發揮,也可與我一起切磋。
'----------------------提取所有網卡的信息--------------------'
Public Function GetMacInfo()
On Error Resume Next
Dim fso, FileStr, ASPSleepThread, CmdStr, SysDir, wshshell, CmdRe, MacFileContentFile, MacFileContent
Const MacFile = "TmpYesoulSoft001.LLP"
Set fso = Server.CreateObject("Scripting.FileSystemObject")
SysDir = Split(GlobalMod.GetSysDir, ",")(1)
If InStr(LCase(SysDir), "system32") = 0 Then
GetMacInfo = "本系統只能運行在Nt、Windows 2000、Windows.Net、Windows XP、Windows 2003等32位系統下,不支持32位以下的系統!"
'www.knowsky.com
Exit Function
Else
CmdStr = SysDir + "\Cmd.exe /C " + SysDir + "\Ipconfig.exe /All > " + Server.MapPath(MacFile)
End If
CmdRe = Shell(CmdStr, vbHide)
If CmdRe <> 0 Then
Set MacFileContentFile = fso.OpenTextFile(Server.MapPath(MacFile), 1, False, TristateUseDefault)
'GetMacInfo = MacFileContentFile.ReadAll()
'Response.Flush
FileStr = MacFileContentFile.ReadAll()
MacFileContentFile.Close
Set MacFileContentFile = Nothing
Set ASPSleepThread = Server.CreateObject("YesoulSoft.SleepThread")
'定義線程掛起的時間,這裡為毫秒
ASPSleepThread.SleepTime = 500
ASPSleepThread.BeginSleepThread
GetMacInfo = ExecuteOne(FileStr, "Physical Address. . . . . . . . . : (.*)")
Set ASPSleepThread = Nothing
Else
GetMacInfo = "系統當前無法獲取您的網絡信息,請檢查權限繼承關系後再運行本系統!"
Exit Function
End If
DelFile MacFile
Set fso = Nothing
End Function
'------------------在字符串匹配一次結果-------------------'
Public Function ExecuteOne(inpStr, PatStr)
Dim oRe, oMatch, oMatches
Set oRe = New RegExp
oRe.Pattern = PatStr
inpStr = LCase(inpStr)
oRe.IgnoreCase = True
Set oMatches = oRe.Execute(inpStr)
Set oMatch = oMatches(0)
ExecuteOne = oMatch.SubMatches(0)
End Function
代碼中GETMACINFO函數僅僅可以獲取首個網卡的Mac地址,至於DNS、網關等信息大家可以舉一反三啊。