随笔 - 809  文章 - 0 评论 - 144 阅读 - 770万

 

复制代码
几个在Delphi 中取本机的计算机名、IP地址、Windows登录的用户名的函数.

uses Windows, WinSock;

{ ComputerLocalIP }

//取本机的 IP 地址

function ComputerLocalIP: string;
var
  ch: array[1..32] of char;
  wsData: TWSAData;
  myHost: PHostEnt;
  i: integer;
begin
  Result := '';
  if WSAstartup(2,wsData)<>0 then Exit; // can’t start winsock
  try
    if GetHostName(@ch[1],32)<>0 then Exit; // getHostName failed
  except
    Exit;
  end;
  myHost := GetHostByName(@ch[1]); // GetHostName error
  if myHost=nil then exit;
  for i:=1 to 4 do
  begin
    Result := Result + IntToStr(Ord(myHost.h_addr^[i-1]));
    if i<4 then
      Result := Result + '.';
  end;
end;

//取本机的计算机名

{ ComputerName }

function ComputerName: string;
var
  FStr: PChar;
  FSize: Cardinal;
begin
  FSize := 255;
  GetMem(FStr, FSize);
  Windows.GetComputerName(FStr, FSize);
  Result := FStr;
  FreeMem(FStr);
end;

//取Windows登录用户名

{ WinUserName }

function WinUserName: string;
var
  FStr: PChar;
  FSize: Cardinal;
begin
  FSize := 255;
  GetMem(FStr, FSize);
  GetUserName(FStr, FSize);
  Result := FStr;
  FreeMem(FStr);
end;
复制代码

1.1:Delphi获取计算机名

复制代码
procedure TMainForm.tmrMonitorTimer(Sender: TObject);
var
  ComputerName:array[0..MAX_COMPUTERNAME_LENGTH+1] of char; //保留计算机名的缓冲区
  Buffer:Dword; // 缓冲区大小
  sComputerName:string;//计算机名
begin
  Buffer:=MAX_COMPUTERNAME_LENGTH+1;
  if GetComputerName(@ComputerName,Buffer)   then
    begin
      sComputerName:=ComputerName;
    end
  else
    begin
      sComputerName:='';
    end;
  ShowMessage(sComputerName);
 
end;
复制代码


1.2: 取得计算机名(delphi )

复制代码
//------非常简单------------------------
procedure TForm2.Button2Click(Sender: TObject);
var
  s: Cardinal;
  p: PChar;
begin
  s := MAX_COMPUTERNAME_LENGTH + 1;
  GetMem(p, s);
  if GetComputerName(p,s) then
  begin
    caption := StrPas(p);
  end
  else ShowMessage('取不到计算机名');

  FreeMem(p);  //记得一定要释放
end;
复制代码

 

 

复制代码
关于取本地计算机的IP地址及计算机名的看法 
大体步骤:
需要用到一个组件,TCPClient 或直接引用:Web.Win.Sockets
1:先创建一个窗体。 2:将一个label1,一个按扭 bitbtn1,bittn2 放入窗体。 3:对BITBTN1 的ONCLICK 事件编程。 编程如下: ....... var tp:ttcpclient; strname,straddr:string; begin tp:=ttcpclient.create(self); tp.close; tp.open; strname:=tp.LocalHostName; straddr:=tp.LocalHostAddr; label1.caption:=strname+' IP: '+straddr; tp.close; end; bitbtn2 的onclick 事件如下: ..... begin close; end; 如上程序,运行后,单击bitbtn1 按扭, label1 将显示出本地计算机的 IP地址 和计算机名。(完毕)
复制代码

----

我修改了下,以后就用第二种方法了,真是方便:

复制代码
var
  MyTcpClient: TTcpClient;
  LocalHostName,LocalHostAddr:string;
begin
   MyTcpClient := TTcpClient.create(self);
   try
     LocalHostName := string(MyTcpClient.LocalHostName);
     LocalHostAddr := string(MyTcpClient.LocalHostAddr);
   finally
     MyTcpClient.Free;
   end;
end;
复制代码

 

2012.07.24补充---------我自己用的:UFastWindowsApi单元:

复制代码
function TFastWindowsApi.GetComputerNameIpListStr: string;
var
  MyTcpClient: TTcpClient;
  LocalHostName,LocalHostAddr:string;
begin
  MyTcpClient := TTcpClient.create(nil);
  try
    LocalHostName := string(MyTcpClient.LocalHostName);
    LocalHostAddr := string(MyTcpClient.LocalHostAddr);
    //返回
    Exit(LocalHostName+','+LocalHostAddr);
  finally
    MyTcpClient.Free;
  end;
end;
复制代码

 

 

posted on   del88  阅读(18)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示