Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2003

Tip: Looking for answers? Try searching our database.

Possible to call DsEnumerateDomainTrusts from VB.NET?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Luke Morehead - 28 Oct 2003 02:03 GMT
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
Stephen Martin - 28 Oct 2003 12:00 GMT
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

> I'm trying to use DsEnumerateDomainTrusts to generate a
> list of trusted domains in VB.NET.  The code I've listed
[quoted text clipped - 59 lines]
>     End If
> End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.