自己画的托马斯简笔画:VB检测局域中电脑方法

来源:百度文库 编辑:神马品牌网 时间:2024/05/16 00:34:07
怎样用VB检测到局域网中的所有电脑名称和IP并显示在列表框中

Public Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_oth_domains As Long
wkui1_logon_server As Long
End Type

Public Type Server_Info
Platform_id As Long
SvrName As String
Version_major As Long
Version_minor As Long
nType As Long
Comment As String
End Type

Public Type G_Grp_Info
GrpName As String
GrpComment As String
End Type

Public Type OS_VER_INFO
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String
End Type

Public Type USER_INFO
strWkstaName As String '工作站名称
strLanGrp As String '所属的工作组名称
strUsrName As String '用户名称
strDomainName As String '网域名称
strSvrName As String '网域PDC
strUsrFullName As String '用户全名
strPassword As String '保留
End Type
Public tUsrInfo As USER_INFO
Sub Main()
.....
tUsrInfo = GetNUsrInfo
......
End Sub
Public Function GetNUsrInfo() As USER_INFO
......
Dim tdfRetInfo As WKSTA_USER_INFO_1, nPtrRetInfo As Long
.............
NetWkstaUserGetInfo ByVal 0&, 1, nPtrRetInfo
RtlMoveMemory tdfRetInfo, ByVal nPtrRetInfo, Len(tdfRetInfo)
'将nPtrRetInfo指针指向的内存块复制到tdfRetInfo中
lstrcpy bBufferA(0), tdfRetInfo.wkui1_logon_domain
'将wkui1_logon_domain指针指向的内存块复制到一个字节数组中
'***********************************************
'这段代码将NT的NULL TERMINATE的UNICODE字符串转化为VB的字符串
strDomainName = bBufferA
strDomainName = Left(strDomainName, InStr(strDomainName, Chr(0)) - 1)
*this is the USER_INFO.strDomainName
........
END SUB

Use netServerEnum.
Sample:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyW" (retVal As Byte, ByVal Ptr As Long) As Long
Public Declare Function StrToPtr Lib "kernel32" Alias "lstrcpyW" (ByVal Ptr As Long, Source As Byte) As Long
Public Declare Function PtrToInt Lib "kernel32" Alias "lstrcpynW" (retVal As Any, ByVal Ptr As Long, ByVal nCharCount As Long) As Long
Public Declare Function StrLen Lib "kernel32" Alias "lstrlenW" (ByVal Ptr As Long) As Long

Public OK As Boolean

Private Sub Form_Load()
Dim sBuffer As String
Dim lSize As Long
Call FillCboSvrs

End Sub

Sub FillCboSvrs()
cboSvrs.Clear
Dim tSvrInfo As Server_Info
Dim strDomainName As String
Dim result As Long, bufptr As Long, entriesread As Long, totalentries As Long, resumehandle As Long, BufLen As Long, _
DNArray() As Byte, SNArray(99) As Byte, UNArray() As Byte, _
SName As String, i As Integer, UNPtr As Long, nLevel As Long, nSvrType As Long, _
TempPtr As MungeLong, TempStr As MungeInt
Dim nPlatform_id As Long, strSvrName As String, nVersion_major As Long, nVersion_minor As Long, nType As Long, strComment As String
Dim SvrInfoArray() As Server_Info
nSvrType = SV_TYPE_ALL
' UNArray = UName & vbNullChar ' Move to Byte array
BufLen = -1 ' Buffer size
resumehandle = 0 ' Start with the first entry
nLevel = 101
strDomainName = tUsrInfo.strDomainName
DNArray = strDomainName & vbNullChar ' Move to byte array
Do
If Len(Trim(strDomainName)) > 0 Then
result = NetServerEnum(ByVal 0, nLevel, bufptr, BufLen, entriesread, totalentries, nSvrType, DNArray(0), resumehandle)
Else
result = NetServerEnum(ByVal 0, nLevel, bufptr, BufLen, entriesread, totalentries, nSvrType, ByVal 0, resumehandle)
End If
If result <> ERROR_SUCCESS And result <> ERROR_MORE_DATA Then
' required
MsgBox ("Error " & result & " enumerating group " & entriesread & " of " & totalentries)
Exit Sub
End If
Dim j As Long
For i = 1 To entriesread
' Get pointer to string from beginning of buffer
' Copy 4 byte block of memory each time
j = (i - 1) * 6

result = PtrToInt(TempPtr.X, bufptr + j * 4, 4)
nPlatform_id = TempPtr.X

result = PtrToInt(TempPtr.X, bufptr + (j + 1) * 4, 4)
' Copy string to array and convert to a string
result = PtrToStr(SNArray(0), TempPtr.X)
strSvrName = Left(SNArray, StrLen(TempPtr.X))

result = PtrToInt(TempPtr.X, bufptr + (j + 2) * 4, 4)
nVersion_major = TempPtr.X

result = PtrToInt(TempPtr.X, bufptr + (j + 3) * 4, 4)
nVersion_minor = TempPtr.X

result = PtrToInt(TempPtr.X, bufptr + (j + 4) * 4, 4)
nType = TempPtr.X

result = PtrToInt(TempPtr.X, bufptr + (j + 5) * 4, 4)
result = PtrToStr(SNArray(0), TempPtr.X)
strComment = Left(SNArray, StrLen(TempPtr.X))
'Add A Record Into Return Recordset

cboSvrs.AddItem strSvrName

Next i

result = NetAPIBufferFree(bufptr)
Loop Until entriesread = totalentries
' The above condition only valid for reading accounts on NT
' but not OK for OS/2 or LanMan

' result = NetAPIBufferFree(bufptr) ' Don't leak memory
End Sub

汗..0奖励都有人为你付出那么多