The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Win32::NameTranslate - Convenience perl wrapper around IADsNameTranslate interface


version 0.06


Win32::NameTranslate is a convenience wrapper around the IADsNameTranslate interface, which can be used to convert the names of Active Directory objects from one format to another. IADsNameTranslate is an ADSI implementation of the DsCrackNames API.

IADsNameTranslate is usually accessed via Win32::OLE, this wrapper just makes it slightly easier to use.


  use strict;
  use warnings;
  use Win32::NameTranslate qw[:all];
  use Win32::OLE;

  # Create a new name translator, using Global Catalog
  my $trans = Win32::NameTranslate->new( ADS_NAME_INITTYPE_GC );

  my $canonical = 'localdomain.local/_SomeOU/_AnotherOU/Tommy Tester';

  # Specify Canonical format and name to lookup
  $trans->set( ADS_NAME_TYPE_CANONICAL, $canonical ) || die Win32::OLE->LastError;

  # Lets get the RFC 1779 'LDAP' type name
  my $rfc = $trans->get( ADS_NAME_TYPE_1779 );

  # rfc = 'CN=tommy tester,OU=_AnotherOU,OU=_SomeOU,DC=localdomain,DC=local'

  my @multiple = (
        'localdomain.local/_SomeOU/_AnotherOU/Tommy Tester',
        'localdomain.local/_Admins/_Enterprise/Johnny Admin',

  # We can lookup multiple names by providing an arrayref
  $trans->set( ADS_NAME_TYPE_CANONICAL, \@multiple ) || die Win32::OLE->LastError;

  my @rfcs = $trans->get( ADS_NAME_TYPE_1779 );


A number of constants are defined and exported by this module. You may specify :all to import all the constants into your namespace. Nothing is imported by default.


The ADS_NAME_INITTYPE_ENUM enumeration specifies the types of initialisation to perform on a NameTranslate object.


Initializes a NameTranslate object by setting the domain that the object binds to.


Initializes a NameTranslate object by setting the server that the object binds to.


Initializes a NameTranslate object by locating the global catalog that the object binds to.


The ADS_NAME_TYPE_ENUM enumeration specifies the formats used for representing distinguished names:


Name format as specified in RFC 1779. For example, "CN=Jeff Smith,CN=users,DC=Fabrikam,DC=com".


Canonical name format. For example, " Smith".


Account name format used in Windows NT 4.0. For example, "Fabrikam\JeffSmith".


Display name format. For example, "Jeff Smith".


Simple domain name format. For example, "".


Simple enterprise name format. For example, "".


Global Unique Identifier format. For example, "{95ee9fff-3436-11d1-b2b0-d15ae3ac8436}".


Unknown name type. The system will estimate the format. This element is a meaningful option only with the set method, but not with the get method.


User principal name format. For example, "".


Extended canonical name format. For example, " Jeff Smith".


Service principal name format. For example, "www/".


A SID string, as defined in the Security Descriptor Definition Language (SDDL), for either the SID of the current object or one from the object SID history. For example, "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"



Creates a new Win32::NameTranslate object, initialising a NameTranslate object internally.

Without any arguments it binds to ADS_NAME_INITTYPE_GC.

Can take five arguments.

The first argument is the type of initialisation to be performed, ADS_NAME_INITTYPE_ENUM.

The second argument is the name of the server or domain, depending on the type of initialisation. When ADS_NAME_INITTYPE_SERVER is used, specify the machine name of a directory server. When ADS_NAME_INITTYPE_DOMAIN is used, specify the domain name. This value is ignored when ADS_NAME_INITTYPE_GC is issued.

Optionally, you may provide a further three arguments to specify user credential than the current user, if these are provided, then all must be provided. The arguments are: username, user domain name and user password, respectively.


  # Just use default, which is ADS_NAME_INITTYPE_GC
  my $trans = Win32::NameTranslate->new();

  # Bind to a specific domain
  my $trans = Win32::NameTranslate->new( ADS_NAME_INITTYPE_DOMAIN, 'localdomain.local' );

  # Bind to a specific domain providing credentials
  my $trans = Win32::NameTranslate->new( ADS_NAME_INITTYPE_DOMAIN, 'localdomain.local', 'johnny', 'LOCAL', 'sekret' );


The set method must be called before the get method.


Directs the directory service to set up a specified object for name translation. This is a single wrapper around the underlying IADsNameTranslate::Set and IADsNameTranslate::SetEx methods.

The first argument required is a format type, ADS_NAME_TYPE_ENUM.

The second argument required is either a scalar of the name to translate or an arrayref of a number of names to translate.


  # translate a single name from Canonical name format
  $trans->set( ADS_NAME_TYPE_CANONICAL, " Smith" );

  # translate a number of names from Canonical name format
  $trans->set( ADS_NAME_TYPE_CANONICAL, [ " Smith", " Rotten", " Bookcase" ] );

The method will return a true value on success or false on failure. You may check Win32::OLE->LastError to see what the error was.


Retrieves the name of a directory object in the specified format. set must be called before using this method. This is a single wrapper around the underlying IADsNameTranslate::Get and IADsNameTranslate::GetEx methods. What is returned is determined on whether set was called with a scalar or arrayref.

The first argument required is the format type, ADS_NAME_TYPE_ENUM, of the output name.

If set was called with a single name to translate the result is a single output name.

If set was called with an arrayref the result is a list of output names.


  # translate to RFC 1779
  my $result = $trans->get( ADS_NAME_TYPE_1779 );

  # we passed an arrayref, so get a list back
  my @results = $trans->get( ADS_NAME_TYPE_1779 );

Returns true or false (respectively) depending on whether set was called with an arrayref or not.



Chris Williams <>


This software is copyright (c) 2014 by Chris Williams.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.