[Lazarus] Finding out the Hostname of the Computer.

Andrew Haines andrewd207 at aol.com
Thu Nov 27 20:49:52 CET 2008


Dave Coventry wrote:
> Hi,
>
> I'm trying to get the name of the computer my app is running on.
>
> I've tried this:
>
>
> function GetThisComputerName: string;
> var
>   c: array[0..127] of Char;
>   computer: string;
>   sz: dword;
>   AProcess: TProcess;
>   AStringList: TStringList;
>
> begin
> {$IFDEF Win32}
>   sz := SizeOf(c);
>   GetComputerName(c, sz);
>   Result := c;
> {$ELSE}
>   AProcess := TProcess.Create(nil);
>   AStringList := TStringList.Create;
>   AProcess.CommandLine := 'echo $HOSTNAME';
>   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
>   AProcess.Execute;
>   AStringList.LoadFromStream(AProcess.Output);
>   Result:=AStringList.Strings[0];
>   AStringList.Free;
>   AProcess.Free;
> {$ENDIF}
>
> end;
>
> I'm not sure if it works under Windows, but running Ubuntu, it returns
> '$HOSTNAME'.
>   
Result := GetEnvironmentVariable('HOSTNAME'); This won't work under 
windows and there is probably a better way to do this with sockets.

Andrew



More information about the Lazarus mailing list