unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
NameUnknown = 0;
// Unknown name type.
NameFullyQualifiedDN = 1;
// Fully qualified distinguished name
NameSamCompatible = 2;
// Windows NT® 4.0 account name
NameDisplay = 3;
// A "friendly" display name
NameUniqueId = 6;
// GUID string that the IIDFromString function returns
NameCanonical = 7;
// Complete canonical name
NameUserPrincipal = 8;
// User principal name
NameCanonicalEx = 9;
NameServicePrincipal = 10;
// Generalized service principal name
DNSDomainName = 11;
// DNS domain name, plus the user name
function GetUserNameEx(NameFormat: DWORD;
lpNameBuffer: LPSTR;
var nSize: DWORD):Boolean;
stdcall;
external '
secur32.dll'
Name '
GetUserNameExA';
function GetLoggedOnUserNameEx(fFormat: DWORD;
var Username:
String): DWORD;
var
Size: DWORD;
Buffer:
array [0..1024]
of Char;
begin
Result := 0;
Size := sizeof(Buffer);
if GetUserNameEx(fFormat, Buffer, Size)
then
SetString(Username, Buffer, Size-1)
else
Result := GetLastError;
end;
procedure TForm1.Button2Click(Sender: TObject);
const
NameFormats :
Array[0..9]
of DWORD = (
NameUnknown,
NameFullyQualifiedDN,
NameSamCompatible,
NameDisplay,
NameUniqueId,
NameCanonical,
NameUserPrincipal,
NameCanonicalEx,
NameServicePrincipal,
DNSDomainName);
var
Username:
String;
err: DWORD;
i : shortint;
sMessage :
string;
begin
for i := 0
to high(NameFormats)
do
begin
err := GetLoggedOnUserNameEx(NameFormats[i], Username);
if err = 0
then
sMessage := sMessage+format('
%d: %s', [NameFormats[i], Username])+#13
else
sMessage := sMessage+format('
%d: %s', [NameFormats[i], SysErrorMessage(err)])+#13;
end;
ShowMessage(sMessage)
end;
end.