Frequently Asked Questions
Retrieving a users login name.
Question:
How do I get the user's login name under 32-bit versions of
Windows?
Answer:
This is easily accomplished by calling the GetUserName() Windows
API function:
procedure TForm1.Button1Click(Sender: TObject);
var
buffer : array[0..255] of char;
buffSize : DWORD;
begin
buffSize := sizeOf(buffer);
GetUserName(@buffer, buffSize);
ShowMessage(buffer);
end;
Using an array of char the memory will be freed on termination
of the application.
Or Using Long Strings Like this:
procedure TForm1.Button2Click(Sender: TObject);
var
buffer : string;
buffSize : DWORD;
begin
buffSize:=128;
SetLength(buffer,BuffSize);
GetUserName(PChar(buffer), buffSize);
ShowMessage(buffer);
end;
Using Long Strings, the memory will be freed when the String
variable falls out of scope.