Pegar Usuário Logado na Máquina

Delphi

19/03/2003

Gostaria de saber como pegar o nome do usuário logado no Windows.


Anonymous

Anonymous

Curtidas 0

Respostas

Espiridiao

Espiridiao

19/03/2003

Ai vai:

[color=blue:f891e6588d]
function GetCurrentUserName: string;
var
Len: cardinal;
{ This will have to be Integer, not cardinal, in Delphi 3. }
begin
Len := 255;
{ arbitrary length to allocate for username string, plus one for null terminator }
SetLength(Result, Len - 1);
{ set the length }
GetUserName(PChar(Result), Len);
{ get the username }
SetLength(Result, Len - 1);
{ set the exact length if it succeeded }
end;[/color:f891e6588d]


GOSTEI 0
Dor_poa

Dor_poa

19/03/2003

function TForm1.usuario : string;
var
szNetName: Array[0..48] of Char;
iResult: DBIResult;
begin
iResult:= DBIGetNetUserName(szNetName);
if iResult <> DBIErr_None then
DBIError( iResult )
else
Result:= StrPas(szNetName);
end;


Colocar na chamada de Uses de sua Unit as seguintes DCUs : DBITYPES, DBIPROCS, DBIERRS, DBTables e DB.


GOSTEI 0
Dor_poa

Dor_poa

19/03/2003

function LogUser : String;
{Requer a unit Registry declarada na clausula Uses da Unit}
var
Registro:TRegistry;
begin
Registro := TRegistry.Create;
Registro.RootKey := HKEY_LOCAL_MACHINE;
if Registro.OpenKey(´Network\Logon´, false) then
begin
result := Registro.ReadString(´username´);
end;
Registro.Free;
end;


GOSTEI 0
POSTAR