在ASP中用组件检测当前网卡地址的代码

2018-09-06 12:28

阅读:486

  OptionExplicit

PrivateConstNCBASTAT=&H33
PrivateConstNCBNAMSZ=16
PrivateConstHEAP_ZERO_MEMORY=&H8
PrivateConstHEAP_GENERATE_EXCEPTIONS=&H4
PrivateConstNCBRESET=&H32

PrivateTypeNCB
ncb_commandAsByteInteger
ncb_retcodeAsByteInteger
ncb_lsnAsByteInteger
ncb_numAsByteInteger
ncb_bufferAsLongString
ncb_lengthAsInteger
ncb_callnameAsString*NCBNAMSZ
ncb_nameAsString*NCBNAMSZ
ncb_rtoAsByteInteger
ncb_stoAsByteInteger
ncb_postAsLong
ncb_lana_numAsByteInteger
ncb_cmd_cpltAsByteInteger
ncb_reserve(9)AsByteReserved,mustbe0
ncb_eventAsLong
EndType
PrivateTypeADAPTER_STATUS
adapter_address(5)AsByteAsString*6
rev_majorAsByteInteger
reserved0AsByteInteger
adapter_typeAsByteInteger
rev_minorAsByteInteger
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

PrivateDeclareFunctionNetbiosLibnetapi32.dll_
(pncbAsNCB)AsByte
PrivateDeclareSubCopyMemoryLibkernel32AliasRtlMoveMemory(_
hpvDestAsAny,ByValhpvSourceAsLong,ByValcbCopyAsLong)
PrivateDeclareFunctionGetProcessHeapLibkernel32()AsLong
PrivateDeclareFunctionHeapAllocLibkernel32_
(ByValhHeapAsLong,ByValdwFlagsAsLong,_
ByValdwBytesAsLong)AsLong
PrivateDeclareFunctionHeapFreeLibkernel32(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


评论


亲,登录后才可以留言!