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

wuyunliangcsdn 2003-01-24 10:09:30
如何用Delphi编程序取得Windows2000登录用户的用户名?
...全文
115 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyunliangcsdn 2003-01-24
  • 打赏
  • 举报
回复
非常感谢,我一定会给你加分(看一下其他朋友有什么高招)。
Billy_Chen28 2003-01-24
  • 打赏
  • 举报
回复
试试以下函数:
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;

2,507

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧