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
Download Visual Studio Add-In

TranslateName (secur32)
 
.
Summary
Converts a directory service object name from one format to another.

C# Signature:

[DllImport("secur32.dll", SetLastError=true)]
static extern int TranslateName(string accountName, int nameFormat,
        int desiredFormat, StringBuilder translatedName, ref int userNameSize);

VB Signature:

Declare Function TranslateName Lib "secur32.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

This may be required to obtain the UPN (or other format) of a user's credentials. Minimum supported OS is Windows 2000. Full documentation of the native API is available from http://msdn.microsoft.com/en-us/library/ms725484(VS.85).aspx.

Tips & Tricks:

Please add some!

Sample Code:

  public class Sample
  {
      enum EXTENDED_NAME_FORMAT
      {
          NameUnknown = 0,
          NameFullyQualifiedDN = 1,
          NameSamCompatible = 2,
          NameDisplay = 3,
          NameUniqueId = 6,
          NameCanonical = 7,
          NameUserPrincipal = 8,
          NameCanonicalEx = 9,
          NameServicePrincipal = 10,
          NameDnsDomain = 12
      }

      [DllImport("secur32.dll", SetLastError=true)]
      public static extern int TranslateName(string accountName, int nameFormat,
        int desiredFormat, StringBuilder translatedName, ref int userNameSize);

      public string GetTranslatedName(string domain, string user)
      {
          string samCompatibleName = domain.ToUpper() + "\\" + username;
          StringBuilder translatedName = new StringBuilder(1024);
          int userNameSize = translatedName.Capacity;

          if (0 != TranslateName(samCompatibleName,
              (int)EXTENDED_NAME_FORMAT.NameSamCompatible,
              (int)EXTENDED_NAME_FORMAT.NameUserPrincipal,
              translatedName, ref userNameSize))
          {
              // Make sure we got a UPN out as expected
              if (translatedName.ToString().Split('@').Length == 2)
              {
                  return translatedName.ToString();
              }
          }

          return null;
       }
  }

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