Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-11

Finding out the Hostname of the Computer.

13 messages · 10 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

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'.

Can anyone see what I'm doing wrong, or offer an alternative way of getting the name of the computer?

Reply by email

Vincent Snijders
to Dave Coventry
Dave Coventry wrote 13 lines
Hi, I'm trying to get the name of the computer my app is running on. I've tried this: I'm not sure if it works under Windows, but running Ubuntu, it returns '$HOSTNAME'. Can anyone see what I'm doing wrong, or offer an alternative way of getting the name of the computer?

$HOSTNAME is replace by the shell, so it only happens if let the shell execute the command.

In this case GetEnvironmentVariable('HostName') (from the sysutils unit would be a lot easier.

Vincent

Reply by email

Andrew Haines
to Dave Coventry: I'm trying to get the name of the computer my app is running on. I've tried this:…
Dave Coventry wrote 37 lines
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

Reply by email

Graeme Geldenhuys
to Dave Coventry: I'm trying to get the name of the computer my app is running on. I've tried this:…
Dave Coventry wrote
I'm trying to get the name of the computer my app is running on. I've tried this:

We use the following in tiOPF for Delphi and FPC.

------------------------------------------------------

function tiGetComputerName : string;
begin
  {$IFDEF MSWINDOWS}
  Result := tiWin32GetComputerName;
  {$ENDIF MSWINDOWS}
  {$IFDEF UNIX}
  Result := tiUnixGetComputerName;
  {$ENDIF UNIX}
end;

---------------------------------------------
uses

  unix;
function tiUnixGetComputerName: string;
begin
  Result := GetHostName;
end;

--------------------------------------------
uses

    Windows;
function tiWin32GetComputerName: string;
var
  computerNameBuffer: array[0..255] of char;
  sizeBuffer: DWord;
begin
  SizeBuffer := 256;
  getComputerName(computerNameBuffer, sizeBuffer);
  result := string(computerNameBuffer);
end;

------------------------------------------------------

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Mark Morgan Lloyd
to Andrew Haines: Result := GetEnvironmentVariable('HOSTNAME'); This won't work under windows and there is…
Andrew Haines wrote
Result := GetEnvironmentVariable('HOSTNAME'); This won't work under windows and there is probably a better way to do this with sockets.

GetHostName() or similar. If you use a shell variable it can be spoofed, so particularly if running as root go to the lowest-level API available.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Malcolm Poole
to Aleš Katona: Ok well first of all, you're scratching yourself on the head with your left foot. If you…
Ale? Katona wrote
If you want the value of $HOSTNAME, use unit SysUtils and GetEnvironmentVariable('HOSTNAME');

Hmm. This was the first thing that I tried. For some reason I get an empty string on my system (Ubuntu Gutsy). That's why I dug around and found the GetHostName function.

Any idea why GetEnvironmentVariable('HOSTNAME') doesn't work for me? 'echo $HOSTNAME' on the commandline works as expected.

Malcolm

Reply by email

Mark Morgan Lloyd
to Malcolm Poole
Malcolm Poole wrote 9 lines
Ale? Katona wrote: > If you want the value of $HOSTNAME, use unit SysUtils and GetEnvironmentVariable('HOSTNAME'); > Hmm. This was the first thing that I tried. For some reason I get an empty string on my system (Ubuntu Gutsy). That's why I dug around and found the GetHostName function. Any idea why GetEnvironmentVariable('HOSTNAME') doesn't work for me? 'echo $HOSTNAME' on the commandline works as expected.

Check it's been exported, i.e. processes forked by the shell simply might not be getting a copy.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email