472,356 Members | 708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

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 4287
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: steve.anon | last post by:
Hi - Any help or pointer on this would be greatly appreciated. I'm working on something similar to blogger.com. Users sign up and they get their own webpage, with a domain name of...
1
by: DMc2004 | last post by:
I have serveral items I would to find out how to do: 1) How to get the username of the person currently logged onto a workstation; 2) How to get the username of who is logged onto a different...
3
by: Nick | last post by:
hi, all I just started to create my own website and I registered a new domain from yahoo, and then I want to register a webhost plan. And how to use this domain I registered? What I undertood...
2
by: Busse | last post by:
hello, i have a problem, at the moment i get the lokal IP and scan a IP-Range, to get the Computer names in the network, but this need time, and i like so sort by WOrkgroup or domain. So if...
2
by: Dan | last post by:
All, I need to be able to retrieve a list of Domains that are part of our network: similar to the list you get when you log onto a Windows station that is part of a domain. I assume that I can...
16
by: Michael | last post by:
Does someone have a reliable routine to get the domain name of the current user of a web page? Something like NSLookup. I have no problem getting their IP address, but I also want the domaine...
3
by: rabselling | last post by:
Hello everyone, I have a domain where there in the network,140 computers working. When ever the new computer come and want to have network, i make a connection with the domain and i give every...
9
by: Sam | last post by:
Validate domain name context Hello , Today I want to make sure from a given text is a domain style or not. for example : the domain can be example.com or example.com.us or example.us so ,...
4
by: JB | last post by:
I am trying to get a list of all the active computers running on my domain. I'm writing some remote management style software with WMI, which works fine when i know the computer name, but i just...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.