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 ); ParameterslpBufferPoints to a buffer to receive the null-terminated character string containing the computer name. nSizePoints 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.
//获取计算机名称 function THouseSys.GetcomputerName:AnsiString; var lpBuffer: array[0..MAX_PATH] of char;dwSize: DWORD; begin dwSize:= MAX_PATH; if not Windows.GetComputerName(lpBuffer, dwSize) then raise Exception.Create(SysErrorMessage(GetLastError())); Result:= StrPas(lpBuffer); end;
procedure TForm1.Button1Click(Sender: TObject); var tmp:pchar; n:Cardinal; begin getmem(tmp, MAX_COMPUTERNAME_LENGTH + 1); getcomputername(tmp,n); showmessage(tmp); end;
LPDWORD nSize // address of size of name buffer
);
ParameterslpBufferPoints to a buffer to receive the null-terminated character string containing the computer name. nSizePoints 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.
function THouseSys.GetcomputerName:AnsiString;
var lpBuffer: array[0..MAX_PATH] of char;dwSize: DWORD;
begin
dwSize:= MAX_PATH;
if not Windows.GetComputerName(lpBuffer, dwSize) then
raise
Exception.Create(SysErrorMessage(GetLastError()));
Result:= StrPas(lpBuffer);
end;
var
tmp:pchar;
n:Cardinal;
begin
getmem(tmp, MAX_COMPUTERNAME_LENGTH + 1);
getcomputername(tmp,n);
showmessage(tmp);
end;
i:cardinal;
i:=100;
setlength(s,100);
getcomputername(pchar(s),i);
edit1.text:=s;
var
ComputerName:pchar;
Size: Cardinal;
Re:Boolean;
begin
{ CopyRight (C) Bowman 2000 }
{ [email protected] }
{Set Size}
Size := MAX_COMPUTERNAME_LENGTH+1;
Getmem(ComputerName,Size);
{retrieve computer name}
Re:=GetComputerName(ComputerName, Size);
if re then
Edit1.Text := StrPas(Computername)
else Showmessage('Computer Name Not Found');
Freemem(ComputerName);
end;
Buffer:PChar;
begin
GetMem(Buffer, MAX_COMPUTERNAME_LENGTH+1);
getcomputername(Buffer,MAX_COMPUTERNAME_LENGTH);
FreeMem(Buffer);
end;
buff:pchar;
begin
getmem(buff,255);
getcomputername(buff,255);
freemem(buff,255);
end;