473,766 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 WNetEnumResourc e API calls. I have
modified the code to work in VB 2003 but I keep getting a 487 -
ERROR_INVALID_A DDRESS 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.Director yServices
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(L ayoutKind.Seque ntial, CharSet:=CharSe t.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 "WNetOpenEn umA" _
(ByVal ByValdwScope As Integer, _
ByVal dwType As Integer, _
ByVal dwUsage As Integer, _
ByVal ByReflpNetResou rce As NETRESOURCE, _
ByRef lphEnum As Integer) As Integer

Private Declare Function WNetEnumResourc e _
Lib "mpr.dll" Alias "WNetEnumResour ceA" _
(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_GLOBAL NET As Integer = &H2
Private Const RESOURCETYPE_AN Y As Integer = &H0
Private Const RESOURCEUSAGE_A LL As Integer = &H0
Private Const RESOURCE_ENUM_A LL As Integer = &HFFFFS
Private Const MAX_RESOURCES As Integer = 256

Private Const NO_ERROR As Integer = 0
Private Const ERROR_NO_MORE_I TEMS 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(RE SOURCE_GLOBALNE T, RESOURCETYPE_AN Y,
RESOURCEUSAGE_A LL, NetResource(0), intEnumHwnd)

If intReturn = NO_ERROR Then
intCount = RESOURCE_ENUM_A LL
intBufferSize = UBound(NetResou rce) * Len(NetResource (0))
intReturn = WNetEnumResourc e(intEnumHwnd, intCount,
NetResource(0), intBufferSize)
If intCount > 0 Then
ReDim strDomainInfo(i ntCount)
For intLoop = 0 To intCount - 1
'// Add domain to collection
strDomainInfo(i ntLoop) =
PointerToAsciiS tr(NetResource( intLoop).lpRemo teName)
Next intLoop
End If
Return strDomainInfo
End If

End Function
Private Function PointerToAsciiS tr(ByVal xi_lngPtrToStri ng As Long)
As String

On Error Resume Next ' Don't accept an error here

Dim p_lngLen As Long
Dim p_strStringValu e As String
Dim p_lngNullPos As Long
Dim p_lngRtn As Long

p_lngLen = StrLenA(xi_lngP trToString)
If xi_lngPtrToStri ng > 0 And p_lngLen > 0 Then
p_strStringValu e = Space$(p_lngLen + 1)
p_lngRtn = StrCopyA(p_strS tringValue, xi_lngPtrToStri ng)
p_lngNullPos = InStr(p_strStri ngValue, Chr(0))
If p_lngNullPos > 0 Then
PointerToAsciiS tr = Left$(p_strStri ngValue, _
p_lngNullPos - 1) 'Lose the null terminator...
Else
'Just pass the string...
PointerToAsciiS tr = p_strStringValu e
End If
Else
PointerToAsciiS tr = ""
End If

End Function

Thanks in Advance,

Jeff
Apr 25 '06 #1
3 4377
Have a look at ManagementObjec t etc.
Imports System.Manageme nt
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.Manageme nt

Module Module1

Sub Main()
Dim objSearcher As New ManagementObjec tSearcher("SELE CT * FROM
Win32_NTDomain" )
Dim objDomain As ManagementObjec t

For Each objDomain In objSearcher.Get ()
Console.WriteLi ne("Domain = " & objDomain("Name ").ToString ())
Next objDomain
Do Until LCase(Console.R eadLine()) = "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.Manageme nt.ManagementEx ception: Invalid class

at
System.Manageme nt.ManagementEx ception.ThrowWi thExtendedInfo( ManagementStatu s
errorCode)
at System.Manageme nt.ManagementOb jectEnumerator. MoveNext()
at DomianNames.Mod ule1.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.mi crosoft.com> schreef in
bericht news:36******** *************** ***********@mic rosoft.com...
Thanks, that got me going however I created 2 questions....

First here is a simple console app sample...

*************** *************** ***
Imports System
Imports System.Manageme nt

Module Module1

Sub Main()
Dim objSearcher As New ManagementObjec tSearcher("SELE CT * FROM
Win32_NTDomain" )
Dim objDomain As ManagementObjec t

For Each objDomain In objSearcher.Get ()
Console.WriteLi ne("Domain = " & objDomain("Name ").ToString ())
Next objDomain
Do Until LCase(Console.R eadLine()) = "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.Manageme nt.ManagementEx ception: Invalid class

at
System.Manageme nt.ManagementEx ception.ThrowWi thExtendedInfo( ManagementStatu s errorCode)
at System.Manageme nt.ManagementOb jectEnumerator. MoveNext()
at DomianNames.Mod ule1.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
1702
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 'username.blogger.com'. Since blogger.com is doing it on the fly (as you sign up), I'm sure they are doing it programmatically. Anyone has any clue on how they do that? TIA,
1
1928
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 workstation; 3) The domain the machine is in; 4) The names of the machines currently active within that domain.
3
1606
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 about transfer domain is I can transfer the domain to the webhost vendor, and they can config it on their server to point this domain to my web pages, is this right? Is the transfer domain free? Thanks.
2
3183
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 someone can say me what kind of code i had to write this would be great. thanks
2
1331
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 get this via Active Directory but being somewhat new to that I am not sure where it is or how to get at it. Any help is appreciated. Thanks! Dan
16
2308
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 name. Thanks, -Michael
3
5308
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 right and even creates an account for the user. But these days i have encounter a problem with the domain controller. Recently i have joined a computer in the domain and i have even given a user account to a person who use this new PC. Till date there...
9
5421
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 , what is the pattren to check the given string is a domain style or not?
4
7697
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 want to be able to produce a list and work it from there. I found the following method: String path = "WinNT://MY.DOMAIN.COM"; String username = "administrator";
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.