views:

82

answers:

2

I used to call GetComputerObjectName(NameUniqueId, ...) to get an Active Directory ID of local machine. Trying to get same functionality in .NET, does it exist, or is p-invoke the best way to achieve this goal?

+2  A: 

System.Environment.MachineName?

Jay Riggs
I wanted NameUniqueId, not NetBIOS name
galets
Sorry, I can't find how you might do this without pinvoke.
Jay Riggs
A: 

Had to P-Invoke...

    [DllImport("secur32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool GetComputerObjectName(int nameFormat, StringBuilder lpNameBuffer, ref int lpnSize);

    public static string GetComputerObjectName()
    {
        const int NameUniqueId = 6;
        var lpBuffer = new StringBuilder(260);
        var bufferLength = lpBuffer.Capacity;

        if (!GetComputerObjectName(NameUniqueId, lpBuffer, ref bufferLength))
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        return lpBuffer.ToString();
    }
galets