1 2 unit Unit1; 3 4 interface 5 6 uses 7 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 8 Dialogs, StdCtrls; 9 10 type 11 TForm1 = class(TForm) 12 Button1: TButton; 13 procedure Button1Click(Sender: TObject); 14 private 15 { Private declarations } 16 public 17 function GetCurrentUserName: string; 18 { Public declarations } 19 end; 20 21 var 22 Form1: TForm1; 23 24 implementation 25 26 {$R *.dfm} 27 28 function TForm1.GetCurrentUserName: string; 29 const 30 cnMaxUserNameLen = 254; 31 var 32 sUserName: string; 33 dwUserNameLen: DWORD; 34 begin 35 dwUserNameLen := cnMaxUserNameLen - 1; 36 SetLength(sUserName, cnMaxUserNameLen); 37 GetUserName(PChar(sUserName), dwUserNameLen); 38 SetLength(sUserName, dwUserNameLen); 39 Result := sUserName; 40 end; 41 42 procedure TForm1.Button1Click(Sender: TObject); 43 begin 44 ShowMessage(GetCurrentUserName); 45 end; 46 47 48 end. 49