Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:42:31+00:00 2026-06-05T18:42:31+00:00

My app is running on a computer that is joined to an Active Directory

  • 0

My app is running on a computer that is joined to an Active Directory domain. Is there a way to get that domain’s DNS name using WinAPI methods? I want something that will work even if there are no DNS servers or Domain Controllers available.

Right now, the only way I can find is through the Domain property of the Win32_ComputerSystem WMI class:

using System.Management;

public class WMIUtility
{
    public static ManagementScope GetDefaultScope(string computerName)
    {
        ConnectionOptions connectionOptions = new ConnectionOptions();
        connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;
        connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
        string path = string.Format("\\\\{0}\\root\\cimv2", computerName);
        return new ManagementScope(path, connectionOptions);
    }

    public static ManagementObject GetComputerSystem(string computerName)
    {
        string path = string.Format("Win32_ComputerSystem.Name='{0}'", computerName);
        return new ManagementObject(
            GetDefaultScope(computerName),
            new ManagementPath(path),
            new ObjectGetOptions()
        );
    }

    public static string GetDNSDomainName(string computerName)
    {
        using (ManagementObject computerSystem = GetComputerSystem(computerName))
        {
            object isInDomain = computerSystem.Properties["PartOfDomain"].Value;
            if (isInDomain == null) return null;
            if(!(bool)isInDomain) return null;
            return computerSystem.Properties["Domain"].Value.ToString();
        }
    }
}

The only thing I can find in WinAPI is the NetGetJoinInformation method, which returns the NetBIOS domain name:

using System.Runtime.InteropServices;

public class PInvoke
{
    public const int NERR_SUCCESS = 0;

    public enum NETSETUP_JOIN_STATUS
    {
        NetSetupUnknownStatus = 0,
        NetSetupUnjoined,
        NetSetupWorkgroupName,
        NetSetupDomainName
    }

    [DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
    protected static extern int NetGetJoinInformation(string lpServer, out IntPtr lpNameBuffer, out NETSETUP_JOIN_STATUS BufferType);

    [DllImport("netapi32.dll", SetLastError = true)]
    protected static extern int NetApiBufferFree(IntPtr Buffer);

    public static NETSETUP_JOIN_STATUS GetComputerJoinInfo(string computerName, out string name)
    {
        IntPtr pBuffer;
        NETSETUP_JOIN_STATUS type;
        int lastError = NetGetJoinInformation(computerName, out pBuffer, out type);
        if(lastError != NERR_SUCCESS)
        {
            throw new System.ComponentModel.Win32Exception(lastError);
        }
        try
        {
            if(pBuffer == IntPtr.Zero)
            {
                name = null;
            }
            else
            {
                switch(type)
                {
                    case NETSETUP_JOIN_STATUS.NetSetupUnknownStatus:
                    case NETSETUP_JOIN_STATUS.NetSetupUnjoined:
                    {
                        name = null;
                        break;
                    }
                    default:
                    {
                        name = Marshal.PtrToStringUni(pBuffer);
                        break;
                    }
                }
            }
            return type;
        }
        finally
        {
            if(pBuffer != IntPtr.Zero)
            {
                NetApiBufferFree(pBuffer);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:42:34+00:00Added an answer on June 5, 2026 at 6:42 pm

    I think what you are looking for is GetComputerNameEx with ComputerNameDnsDomain as first parameter. But it’s possible that you want one of the other types there. Still, GetComputerNameEx is the function you are looking for from how I understand the question.

    PInvoke details here.

    You raise a fair point in your comment, so sure, in this case LsaQueryInformationPolicy with PolicyDnsDomainInformation may be the better way to get the DNS name of the domain of which the computer is a member.

    But it is a special case and your question didn’t mention any such special cases. This should only ever be the case when the primary DNS suffix has been set and differs from the DNS domain name of which the machine is a member. For all practical purposes GetComputerNameEx will do exactly what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iPhone app running in the simulator that won't quit. I also
I got an app running on my SQL Server that is starting to slow
I have a BlackBerry app running in the background that needs to know when
I have a BlackBerry app running in the background that needs to know when
Other than running it on a computer that only has .NET Framework Version 3.5,
Is there any way to detect if the host PC a java app is
I have an Android app running on a phone that's connected to a host
So... this is beyond strange. I had an app that was running perfectly fine
How can my app get a valid last time connected to domain timestamp from
What I know is that I want to create an app running on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.