1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2002 VBnet, Randy Birch, All Rights Reserved.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const NERR_SUCCESS As Long = 0&
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Declare Function NetGetJoinInformation Lib "Netapi32" _
(ByVal lpServer As Long, _
lpNameBuffer As Long, _
BufferType As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32.dll" _
(ByVal Buffer As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, uFrom As Any, _
ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Sub Command1_Click()
Dim bufptr As Long
Dim dwServer As Long
Dim dwBufferType As Long
Dim success As Long
Dim sServer As String
sServer = QualifyServer(Environ$("COMPUTERNAME") & vbNullString)
dwServer = StrPtr(sServer)
success = NetGetJoinInformation(dwServer, _
bufptr, _
dwBufferType)
If (success = NERR_SUCCESS) Then
Print "domain or workgroup : "; GetPointerToByteStringW(bufptr)
Print "join status : "; GetJoinStatus(dwBufferType)
End If
NetApiBufferFree bufptr
End Sub
Private Function GetJoinStatus(dwStatus As Long) As String
Select Case dwStatus
Case 0: GetJoinStatus = "The status is unknown"
Case 1: GetJoinStatus = "The computer is not joined"
Case 2: GetJoinStatus = "The computer is joined to a workgroup"
Case 3: GetJoinStatus = "The computer is joined to a domain"
Case Else: GetJoinStatus = "dwStatus outside valid enum range"
End Select
End Function
Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
Dim tmp() As Byte
Dim tmplen As Long
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp
End If
End If
End Function
Private Function QualifyServer(ByVal sServer As String) As String
'see if there are already two slashes
'preceeding the server name
If Left$(sServer, 2) = "\\" Then
'there are, so the server is already
'qualified; return the passed string
QualifyServer = sServer
Else
'there aren't two, but is there one?
If Left$(sServer, 1) = "\" Then
'yes, so add one more
QualifyServer = "\" & sServer
Else
'the string needs both
QualifyServer = "\\" & sServer
End If 'If Left$(sServer, 1) <> "\"
End If 'If Left$(sServer, 2) = "\\"
End Function |
Partager