本帖最后由 群发软件 于 2017-8-5 23:44 编辑
主机的名称。
向窗体上添加两个TLabel组件、两个TEdit组件和一个TButton组件,设计完成的主界面如图1所示。
图1 主界面
在程序运行过程中首先需要向程序的uses段中加入Winsock,这样就可以调用与Winsock相关的函数进行操作。
Attributes和FileName是两个全局变量。其中Word类型的Attributes变量用于存储文件的属性,而String类型的FileName属性用于存储文件的路径和文件名。
另外还要在FormCreate过程中添加下列代码:
procedure TfrmMain.btnConversionClick(Sender: TObject);
var
WSAData: TWSAData;
HostEnt: PHostEnt;
IPAddress: string;
addr: dword;
begin
edtName.Clear;
WSAStartup(2, WSAData);
IPAddress:=edtAddress.Text;
try
addr := inet_addr(PChar(IPAddress));
HostEnt:= GetHostByAddr(@addr, Length(IPAddress), PF_INET);
edtName.Text:=HostEnt.h_name;
except
ShowMessage('无效的IP地址');
end;
WSACleanup;
end;
程序运行初期就会执行FormCreate中的代码,首先通过OpenDialog.Execute语句来显示一个提示读者选择文件的对话框。如果用户在该对话框中确认选择了一个有效文件后,就把文件路径和文件名等信息存储在变量FileName中,把文件读写属性等信息存储在变量Attributes中。然后通过判断Attributes分别与faReadOnly、faArchive、faSysFile和faHidden常量比较的结果,来决定ReadOnly、Archive、System和Hidden4个CheckBox组件的选中状态。
通过lblPosition.Caption:=lblPosition.Caption+ExtractFileDir(FileName)语句在lblPosition组件中显示文件位置,通过lblName.Caption:=lblName.Caption+ExtractFileName(FileName)语句显示文件名。最后通过GetFileSize函数和lblSize.Caption:=lblSize.Caption+Format('%d bytes', [GetFileSize(FileName)])语句来显示文件大小,GetFileSize函数代码如下:
function TfrmMain.GetFileSize(const FileName: string): LongInt;
var
SearchRec: TSearchRec;
begin
try
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
Result := SearchRec.Size
else Result := -1;
finally
SysUtils.FindClose(SearchRec);
end;
end;
首先声明SearchRec为一个查找记录,然后通过FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec)语句来查找目标文件,找到后设置GetFileSize函数的返回值为目标文件的大小SearchRec.Size。
程序代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask,Winsock;
type
TfrmMain = class(TForm)
Label1: TLabel;
btnConversion: TButton;
edtName: TEdit;
Label2: TLabel;
edtAddress: TEdit;
procedure btnConversionClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure edtAddressChange(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.btnConversionClick(Sender: TObject);
var
WSAData: TWSAData;
HostEnt: PHostEnt;
IPAddress: string;
addr: dword;
begin
edtName.Clear;
WSAStartup(2, WSAData);
IPAddress:=edtAddress.Text;
try
addr := inet_addr(PChar(IPAddress));
HostEnt:= GetHostByAddr(@addr, Length(IPAddress), PF_INET);
edtName.Text:=HostEnt.h_name;
except
ShowMessage('无效的IP地址');
end;
WSACleanup;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
edtAddress.Clear;
edtName.Clear;
btnConversion.Enabled:=False;
end;
procedure TfrmMain.edtAddressChange(Sender: TObject);
begin
btnConversion.Enabled:=True;
if edtAddress.Text='' then
begin
btnConversion.Enabled:=False;
end;
end;
end.
- function GetComputerName:String; //获取计算机名称
- var
- wVersionRequested : WORD;
- wsaData : TWSAData;
- p : PHostEnt; s : array[0..128] of char;
- begin
- try
- wVersionRequested := MAKEWORD(1, 1); //创建 WinSock
- WSAStartup(wVersionRequested, wsaData); //创建 WinSock
- GetHostName(@s,128);
- p:=GetHostByName(@s);
- Result:=p^.h_Name;
- finally
- WSACleanup; //释放 WinSock
- end;
- end;
-
- function GetHostIP:String; //获取IP
- var
- wVersionRequested : WORD;
- wsaData : TWSAData;
- p : PHostEnt; s : array[0..128] of char; p2 : pchar;
- begin
- try
- wVersionRequested := MAKEWORD(1, 1); //创建 WinSock
- WSAStartup(wVersionRequested, wsaData); //创建 WinSock
- GetHostName(@s,128);
- p:=GetHostByName(@s);
- p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
- Result:= P2;
- finally
- WSACleanup; //释放 WinSock
- end;
- end;
[html] view plain copy
- 获取本机IP,可以获得内网IP或外网IP的函数,在Uses中加入WinSock:
-
- Function GetLocalIp(InternetIP:boolean):String;
- type
- TaPInAddr = Array[0..10] of PInAddr;
- PaPInAddr = ^TaPInAddr;
- var
- phe: PHostEnt;
- pptr: PaPInAddr;
- Buffer: Array[0..63] of Char;
- I: Integer;
- GInitData: TWSAData;
- IP: String;
- begin
- Screen.Cursor := crHourGlass;
- try
- WSAStartup($101, GInitData);
- IP:='0.0.0.0';
- GetHostName(Buffer, SizeOf(Buffer));
- phe := GetHostByName(buffer);
- if phe = nil then
- begin
- ShowMessage(IP);
- Result:=IP;
- Exit;
- end;
- pPtr := PaPInAddr(phe^.h_addr_list);
- if InternetIP then
- begin
- I := 0;
- while pPtr^[I] <> nil do
- begin
- IP := inet_ntoa(pptr^[I]^);
- Inc(I);
- end;
- end
- else
- IP := inet_ntoa(pptr^[0]^);
- WSACleanup;
- Result:=IP;//如果上网则为上网ip否则是网卡ip
- finally
- Screen.Cursor := crDefault;
- end;
- end;