469,892 Members | 2,171 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,892 developers. It's quick & easy.

Display All Domain Names on the Network

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
Apr 25 '06 #1
3 4212
Have a look at ManagementObject etc.
Imports System.Management
Apr 26 '06 #2
Thanks, that got me going however I created 2 questions....

First here is a simple console app sample...

*********************************
Imports System
Imports System.Management

Module Module1

Sub Main()
Dim objSearcher As New ManagementObjectSearcher("SELECT * FROM
Win32_NTDomain")
Dim objDomain As ManagementObject

For Each objDomain In objSearcher.Get()
Console.WriteLine("Domain = " & objDomain("Name").ToString())
Next objDomain
Do Until LCase(Console.ReadLine()) = "exit"
Loop

End Sub

End Module
*********************************

For the most part this gets me what I need a list of all available domains
however while testing it I ran into 2 issues:

1) I get the following exception when running the code on a Windows 2000 SP4
with Framework 1.1.4322.573. It works fine on XP boxes...

*********************************
Unhandled Exception: System.Management.ManagementException: Invalid class

at
System.Management.ManagementException.ThrowWithExt endedInfo(ManagementStatus
errorCode)
at System.Management.ManagementObjectEnumerator.MoveN ext()
at DomianNames.Module1.Main()
*********************************

I have looked at just about everything I can think of and I am at a loss.

2) The objSearcher.Get() is very slow. The network I am working on has 25
domains in the forest. It takes about 1.5 minutes to execute. Is there any
way to speed this up?

Thanks again,

Jeff Waskiewicz
Apr 26 '06 #3
Hi to answer your first question, I just checked it (using wbemtest and
connect to root\cimv2) the Win32_NTDomain class doesn't exist on windows
2000, so that should explain your error. I'll see if I can find a win2000
sample
With the second question I can't help you.

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Jeff Waskiewicz" <Je************@discussions.microsoft.com> schreef in
bericht news:36**********************************@microsof t.com...
Thanks, that got me going however I created 2 questions....

First here is a simple console app sample...

*********************************
Imports System
Imports System.Management

Module Module1

Sub Main()
Dim objSearcher As New ManagementObjectSearcher("SELECT * FROM
Win32_NTDomain")
Dim objDomain As ManagementObject

For Each objDomain In objSearcher.Get()
Console.WriteLine("Domain = " & objDomain("Name").ToString())
Next objDomain
Do Until LCase(Console.ReadLine()) = "exit"
Loop

End Sub

End Module
*********************************

For the most part this gets me what I need a list of all available domains
however while testing it I ran into 2 issues:

1) I get the following exception when running the code on a Windows 2000 SP4 with Framework 1.1.4322.573. It works fine on XP boxes...

*********************************
Unhandled Exception: System.Management.ManagementException: Invalid class

at
System.Management.ManagementException.ThrowWithExt endedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectEnumerator.MoveN ext()
at DomianNames.Module1.Main()
*********************************

I have looked at just about everything I can think of and I am at a loss.

2) The objSearcher.Get() is very slow. The network I am working on has 25
domains in the forest. It takes about 1.5 minutes to execute. Is there any way to speed this up?

Thanks again,

Jeff Waskiewicz

Apr 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by steve.anon | last post: by
1 post views Thread by DMc2004 | last post: by
2 posts views Thread by Dan | last post: by
16 posts views Thread by Michael | last post: by
9 posts views Thread by Sam | last post: by
4 posts views Thread by JB | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.