如何用Delphi编程序取得Windows2000登录用户的用户名?

解决方案 »

  1.   

    试试以下函数:
    function GetCurrentUserName : string; 
    const 
      cnMaxUserNameLen = 254; 
    var 
      sUserName : string; dwUserNameLen : DWord; 
    begin 
      dwUserNameLen := cnMaxUserNameLen-1; 
      SetLength( sUserName, cnMaxUserNameLen ); 
      GetUserName( PChar( sUserName ), dwUserNameLen ); SetLength( sUserName,   dwUserNameLen ); 
      Result := sUserName; 
    end;或:
    //================================================================
      // Function: GetMachine()
      // 获取本机器的名称
      // 参数:无
      // 返回:string
      // 完成度:95%
      //================================================================  function GetMachine: string;
      var
        n: dword;
        buf: pchar;
      const
        rkMachine = {HKEY_LOCAL_MACHINE}
        '\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName';
        rvMachine = 'ComputerName';
      begin
        n := 255;
        buf := stralloc(n);
        GetComputerName(buf, n);
        result := buf;
        strdispose(buf);
        with TRegistry.Create do
        begin
          rootkey := HKEY_LOCAL_MACHINE;
          if OpenKeyReadOnly(rkMachine) then
          begin
            if ValueExists(rvMachine) then
              result := ReadString(rvMachine);
            closekey;
          end;
          free;
        end;
      end;  //================================================================
      // Function: GetUser()
      // 获取系统中,当前用户的用户名
      // 参数:无
      // 返回:string
      // 完成度:95%
      //================================================================  function GetUser: string;
      var
        n: dword;
        buf: pchar;
      begin
        n := 255;
        buf := stralloc(n);
        GetUserName(buf, n);
        result := buf;
        strdispose(buf);
      end;