Delphi get local computer name and IP address

Source: Internet
Author: User
Tags mongodb postgresql redis response code firewall


This procedure describes how to obtain the name and IP address of the local computer.



Add two Tlabel components, two tedit components, and a TButton component to the form.








First add a reference to Winsock in the uses section of your program, and then add a gethostname custom function that calls the GetComputerName function to get the computer name, and the response code is as follows:




function TfrmMain.GetHostName:String;
  var
   ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of char;
   Size: Cardinal;
  begin
   result:='';
   Size := MAX_COMPUTERNAME_LENGTH+1;
   GetComputerName(ComputerName, Size);
   Result:=StrPas(ComputerName);
end;




Add a Nametoip custom function in which the gethostbyname function is invoked to obtain the IP address of the computer based on the computer name, and the response code is as follows:




function TfrmMain.NameToIP(Name:string):String;
  var
   WSAData: TWSAData;
   HostEnt: PHostEnt;
  begin
   result:='';
   WSAStartup(2, WSAData);
   HostEnt := GetHostByName(PChar(Name));
   if HostEnt <> nil then
   begin
    with HostEnt^ do
     result:= Format('%d.%d.%d.%d',[Byte(h_addr^[0]), Byte(h_addr^[1]),Byte(h_addr^[2]), Byte(h_addr^[3])]);
   end;
   WSACleanup;
  end;






Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.