(* * author:http://www.michael-puff.de * date:2006-03-29 * license:public DOMAIN *)functionGetUserNameEx (Nameformat:dword; lpnamebuffer:lpstr;varNsize:dword): Boolean;stdcall;External' Secur32.dll 'Name' Getusernameexa ';functionGetloggedonusernameex (Fformat:dword;varUsername:string): DWORD;ConstNameunknown =0;//Unknown name type. NAMEFULLYQUALIFIEDDN =1;//Fully qualified distinguished nameNamesamcompatible =2;//Windows nt®4.0 account nameNamedisplay =3;//A "friendly" Display nameNameuniqueid =6;//GUID string that the Iidfromstring function returnsNamecanonical =7;//Complete canonical nameNameuserprincipal =8;//User principal nameNamecanonicalex =9; Nameserviceprincipal =Ten;//Generalized Service Principal nameDnsDomainName = One;//DNS domain name, plus the user namevarSize:dword; Buffer:Array[0..1024x768] ofChar;beginResult: =0; Size: = sizeof (Buffer);ifGetUserNameEx (Fformat, Buffer, Size) ThenSetString (Username, Buffer, Size)ElseResult: = GetLastError;End;procedureTform1.button1click (Sender:tobject);varUsername:string; Err:dword;beginERR: = Getloggedonusernameex (3, Username);ifErr =0 ThenShowMessage (Username)ElseShowMessage (Syserrormessage (err));End;
Http://www.michael-puff.de/Programmierung/Delphi/Code-Snippets/GetUserNameEx.shtml
Delphi-getusernameex (Learn about exporting the Windows API, and the use of array char to act as a buffer, the subscript must start from zero)