AWehrstedt
Programmer
I have an application the uses the NetAPI "NetServerEnum" function to determine the domain controllers on the network, determines if you belong to a specified access account and from that result determines your access level to the application.
The code executes perfectly in the VB designing environment. However as soon as the source code is compiled into an .exe file, the NetServerEnum function doesn't work properly and always returns 0 servers. There are 2 that get picked up before compiling. The only thing I can think of is the stDomain and stCompName net to be set, but everything I've tried produces the same result.
Any help would be greatly appreciated!!
here is the the code for the EnumerateServers fuction: The lgTotalEntries returns 0 (which is shouldn't, it should return 2). The error I get is because lgTotalEntries is 2 (Error Code 87).
Public Function EnumerateServers(stType As String) As Long
'* Enumerates Servers by Type Passed into function
Dim lgResult As Long, lgTotalEntries As Long
Dim lgPrefMaxLen As Long, lgEntriesRead As Long
Dim lgResumeHandle As Long, stDomain As String
Dim lgBufPtr As Long, lgServerType As Long
Dim btSrvArray(32) As Byte, btDomain() As Byte, btCompName() As Byte
Dim stCompName As String
Dim stServerName As String, stText As String
Dim ServerStruct As SERVER_INFO_100
On Error GoTo simptrap
'* Clear values and initialize all of the variables
lgResumeHandle = 0
ReDim stServerArray(0) As String
'* Set the domain to the default, primary domain
stDomain = ""
'* Set the computer name to the default, local system
stCompName = ""
'* Create Null Terminated Strings
btDomain = stDomain & vbNullChar
btCompName = stCompName & vbNullChar
'* Determine stType Values and set servertype value
'* You may add or remove these types as needed for your application
If stType = "DCs" Then
'* Enumerate Domain Controllers
lgServerType = Abs(SV_TYPE_DOMAIN_CTRL) + Abs(SV_TYPE_DOMAIN_BAKCTRL)
End If
'* Call NetServerEnum to get a list of Servers
lgResult = NetServerEnum(btCompName(0), 100, lgBufPtr, lgPrefMaxLen, lgEntriesRead, lgTotalEntries, ByVal lgServerType, ByVal btDomain(0), lgResumeHandle)
EnumerateServers = lgResult
'* Now that we know how many entries were read, size the array
If lgTotalEntries <= 0 Then
MsgBox "No Entries Read. Error Code = " & CStr(lgResult)
Exit Function
End If
ReDim stServerArray(lgTotalEntries - 1) As String
'* Check for errors
If lgResult <> 0 And lgResult <> 234 Then '* 234 means multiple reads required
If lgResult = 2351 Then
stText = "ServerName not found." & Chr$(13) & "Be sure you used the leading \\ on the servername."
Else
stText = "Error " & lgResult & " enumerating server " & lgEntriesRead & " of " & lgTotalEntries
End If
MsgBox stText
Exit Function
End If
For I = 1 To lgTotalEntries
'* Dereference the ServerStruct Data Structure
CopyMemory ServerStruct, lgBufPtr, Len(ServerStruct)
'* Dereference the server name variable
CopyMemory btSrvArray(0), ServerStruct.lgSvi100_servername, 33
stServerName = btSrvArray
Trim (stServerName)
'* send the servername to the array
'* Change the code here to send the servername to a control, etc.
stServerArray(I - 1) = stServerName
'* Clear the servername variable here
stServerName = ""
'* Move to the next part of the buffer
lgBufPtr = lgBufPtr + Len(ServerStruct)
Next I
'* Release the memory used for the ServerStruct buffer
lgResult = NetAPIBufferFree(lgBufPtr)
Exit Function
simptrap:
Resume Next
End Function
The code executes perfectly in the VB designing environment. However as soon as the source code is compiled into an .exe file, the NetServerEnum function doesn't work properly and always returns 0 servers. There are 2 that get picked up before compiling. The only thing I can think of is the stDomain and stCompName net to be set, but everything I've tried produces the same result.
Any help would be greatly appreciated!!
here is the the code for the EnumerateServers fuction: The lgTotalEntries returns 0 (which is shouldn't, it should return 2). The error I get is because lgTotalEntries is 2 (Error Code 87).
Public Function EnumerateServers(stType As String) As Long
'* Enumerates Servers by Type Passed into function
Dim lgResult As Long, lgTotalEntries As Long
Dim lgPrefMaxLen As Long, lgEntriesRead As Long
Dim lgResumeHandle As Long, stDomain As String
Dim lgBufPtr As Long, lgServerType As Long
Dim btSrvArray(32) As Byte, btDomain() As Byte, btCompName() As Byte
Dim stCompName As String
Dim stServerName As String, stText As String
Dim ServerStruct As SERVER_INFO_100
On Error GoTo simptrap
'* Clear values and initialize all of the variables
lgResumeHandle = 0
ReDim stServerArray(0) As String
'* Set the domain to the default, primary domain
stDomain = ""
'* Set the computer name to the default, local system
stCompName = ""
'* Create Null Terminated Strings
btDomain = stDomain & vbNullChar
btCompName = stCompName & vbNullChar
'* Determine stType Values and set servertype value
'* You may add or remove these types as needed for your application
If stType = "DCs" Then
'* Enumerate Domain Controllers
lgServerType = Abs(SV_TYPE_DOMAIN_CTRL) + Abs(SV_TYPE_DOMAIN_BAKCTRL)
End If
'* Call NetServerEnum to get a list of Servers
lgResult = NetServerEnum(btCompName(0), 100, lgBufPtr, lgPrefMaxLen, lgEntriesRead, lgTotalEntries, ByVal lgServerType, ByVal btDomain(0), lgResumeHandle)
EnumerateServers = lgResult
'* Now that we know how many entries were read, size the array
If lgTotalEntries <= 0 Then
MsgBox "No Entries Read. Error Code = " & CStr(lgResult)
Exit Function
End If
ReDim stServerArray(lgTotalEntries - 1) As String
'* Check for errors
If lgResult <> 0 And lgResult <> 234 Then '* 234 means multiple reads required
If lgResult = 2351 Then
stText = "ServerName not found." & Chr$(13) & "Be sure you used the leading \\ on the servername."
Else
stText = "Error " & lgResult & " enumerating server " & lgEntriesRead & " of " & lgTotalEntries
End If
MsgBox stText
Exit Function
End If
For I = 1 To lgTotalEntries
'* Dereference the ServerStruct Data Structure
CopyMemory ServerStruct, lgBufPtr, Len(ServerStruct)
'* Dereference the server name variable
CopyMemory btSrvArray(0), ServerStruct.lgSvi100_servername, 33
stServerName = btSrvArray
Trim (stServerName)
'* send the servername to the array
'* Change the code here to send the servername to a control, etc.
stServerArray(I - 1) = stServerName
'* Clear the servername variable here
stServerName = ""
'* Move to the next part of the buffer
lgBufPtr = lgBufPtr + Len(ServerStruct)
Next I
'* Release the memory used for the ServerStruct buffer
lgResult = NetAPIBufferFree(lgBufPtr)
Exit Function
simptrap:
Resume Next
End Function