Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-29 Thread Mark Morgan Lloyd
Malcolm Poole wrote:
 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]
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-28 Thread Graeme Geldenhuys
On Thu, Nov 27, 2008 at 3:28 PM, Dave Coventry [EMAIL PROTECTED] 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/
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-28 Thread Malcolm Poole
Dave Coventry wrote:
 Can anyone see what I'm doing wrong, or offer an alternative way of
 getting the name of the computer?
uses unix;

GetHostName :-)

Malcolm
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-28 Thread Dave Coventry
Thanks guys.

I knew there must be an easier way of doing it!
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-28 Thread Mark Morgan Lloyd
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]
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-28 Thread Malcolm Poole
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
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


[Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread 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?
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread Vincent Snijders
Dave Coventry schreef:
 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
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread Aleš Katona
Ok well first of all, you're scratching yourself on the head with your left 
foot.

If you want the value of $HOSTNAME, use unit SysUtils and 
GetEnvironmentVariable('HOSTNAME');

Ales
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread Henry Vermaak
2008/11/27 Dave Coventry [EMAIL PROTECTED]:
 Hi,

 I'm trying to get the name of the computer my app is running on.

try: http://www.freepascal.org/docs-html/rtl/unix/gethostname.html

henry
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread Sergei Gorelkin
Dave Coventry wrote:

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

A simple

result := GetEnvironmentVariable('HOSTNAME');

should do it.
Alternatively, you may read the /etc/hostname file just as you read any 
other text file.

Regards,
Sergei

___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread Andrew Haines
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
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Finding out the Hostname of the Computer.

2008-11-27 Thread Micha Nelissen
Dave Coventry wrote:
 {$ELSE}
   AProcess := TProcess.Create(nil);
   AStringList := TStringList.Create;
   AProcess.CommandLine := 'echo $HOSTNAME';

Try running 'hostname' instead?

Micha
___
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus