473,382 Members | 1,349 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,382 software developers and data experts.

Possible to call DsEnumerateDomainTrusts from VB.NET?

I'm trying to use DsEnumerateDomainTrusts to generate a
list of trusted domains in VB.NET. The code I've listed
below generates the error, "An unhandled exception of
type 'System.NullReferenceException' occurred in
NetDisco.exe. Additional information: Object reference
not set to an instance of an object." Any ideas on how
to call this API function properly from VB.NET? Thank
you.

Luke Morehead

=-=-=-=-=-=-=-=-

Private Declare Function DsEnumerateDomainTrusts
Lib "netapi32.dll" Alias "DsEnumerateDomainTrustsW" ( _
ByVal lpszServerName As IntPtr, _
ByVal luFlags As UInt64, _
ByRef lpBuffer As IntPtr, _
ByRef pluDomainCount As IntPtr) As Integer

Private Const DS_DOMAIN_DIRECT_OUTBOUND = 2
Private Const NERR_Success = 0

Private Structure DS_DOMAIN_TRUSTS
Dim lpszNetBIOSDomainName As IntPtr
Dim lpszDNSDomainName As IntPtr
Dim luFlags As UInt64
Dim luParentIndex As UInt64
Dim luTrustType As UInt64
Dim luTrustAttributes As UInt64
Dim pSID As IntPtr
Dim gGUID As Guid
End Structure

Private Sub GetTrustedDomains(ByVal strDomainController
as String)
Dim lpDomainCount As IntPtr
Dim lpBuffer as IntPtr = IntPtr.Zero
Dim intResult as Integer
intResult = DsEnumerateDomainTrusts _
(Marshal.StringToHGlobalUni(strDomainController),
_
Convert.ToUInt64(DS_DOMAIN_DIRECT_OUTBOUND), _
lpBuffer, lpDomainCount)
If intResult = NERR_Success Then
Dim DomainTrusts As DS_DOMAIN_TRUSTS
Dim intDomainCount As Integer
Dim strTrustedDomain As String
intDomainCount = lpDomainCount.ToInt32
For i = 1 To intDomainCount
DomainTrusts = CType(Marshal.PtrToStructure _
(lpBuffer, GetType(DS_DOMAIN_TRUSTS)), _
DS_DOMAIN_TRUSTS)
Console.WriteLine("Trusted Domain: " & _
Marshal.PtrToStringUni
(DomainTrusts.lpszNetBIOSDomainName)
Next i
Else
Console.WriteLine("DsEnumerateDomainTrusts
failed: " _
& intResult)
End If
End Sub

Nov 20 '05 #1
2 2325
I've made a few corrections to your code. This isn't tested but it should point you
in the right direction. You might want to check into the DirectoryServices
namespace for this type of thing rather than using interop to the underlying API.

Private Declare Auto Function DsEnumerateDomainTrusts _
Lib "netapi32.dll" (ByVal lpszServerName As String, _
ByVal luFlags As Integer, _
ByRef lpBuffer As IntPtr, _
ByRef pluDomainCount As Integer) As Integer

Private Declare Function NetApiBufferFree _
Lib "netapi32.dll" (ByRef lpBuffer As IntPtr) As Integer

Private Const DS_DOMAIN_DIRECT_OUTBOUND As Integer = 2
Private Const NERR_Success As Integer = 0

<StructLayout(LayoutKind.Sequential)> _
Private Structure DS_DOMAIN_TRUSTS
<MarshalAs(UnmanagedType.LPTStr)> Dim lpszNetBIOSDomainName As String
<MarshalAs(UnmanagedType.LPTStr)> Dim lpszDNSDomainName As String
Dim luFlags As Integer
Dim luParentIndex As Integer
Dim luTrustType As Integer
Dim luTrustAttributes As Integer
Dim pSID As IntPtr
Dim gGUID As Guid
End Structure

Private Sub GetTrustedDomains(ByVal strDomainController As String)
Dim lpDomainCount As Integer
Dim lpBuffer As IntPtr
Dim lpStruct As IntPtr
Dim intResult As Integer
Dim DomainTrusts As DS_DOMAIN_TRUSTS
Dim i As Integer

intResult = DsEnumerateDomainTrusts(strDomainController, _
DS_DOMAIN_DIRECT_OUTBOUND, lpBuffer, lpDomainCount)
If intResult = NERR_Success Then
lpStruct = lpBuffer
For i = 1 To lpDomainCount
DomainTrusts = CType(Marshal.PtrToStructure _
(lpStruct, GetType(DS_DOMAIN_TRUSTS)), DS_DOMAIN_TRUSTS)
Console.WriteLine("Trusted Domain: " & _
(DomainTrusts.lpszNetBIOSDomainName))
lpStruct = New IntPtr(lpStruct.ToInt64 + Marshal.SizeOf(DomainTrusts))
Next i
NetApiBufferFree(lpBuffer)
Else
Console.WriteLine("DsEnumerateDomainTrusts failed: " _
& intResult.ToString)
End If
End Sub

"Luke Morehead" <lu**@fixedbid.com> wrote in message news:03****************************@phx.gbl...
I'm trying to use DsEnumerateDomainTrusts to generate a
list of trusted domains in VB.NET. The code I've listed
below generates the error, "An unhandled exception of
type 'System.NullReferenceException' occurred in
NetDisco.exe. Additional information: Object reference
not set to an instance of an object." Any ideas on how
to call this API function properly from VB.NET? Thank
you.

Luke Morehead

=-=-=-=-=-=-=-=-

Private Declare Function DsEnumerateDomainTrusts
Lib "netapi32.dll" Alias "DsEnumerateDomainTrustsW" ( _
ByVal lpszServerName As IntPtr, _
ByVal luFlags As UInt64, _
ByRef lpBuffer As IntPtr, _
ByRef pluDomainCount As IntPtr) As Integer

Private Const DS_DOMAIN_DIRECT_OUTBOUND = 2
Private Const NERR_Success = 0

Private Structure DS_DOMAIN_TRUSTS
Dim lpszNetBIOSDomainName As IntPtr
Dim lpszDNSDomainName As IntPtr
Dim luFlags As UInt64
Dim luParentIndex As UInt64
Dim luTrustType As UInt64
Dim luTrustAttributes As UInt64
Dim pSID As IntPtr
Dim gGUID As Guid
End Structure

Private Sub GetTrustedDomains(ByVal strDomainController
as String)
Dim lpDomainCount As IntPtr
Dim lpBuffer as IntPtr = IntPtr.Zero
Dim intResult as Integer
intResult = DsEnumerateDomainTrusts _
(Marshal.StringToHGlobalUni(strDomainController),
_
Convert.ToUInt64(DS_DOMAIN_DIRECT_OUTBOUND), _
lpBuffer, lpDomainCount)
If intResult = NERR_Success Then
Dim DomainTrusts As DS_DOMAIN_TRUSTS
Dim intDomainCount As Integer
Dim strTrustedDomain As String
intDomainCount = lpDomainCount.ToInt32
For i = 1 To intDomainCount
DomainTrusts = CType(Marshal.PtrToStructure _
(lpBuffer, GetType(DS_DOMAIN_TRUSTS)), _
DS_DOMAIN_TRUSTS)
Console.WriteLine("Trusted Domain: " & _
Marshal.PtrToStringUni
(DomainTrusts.lpszNetBIOSDomainName)
Next i
Else
Console.WriteLine("DsEnumerateDomainTrusts
failed: " _
& intResult)
End If
End Sub

Nov 20 '05 #2
Thanks for your insights, especially regarding the nuances of API data
type marshalling. That did the trick!

Luke

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: FrankS | last post by:
Hi All, I have a problem with an call-template cmd at xslt 1.0: With: pCall = 'ExInput' ------ I try to: <xsl:call-template name="{$pCall}"> <xsl:with-param name="pVal" select="$pValue"/>...
7
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
4
by: murat oguzalp | last post by:
i want to call base and this constructor at the same time. is is possible? i mean: B(int a, int b, int c):base(a):this(b) { // do something with c } at java i used to do that:
8
by: Arno R | last post by:
Hi all, Is the above possible? I guess I need an API call for this ? How and where to find this? One of my new apps is used as a sort of 'dedicated' thing. Users see an empty desktop with...
11
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
2
by: Jack | last post by:
Hello, I have a 3-level class hirarchy: 1=Grandparent 2=Parent 3=Child The Grandparent has a method called OnPaint which I want to override from the Child class. I can do this, but how...
0
by: hastha23 | last post by:
Dear Friends, My oracle Version is 10g. I calling a function from sql select same time function body contain DML statement,that time is possible call function from sql? and again one, ...
5
by: Arquitecto | last post by:
Hi ,i would like to give u a piece of code and discuss if it is possible to call the hidden_function without modify code to call it . I have read on internet about buffer overflows and i can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.