前言,筆者由於前段時間要做一個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.downcodes.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、閘道等資訊大家可以舉一反三啊。