首页 > 代码库 > 获取本机当前用户名

获取本机当前用户名

// 
function GetUser: string; //获取本机当前用户名
var
 Buffer: array[0..255] of Char;
 Size: cardinal;
begin
 Size := 256;
 if GetUserName(Buffer, Size) then
  begin
    Result := Buffer
  end
 else
   begin
    Result := ‘Unknown‘
   end;
end;

function GetComputerName: string;//获取计算机名
var
  buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
  Size: Cardinal;
begin
  Size := MAX_COMPUTERNAME_LENGTH + 1;
  Windows.GetComputerName(@buffer, Size);
  Result := StrPas(buffer);
end;
//显示信息
procedure TForm1.FormCreate(Sender: TObject);
begin
   Edit1.Text := GetUser;
   Edit2.Text := GetComputerName;
end;


来自为知笔记(Wiz)