如何获得本机的计算机名?(50分)

  • 主题发起人 主题发起人 FinalLeon
  • 开始时间 开始时间
F

FinalLeon

Unregistered / Unconfirmed
GUEST, unregistred user!
如何获得本机的计算机名?
 
function TForm1.Computername: string;
var temp:pchar;
size:DWord;
begin
getmem(temp,255);
size:=255;
if GetComputerName(temp,size)=false then
begin
freemem(temp);
exit;
end;
result:=temp;
freemem(temp);
end;
 
可以调用kernel32库里的GetComputerName函数
原型为GetComputerName(ByVal lpBuffer As String, nSize As Long) As Long
 
var
lpName: PAnsiChar;
lpUserName: PAnsiChar;
lpnLength: DWORD;
begin
Result:='';
lpnLength := 0;
WNetGetUser(nil, nil, lpnLength);
// 取得字串长度
if lpnLength > 0 then
begin
GetMem(lpUserName, lpnLength);
if WNetGetUser(lpName, lpUserName, lpnLength) = NO_ERROR then
Result := lpUserName;
FreeMem(lpUserName, lpnLength);
end;
label1.caption:=result;
end;
 
用winsock中的gethostname()很容易的呀
 
var
s:Array[1..127] of char;
i:DWord;
begin
GetComputerName(@s,i);
Edit1.text:=s;
end;
 
char Buffer[25];
DWORD nSize = 25;
GetUserName(Buffer,&nSize);
return (String)Buffer;
 
GETHOSTNAMEBYADDR
 
强烈建议此类问题不要再问,多找一下就有答案了
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2163788
 
后退
顶部