Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
DsEnumerateDomainTrusts (netapi32)
 
.
Summary
The DsEnumerateDomainTrusts function obtains domain trust data for a specified domain.

C# Signature:

[DllImport("Netapi32.dll", CallingConvention=CallingConvention.Winapi, SetLastError=true, CharSet=CharSet.Auto)]
private static extern uint DsEnumerateDomainTrusts(string ServerName,
                            uint Flags,
                            out IntPtr Domains,
                            out uint DomainCount);

VB Signature:

Declare Function DsEnumerateDomainTrusts Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

[Flags]
private enum DS_DOMAIN_TRUST_TYPE : uint
{
     DS_DOMAIN_IN_FOREST       = 0x0001,  // Domain is a member of the forest
     DS_DOMAIN_DIRECT_OUTBOUND     = 0x0002,  // Domain is directly trusted
     DS_DOMAIN_TREE_ROOT       = 0x0004,  // Domain is root of a tree in the forest
     DS_DOMAIN_PRIMARY         = 0x0008,  // Domain is the primary domain of queried server
     DS_DOMAIN_NATIVE_MODE     = 0x0010,  // Primary domain is running in native mode
     DS_DOMAIN_DIRECT_INBOUND      = 0x0020   // Domain is directly trusting
}

[StructLayout(LayoutKind.Sequential)]
private struct DS_DOMAIN_TRUSTS
{
     [MarshalAs(UnmanagedType.LPTStr)]
     public string NetbiosDomainName;
     [MarshalAs(UnmanagedType.LPTStr)]
     public string DnsDomainName;
     public uint Flags;
     public uint ParentIndex;
     public uint TrustType;
     public uint TrustAttributes;
     public IntPtr DomainSid;
     public Guid DomainGuid;
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Domains is an out parameter that receives a pointer to an array of DS_DOMAIN_TRUSTS structures. Each structure in this array contains trust data about a domain. The caller must free this memory when it is no longer required by calling NetApiBufferFree.

Tips & Tricks:

Please add some!

Sample Code:

public class DomainLister
{
     public static DS_DOMAIN_TRUSTS[] GetTrustedDomains()
     {
     // What trust types are we interested in ?
     uint trustTypes = (uint)(DS_DOMAIN_TRUST_TYPE.DS_DOMAIN_PRIMARY | DS_DOMAIN_TRUST_TYPE.DS_DOMAIN_DIRECT_OUTBOUND);

     IntPtr buf = new IntPtr();
     uint numDomains = 0;
     DS_DOMAIN_TRUSTS[] trusts = new DS_DOMAIN_TRUSTS[0];

     // Make the call - not doing anything special with the result value here
     uint result = DsEnumerateDomainTrusts(null,
                           trustTypes,
                           out buf,
                           out numDomains);

     try
     {
         if((numDomains > 0) && (result == 0))
         {
         // Marshal the received buffer to managed structs

         trusts = new DS_DOMAIN_TRUSTS[numDomains];

         IntPtr iter = buf;

         for(int i=0; i < numDomains; i++)
         {
             trusts[i] = (DS_DOMAIN_TRUSTS)Marshal.PtrToStructure(iter, typeof(DS_DOMAIN_TRUSTS));
             iter = (IntPtr)(iter.ToInt64() + (long)Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS)));
         }
         }
     }
     finally
     {
         // Make sure we free the buffer whatever happens
         NetApiBufferFree(buf);
     }

     return trusts;
     }

     [StructLayout(LayoutKind.Sequential)]
     public struct DS_DOMAIN_TRUSTS
     {
     [MarshalAs(UnmanagedType.LPTStr)]
     public string NetbiosDomainName;
     [MarshalAs(UnmanagedType.LPTStr)]
     public string DnsDomainName;
     public uint Flags;
     public uint ParentIndex;
     public uint TrustType;
     public uint TrustAttributes;
     public IntPtr DomainSid;
     public Guid DomainGuid;
     }

     [Flags]
     private enum DS_DOMAIN_TRUST_TYPE : uint
     {
     DS_DOMAIN_IN_FOREST       = 0x0001,  // Domain is a member of the forest
     DS_DOMAIN_DIRECT_OUTBOUND     = 0x0002,  // Domain is directly trusted
     DS_DOMAIN_TREE_ROOT       = 0x0004,  // Domain is root of a tree in the forest
     DS_DOMAIN_PRIMARY         = 0x0008,  // Domain is the primary domain of queried server
     DS_DOMAIN_NATIVE_MODE     = 0x0010,  // Primary domain is running in native mode
     DS_DOMAIN_DIRECT_INBOUND      = 0x0020   // Domain is directly trusting
     }

     [DllImport("Netapi32.dll", CallingConvention=CallingConvention.Winapi, SetLastError=true, CharSet=CharSet.Auto)]
     private static extern uint DsEnumerateDomainTrusts(string ServerName,
     uint Flags,
     out IntPtr Domains,
     out uint DomainCount);

     [DllImport("Netapi32.dll",EntryPoint="NetApiBufferFree")]
     private static extern uint NetApiBufferFree(IntPtr buffer);
}

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions