OptionExplicit
PrivateConstNCBASTAT=&H33
PrivateConstNCBNAMSZ=16
PrivateConstHEAP_ZERO_MEMORY=&H8
PrivateConstHEAP_GENERATE_EXCEPTIONS=&H4
PrivateConstNCBRESET=&H32
PrivateTypeNCB
ncb_commandAsByte'Integer
ncb_retcodeAsByte'Integer
ncb_lsnAsByte'Integer
ncb_numAsByte'Integer
ncb_bufferAsLong'String
ncb_lengthAsInteger
ncb_callnameAsString*NCBNAMSZ
ncb_nameAsString*NCBNAMSZ
ncb_rtoAsByte'Integer
ncb_stoAsByte'Integer
ncb_postAsLong
ncb_lana_numAsByte'Integer
ncb_cmd_cpltAsByte'Integer
ncb_reserve(9)AsByte'Reserved,mustbe0
ncb_eventAsLong
EndType
PrivateTypeADAPTER_STATUS
adapter_address(5)AsByte'AsString*6
rev_majorAsByte'Integer
reserved0AsByte'Integer
adapter_typeAsByte'Integer
rev_minorAsByte'Integer
durationAsInteger
frmr_recvAsInteger
frmr_xmitAsInteger
iframe_recv_errAsInteger
xmit_abortsAsInteger
xmit_successAsLong
recv_successAsLong
iframe_xmit_errAsInteger
recv_buff_unavailAsInteger
t1_timeoutsAsInteger
ti_timeoutsAsInteger
Reserved1AsLong
free_ncbsAsInteger
max_cfg_ncbsAsInteger
max_ncbsAsInteger
xmit_buf_unavailAsInteger
max_dgram_sizeAsInteger
pending_sessAsInteger
max_cfg_sessAsInteger
max_sessAsInteger
max_sess_pkt_sizeAsInteger
name_countAsInteger
EndType
PrivateTypeNAME_BUFFER
nameAsString*NCBNAMSZ
name_numAsInteger
name_flagsAsInteger
EndType
PrivateTypeASTAT
adaptAsADAPTER_STATUS
NameBuff(30)AsNAME_BUFFER
EndType
PrivateDeclareFunctionNetbiosLib"netapi32.dll"_
(pncbAsNCB)AsByte
PrivateDeclareSubCopyMemoryLib"kernel32"Alias"RtlMoveMemory"(_
hpvDestAsAny,ByValhpvSourceAsLong,ByValcbCopyAsLong)
PrivateDeclareFunctionGetProcessHeapLib"kernel32"()AsLong
PrivateDeclareFunctionHeapAllocLib"kernel32"_
(ByValhHeapAsLong,ByValdwFlagsAsLong,_
ByValdwBytesAsLong)AsLong
PrivateDeclareFunctionHeapFreeLib"kernel32"(ByValhHeapAsLong,_
ByValdwFlagsAsLong,lpMemAsAny)AsLong
PublicFunctionGetMACAddress(sIPAsString)AsString
DimsRtnAsString
DimmyNcbAsNCB
DimbRetAsByte
DimaIP()AsString
DimxAsLong
DimnIPAsString
IfInStr(sIP,".")=0Then
GetMACAddress="无效的IP地址."
ExitFunction
EndIf
aIP=Split(sIP,".",-1,vbTextCompare)
IfUBound(aIP())<>3Then
GetMACAddress="无效的IP地址."
ExitFunction
EndIf
Forx=0ToUBound(aIP())
IfLen(aIP(x))>3Then
GetMACAddress="无效的IP地址"
ExitFunction
EndIf
IfIsNumeric(aIP(x))=FalseThen
GetMACAddress="无效的IP地址"
ExitFunction
EndIf
IfInStr(aIP(x),",")<>0Then
GetMACAddress="无效的IP地址"
ExitFunction
EndIf
IfCLng(aIP(x))>255Then
GetMACAddress="无效的IP地址"
ExitFunction
EndIf
IfnIP=""Then
nIP=String(3-Len(aIP(x)),"0")&aIP(x)
Else
nIP=nIP&"."&String(3-Len(aIP(x)),"0")&aIP(x)
EndIf
Next
sRtn=""
myNcb.ncb_command=NCBRESET
bRet=Netbios(myNcb)
myNcb.ncb_command=NCBASTAT
myNcb.ncb_lana_num=0
myNcb.ncb_callname=nIP&Chr(0)
DimmyASTATAsASTAT,tempASTATAsASTAT
DimpASTATAsLong
myNcb.ncb_length=Len(myASTAT)
pASTAT=HeapAlloc(GetProcessHeap(),HEAP_GENERATE_EXCEPTIONSOrHEAP_ZERO_MEMORY,myNcb.ncb_length)
IfpASTAT=0Then
GetMACAddress="memoryallcoationfailed!"
ExitFunction
EndIf
myNcb.ncb_buffer=pASTAT
bRet=Netbios(myNcb)
IfbRet<>0Then
GetMACAddress="不能从当前IP地址获得MAC,当前IP地址:"&sIP
ExitFunction
EndIf
CopyMemorymyASTAT,myNcb.ncb_buffer,Len(myASTAT)
DimsTempAsString
DimiAsLong
Fori=0To5
sTemp=Hex(myASTAT.adapt.adapter_address(i))
Ifi=0Then
sRtn=IIf(Len(sTemp)<2,"0"&sTemp,sTemp)
Else
sRtn=sRtn&Space(1)&IIf(Len(sTemp)<2,"0"&sTemp,sTemp)
EndIf
Next
HeapFreeGetProcessHeap(),0,pASTAT
GetMACAddress=sRtn
EndFunction
使用方法:
setS_MAC=server.CreateObject("工程名.类名")
response.writeS_MAC.GetMACAddress(Request.Servervariables("REMOTE_HOST"))
setS_MAC=nothing
【在ASP中用组件检测当前网卡地址的代码】相关文章: