1、先枚举局域网所有机器列表,到 ComboBox 里面。 2、选择,发送Option ExplicitConst ERROR_SUCCESS = 0 Const ERROR_MORE_DATA = 234 Const SV_TYPE_SERVER = &H2 Const SIZE_SI_101 = 24Private Type SERVER_INFO_101 dwPlatformId As Long lpszServerName As Long dwVersionMajor As Long dwVersionMinor As Long dwType As Long lpszComment As Long End TypePrivate Declare Function NetServerEnum Lib "netapi32.dll" (ByVal servername As String, _ ByVal level As Long, buffer As Long, ByVal prefmaxlen As Long, entriesread As Long, _ totalentries As Long, ByVal servertype As Long, ByVal domain As String, resumehandle As Long) As LongPrivate Declare Function NetApiBufferFree Lib "netapi32.dll" (BufPtr As Any) As Long Private Declare Sub RtlMoveMemory Lib "KERNEL32" _ (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long) Private Declare Function lstrcpyW Lib "KERNEL32" _ (ByVal lpszDest As String, ByVal lpszSrc As Long) As LongPrivate Const NERR_Success As Long = 0& Private Const NERR_BASE = 2100 Private Const NERR_NameNotFound = NERR_BASE + 173 Private Const NERR_NetworkError = NERR_BASE + 36 Private Const ERROR_ACCESS_DENIED = 5 Private Const ERROR_INVALID_PARAMETER = 87 Private Const ERROR_NOT_SUPPORTED = 50Private Declare Function NetMessageBufferSend Lib "netapi32.dll" (servername As Any, _ msgname As Byte, fromname As Any, buf As Byte, ByVal buflen As Long) As LongPrivate Sub cmdSend_Click() Dim nRet As Long Dim sTo() As Byte Dim sMsg() As Byte sTo = lst.List(lst.ListIndex) & Chr(0) sMsg = txtMsg & Chr(0) nRet = NetMessageBufferSend(ByVal 0, sTo(0), ByVal 0, sMsg(0), UBound(sMsg)) Select Case nRet Case NERR_Success: MsgBox "Success" Case NERR_NameNotFound: MsgBox "NameNotFound" Case NERR_NetworkError: MsgBox "NetworkError" Case ERROR_ACCESS_DENIED: MsgBox "ACCESS_DENIED" Case ERROR_INVALID_PARAMETER: MsgBox "INVALID_PARAMETER" Case ERROR_NOT_SUPPORTED: MsgBox "NOT_SUPPORTED" Case Else: MsgBox "Unexpected error" End Select End SubPrivate Function PointerToString(lpszString As Long) As String Dim lpszStr1 As String, lpszStr2 As String, nRet As Long lpszStr1 = String(1000, "*") nRet = lstrcpyW(lpszStr1, lpszString) lpszStr2 = (StrConv(lpszStr1, vbFromUnicode)) PointerToString = Left(lpszStr2, InStr(lpszStr2, Chr$(0)) - 1) End FunctionPrivate Sub Form_Load() Dim pszServer As String, pszDomain As String Dim nLevel As Long, i As Long, BufPtr As Long, TempBufPtr As Long Dim nPrefMaxLen As Long, nEntriesRead As Long, nTotalEntries As Long Dim nServerType As Long, nResumeHandle As Long, nRet As Long Dim ServerInfo As SERVER_INFO_101
nLevel = 101 BufPtr = 0 nPrefMaxLen = &HFFFFFFFF nEntriesRead = 0 nTotalEntries = 0 nServerType = SV_TYPE_SERVER nResumeHandle = 0 Do nRet = NetServerEnum(pszServer, nLevel, BufPtr, nPrefMaxLen, nEntriesRead, _ nTotalEntries, nServerType, pszDomain, nResumeHandle) If ((nRet = ERROR_SUCCESS) Or (nRet = ERROR_MORE_DATA)) And (nEntriesRead > 0) Then TempBufPtr = BufPtr For i = 1 To nEntriesRead RtlMoveMemory ServerInfo, TempBufPtr, SIZE_SI_101 lst.AddItem PointerToString(ServerInfo.lpszServerName) TempBufPtr = TempBufPtr + SIZE_SI_101 Next i Else MsgBox "NetServerEnum failed: " & nRet End If NetApiBufferFree (BufPtr) Loop While nEntriesRead < nTotalEntries End Sub
2、选择,发送Option ExplicitConst ERROR_SUCCESS = 0
Const ERROR_MORE_DATA = 234
Const SV_TYPE_SERVER = &H2
Const SIZE_SI_101 = 24Private Type SERVER_INFO_101
dwPlatformId As Long
lpszServerName As Long
dwVersionMajor As Long
dwVersionMinor As Long
dwType As Long
lpszComment As Long
End TypePrivate Declare Function NetServerEnum Lib "netapi32.dll" (ByVal servername As String, _
ByVal level As Long, buffer As Long, ByVal prefmaxlen As Long, entriesread As Long, _
totalentries As Long, ByVal servertype As Long, ByVal domain As String, resumehandle As Long) As LongPrivate Declare Function NetApiBufferFree Lib "netapi32.dll" (BufPtr As Any) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" _
(hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function lstrcpyW Lib "KERNEL32" _
(ByVal lpszDest As String, ByVal lpszSrc As Long) As LongPrivate Const NERR_Success As Long = 0&
Private Const NERR_BASE = 2100
Private Const NERR_NameNotFound = NERR_BASE + 173
Private Const NERR_NetworkError = NERR_BASE + 36
Private Const ERROR_ACCESS_DENIED = 5
Private Const ERROR_INVALID_PARAMETER = 87
Private Const ERROR_NOT_SUPPORTED = 50Private Declare Function NetMessageBufferSend Lib "netapi32.dll" (servername As Any, _
msgname As Byte, fromname As Any, buf As Byte, ByVal buflen As Long) As LongPrivate Sub cmdSend_Click()
Dim nRet As Long
Dim sTo() As Byte
Dim sMsg() As Byte sTo = lst.List(lst.ListIndex) & Chr(0)
sMsg = txtMsg & Chr(0)
nRet = NetMessageBufferSend(ByVal 0, sTo(0), ByVal 0, sMsg(0), UBound(sMsg))
Select Case nRet
Case NERR_Success: MsgBox "Success"
Case NERR_NameNotFound: MsgBox "NameNotFound"
Case NERR_NetworkError: MsgBox "NetworkError"
Case ERROR_ACCESS_DENIED: MsgBox "ACCESS_DENIED"
Case ERROR_INVALID_PARAMETER: MsgBox "INVALID_PARAMETER"
Case ERROR_NOT_SUPPORTED: MsgBox "NOT_SUPPORTED"
Case Else: MsgBox "Unexpected error"
End Select
End SubPrivate Function PointerToString(lpszString As Long) As String
Dim lpszStr1 As String, lpszStr2 As String, nRet As Long
lpszStr1 = String(1000, "*")
nRet = lstrcpyW(lpszStr1, lpszString)
lpszStr2 = (StrConv(lpszStr1, vbFromUnicode))
PointerToString = Left(lpszStr2, InStr(lpszStr2, Chr$(0)) - 1)
End FunctionPrivate Sub Form_Load()
Dim pszServer As String, pszDomain As String
Dim nLevel As Long, i As Long, BufPtr As Long, TempBufPtr As Long
Dim nPrefMaxLen As Long, nEntriesRead As Long, nTotalEntries As Long
Dim nServerType As Long, nResumeHandle As Long, nRet As Long
Dim ServerInfo As SERVER_INFO_101
nLevel = 101
BufPtr = 0
nPrefMaxLen = &HFFFFFFFF
nEntriesRead = 0
nTotalEntries = 0
nServerType = SV_TYPE_SERVER
nResumeHandle = 0 Do
nRet = NetServerEnum(pszServer, nLevel, BufPtr, nPrefMaxLen, nEntriesRead, _
nTotalEntries, nServerType, pszDomain, nResumeHandle)
If ((nRet = ERROR_SUCCESS) Or (nRet = ERROR_MORE_DATA)) And (nEntriesRead > 0) Then
TempBufPtr = BufPtr
For i = 1 To nEntriesRead
RtlMoveMemory ServerInfo, TempBufPtr, SIZE_SI_101
lst.AddItem PointerToString(ServerInfo.lpszServerName)
TempBufPtr = TempBufPtr + SIZE_SI_101
Next i
Else
MsgBox "NetServerEnum failed: " & nRet
End If
NetApiBufferFree (BufPtr)
Loop While nEntriesRead < nTotalEntries
End Sub