关于获得机器名字的问题(100分)

  • 主题发起人 主题发起人 sanlanggugu
  • 开始时间 开始时间
S

sanlanggugu

Unregistered / Unconfirmed
GUEST, unregistred user!
老大们?获得本机名称的API函数是什么哦 ?????????????????
 
it is in Delphi help->windows sdk

procedure TForm1.Button1Click(Sender: Tobject);
var
ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of char;
Size: Integer;
begin
Size := MAX_COMPUTERNAME_LENGTH+1;

if GetComputerName(ComputerName, Size) then
Edit1.Text := StrPas(Computername)
else
Showmessage('Computer Name Not Found');
end;

 
The GetComputerName function retrieves the computer name of the current system. This name is established at system startup, when it is initialized from the registry.

BOOL GetComputerName(

LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);


Parameters

lpBuffer

Points to a buffer to receive the null-terminated character string containing the computer name.

nSize

Points to a variable that specifies the maximum size, in characters, of the buffer. This value should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.



Return Values

If the function succeeds, the return value is nonzero and the variable represented by the nSize parameter contains the number of characters copied to the destination buffer, not including the terminating null character.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

See Also

SetComputerName
 
procedure TfrmMain.Button1Click(Sender: TObject);
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
p2 : pchar;
OutPut:array[0..100] of char;
begin
{Start up WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
{Get the computer name}
GetHostName(@s, 128);
p := GetHostByName(@s);
{Get the IpAddress}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
StrPCopy(OutPut,'Hostname: '+Format('%s', [p^.h_Name])+#10#13+
'IPaddress: '+Format('%s',[p2])
);
WSACleanup;
MessageBox(0,OutPut,'NetInfo',mb_ok or mb_iconinformation);
end;

拷贝来的,还有IP,试试看。
 
用TIDIPWatch控件的LocalName属性直接就取出来了,很方便
 
接受答案了.
 
后退
顶部