473,395 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 4349
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.