Hello,
I think the subject says it all I am trying to return a list of domain names
to later display in a combobox. I found a number of VB6 samples that show
how to do this using the WNetOpenEnum and WNetEnumResource API calls. I have
modified the code to work in VB 2003 but I keep getting a 487 -
ERROR_INVALID_ADDRESS error. My envioronment is W2K with a W2003 DC. So I
have 2 questions:
1) Is this the best way to do this in .NET? Is there an easier way to get
what I need?
2) If this is the right way here is the code? Any ideas what my issue may be?
Option Explicit On
Option Strict Off
Imports System
Imports System.Text
Imports System.DirectoryServices
Imports System.Security.Principal
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.Marshal
Module Module1
'***** Declare Variables and API declarations required to build
Domain List
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure
Private Declare Function WNetOpenEnum _
Lib "mpr.dll" Alias "WNetOpenEnumA" _
(ByVal ByValdwScope As Integer, _
ByVal dwType As Integer, _
ByVal dwUsage As Integer, _
ByVal ByReflpNetResource As NETRESOURCE, _
ByRef lphEnum As Integer) As Integer
Private Declare Function WNetEnumResource _
Lib "mpr.dll" Alias "WNetEnumResourceA" _
(ByVal hEnum As Integer, _
ByRef lpcCount As Integer, _
ByRef lpBuffer As NETRESOURCE, _
ByRef lpBufferSize As Integer) As Integer
Private Declare Function WNetCloseEnum _
Lib "mpr.dll" (ByVal hEnum As Integer) As Integer
Private Declare Function StrLenA _
Lib "kernel32" Alias "lstrlenA" _
(ByVal Ptr As Long) As Long
Private Declare Function StrCopyA _
Lib "kernel32" Alias "lstrcpyA" _
(ByVal RetVal As String, _
ByVal Ptr As Long) As Long
Private Const RESOURCE_GLOBALNET As Integer = &H2
Private Const RESOURCETYPE_ANY As Integer = &H0
Private Const RESOURCEUSAGE_ALL As Integer = &H0
Private Const RESOURCE_ENUM_ALL As Integer = &HFFFFS
Private Const MAX_RESOURCES As Integer = 256
Private Const NO_ERROR As Integer = 0
Private Const ERROR_NO_MORE_ITEMS As Integer = 259&
'************************************************* ******
Private Function GetDomainList() As String()
Dim NetResource(MAX_RESOURCES) As NETRESOURCE
Dim intBufferSize As Integer
Dim intEnumHwnd As Integer
Dim intReturn As Integer
Dim intCount As Integer
Dim intLoop As Integer
Dim strDomainInfo() As String
intReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
RESOURCEUSAGE_ALL, NetResource(0), intEnumHwnd)
If intReturn = NO_ERROR Then
intCount = RESOURCE_ENUM_ALL
intBufferSize = UBound(NetResource) * Len(NetResource(0))
intReturn = WNetEnumResource(intEnumHwnd, intCount,
NetResource(0), intBufferSize)
If intCount > 0 Then
ReDim strDomainInfo(intCount)
For intLoop = 0 To intCount - 1
'// Add domain to collection
strDomainInfo(intLoop) =
PointerToAsciiStr(NetResource(intLoop).lpRemoteNam e)
Next intLoop
End If
Return strDomainInfo
End If
End Function
Private Function PointerToAsciiStr(ByVal xi_lngPtrToString As Long)
As String
On Error Resume Next ' Don't accept an error here
Dim p_lngLen As Long
Dim p_strStringValue As String
Dim p_lngNullPos As Long
Dim p_lngRtn As Long
p_lngLen = StrLenA(xi_lngPtrToString)
If xi_lngPtrToString > 0 And p_lngLen > 0 Then
p_strStringValue = Space$(p_lngLen + 1)
p_lngRtn = StrCopyA(p_strStringValue, xi_lngPtrToString)
p_lngNullPos = InStr(p_strStringValue, Chr(0))
If p_lngNullPos > 0 Then
PointerToAsciiStr = Left$(p_strStringValue, _
p_lngNullPos - 1) 'Lose the null terminator...
Else
'Just pass the string...
PointerToAsciiStr = p_strStringValue
End If
Else
PointerToAsciiStr = ""
End If
End Function
Thanks in Advance,
Jeff