using System.Runtime.InteropServices; using System.Text; namespace Vanara.PInvoke { public static partial class Secur32 { /// Specifies a format for a directory service object name. // https://docs.microsoft.com/en-us/windows/desktop/api/secext/ne-secext-extended_name_format typedef enum { NameUnknown, // NameFullyQualifiedDN, NameSamCompatible, NameDisplay, NameUniqueId, NameCanonical, NameUserPrincipal, NameCanonicalEx, // NameServicePrincipal, NameDnsDomain, NameGivenName, NameSurname } *PEXTENDED_NAME_FORMAT; [PInvokeData("secext.h", MSDNShortId = "1270c412-2fa5-4f5d-a86e-1ab3146c6683")] public enum EXTENDED_NAME_FORMAT { /// An unknown name type. NameUnknown = 0, /// The fully qualified distinguished name (for example, CN=Jeff Smith,OU=Users,DC=Engineering,DC=Microsoft,DC=Com). NameFullyQualifiedDN, /// /// A legacy account name (for example, Engineering\JSmith). The domain-only version includes trailing backslashes (\\). /// NameSamCompatible, /// /// A "friendly" display name (for example, Jeff Smith). The display name is not necessarily the defining relative distinguished /// name (RDN). /// NameDisplay, /// A GUID string that the IIDFromString function returns (for example, {4fa050f0-f561-11cf-bdd9-00aa003a77b6}). NameUniqueId = 6, /// /// The complete canonical name (for example, engineering.microsoft.com/software/someone). The domain-only version includes a /// trailing forward slash (/). /// NameCanonical, /// The user principal name (for example, someone@example.com). NameUserPrincipal, /// /// The same as NameCanonical except that the rightmost forward slash (/) is replaced with a new line character (\n), even in a /// domain-only case (for example, engineering.microsoft.com/software\nJSmith). /// NameCanonicalEx, /// The generalized service principal name (for example, www/www.microsoft.com@microsoft.com). NameServicePrincipal, /// The DNS domain name followed by a backward-slash and the SAM user name. NameDnsDomain = 12, /// NameGivenName, /// NameSurname, } /// Retrieves the local computer's name in a specified format. /// /// The format for the name. This parameter is a value from the EXTENDED_NAME_FORMAT enumeration type. It cannot be NameUnknown. /// /// /// A pointer to a buffer that receives the name in the specified format. /// /// If this parameter is NULL, either the function succeeds and the lpnSize parameter receives the required size, or the /// function fails with ERROR_INSUFFICIENT_BUFFER and lpnSize receives the required size. The behavior depends on the value of /// NameFormat and the version of the operating system. /// /// /// /// On input, specifies the size of the lpNameBuffer buffer, in TCHARs. On success, receives the size of the name copied to /// the buffer. If the lpNameBuffer buffer is too small to hold the name, the function fails and lpnSize receives the required buffer size. /// /// /// If the function succeeds, the return value is a nonzero value. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/secext/nf-secext-getcomputerobjectnamea BOOLEAN SEC_ENTRY // GetComputerObjectNameA( EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize ); [DllImport(Lib.Secur32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("secext.h", MSDNShortId = "aead19ae-a27c-486e-aa2e-220d337044fc")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool GetComputerObjectName(EXTENDED_NAME_FORMAT NameFormat, StringBuilder lpNameBuffer, ref uint nSize); /// /// /// Retrieves the name of the user or other security principal associated with the calling thread. You can specify the format of the /// returned name. /// /// If the thread is impersonating a client, GetUserNameEx returns the name of the client. /// /// /// The format of the name. This parameter is a value from the EXTENDED_NAME_FORMAT enumeration type. It cannot be /// NameUnknown. If the user account is not in a domain, only NameSamCompatible is supported. /// /// /// A pointer to a buffer that receives the name in the specified format. The buffer must include space for the terminating null character. /// /// /// /// On input, this variable specifies the size of the lpNameBuffer buffer, in TCHARs. If the function is successful, the /// variable receives the number of TCHARs copied to the buffer, not including the terminating null character. /// /// /// If lpNameBuffer is too small, the function fails and GetLastError returns ERROR_MORE_DATA. This parameter receives the required /// buffer size, in Unicode characters (whether or not Unicode is being used), including the terminating null character. /// /// /// /// If the function succeeds, the return value is a nonzero value. /// /// If the function fails, the return value is zero. To get extended error information, call GetLastError. Possible values include /// the following. /// /// /// /// Return code /// Description /// /// /// ERROR_MORE_DATA /// The lpNameBuffer buffer is too small. The lpnSize parameter contains the number of bytes required to receive the name. /// /// /// ERROR_NO_SUCH_DOMAIN /// The domain controller is not available to perform the lookup /// /// /// ERROR_NONE_MAPPED /// The user name is not available in the specified format. /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/secext/nf-secext-getusernameexa BOOLEAN SEC_ENTRY GetUserNameExA( // EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize ); [DllImport(Lib.Secur32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("secext.h", MSDNShortId = "7e7d618b-2e64-4b0b-aed3-f3221b0443ca")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool GetUserNameEx(EXTENDED_NAME_FORMAT NameFormat, StringBuilder lpNameBuffer, ref uint nSize); /// Converts a directory service object name from one format to another. /// The name to be translated. /// /// The format of the name to be translated. This parameter is a value from the EXTENDED_NAME_FORMAT enumeration type. /// /// /// The format of the converted name. This parameter is a value from the EXTENDED_NAME_FORMAT enumeration type. It cannot be NameUnknown. /// /// A pointer to a buffer that receives the converted name. /// /// /// On input, the variable indicates the size of the lpTranslatedName buffer, in TCHARs. On output, the variable returns the /// size of the returned string, in TCHARs, including the terminating null character. /// /// If lpTranslated is NULL and nSize is 0, the function succeeds and nSize receives the required buffer size. /// /// If the lpTranslatedName buffer is too small to hold the converted name, the function fails and nSize receives the required buffer size. /// /// /// /// If the function succeeds, the return value is a nonzero value. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// TranslateName fails if it cannot bind to Active Directory on a domain controller. // https://docs.microsoft.com/en-us/windows/desktop/api/secext/nf-secext-translatenamea BOOLEAN SEC_ENTRY TranslateNameA( LPCSTR // lpAccountName, EXTENDED_NAME_FORMAT AccountNameFormat, EXTENDED_NAME_FORMAT DesiredNameFormat, LPSTR lpTranslatedName, PULONG // nSize ); [DllImport(Lib.Secur32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("secext.h", MSDNShortId = "4df25519-e7d6-46ea-b0e8-ba1f82e5f94f")] [return: MarshalAs(UnmanagedType.U1)] public static extern bool TranslateName(string lpAccountName, EXTENDED_NAME_FORMAT AccountNameFormat, EXTENDED_NAME_FORMAT DesiredNameFormat, StringBuilder lpTranslatedName, ref uint nSize); } }