|
|
|
GetUserName
This code demonstrates how to obtain the name of the user currently
logged on to this machine. Again, this code really doesn't depict an
advanced topic, rather it represents a useful code sample that is
regularly used across separate projects.
const
sNoUserNameError = 'There was an error
determining the identity of
'the current user.'
function GetUserName: string;
var lpBuffer: array of char;
dwBufferSize: DWORD;
begin
dwBufferSize := 0;
// When called with null parameters,
GetUserName returns the
// actual size required for the outgoing buffer, including
// the terminating null character.
Windows.GetUserName(@lpBuffer, dwBufferSize);
if dwBufferSize = 0 then
raise Exception.Create(sNoUserNameError);
SetLength(lpBuffer, dwBufferSize);
Win32Check(Windows.GetUserName(@lpBuffer[0],
dwBufferSize));
Result := pChar(@lpBuffer[0]);
end;
|