Recent

Author Topic: How to get the Computer Name of the computer i'm using?  (Read 14364 times)

lestroso

  • Full Member
  • ***
  • Posts: 134
    • FaSasoftware
How to get the Computer Name of the computer i'm using?
« on: December 27, 2015, 11:45:16 am »
Dear friends,

i have tryed this code below without success ....I see with this code a blank message window...I need to retrieve The Computer Name for every platform...Can somebody help me please??

Thank a lot!!

Lestroso :-[


Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,unix;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.  
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.  ShowMessage(GetEnvironmentVariable('COMPUTERNAME')) ;  <--------THIS DON'T WORK!!!
  37. end;
  38. end.
  39.    
  40.  
  41.  
  42.  
« Last Edit: December 27, 2015, 11:46:52 am by lestroso »

christensen

  • Full Member
  • ***
  • Posts: 127
Re: How to get the Computer Name of the computer i'm using?
« Reply #1 on: December 27, 2015, 12:09:06 pm »
What error you get?

for me it is working fine!

Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: How to get the Computer Name of the computer i'm using?
« Reply #2 on: December 27, 2015, 02:49:48 pm »
hello,
for windows and linux you can try the command hostname.
Example :
Code: Pascal  [Select][+][-]
  1. uses  Classes, SysUtils, process;
  2. ...
  3. var
  4.   AProcess     : TProcess;
  5.   OutputLines : TStringList;
  6. begin
  7. OutputLines := TStringList.Create();
  8. AProcess := TProcess.Create(nil);
  9. AProcess.Executable := 'hostname';
  10. AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  11. AProcess.Execute;
  12. OutputLines.LoadFromStream(AProcess.Output);
  13. // display computername
  14. writeln(OutputLines[0]);
  15. AProcess.Free;
  16. OutputLines.Free;        
  17. end;            
  18.  

Edit : I have replaced OutputLines.Text by OutputLines[0] (first line) because you can have more than one line in the output (carriage return for example).
Friendly, J.P
« Last Edit: December 29, 2015, 01:25:31 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

lestroso

  • Full Member
  • ***
  • Posts: 134
    • FaSasoftware
Re: How to get the Computer Name of the computer i'm using?
« Reply #3 on: December 28, 2015, 01:59:25 pm »
Dear Jurassic Pork,

your software is too complicated for me  and don't work.....i need for mac os x and windows .....is it possible to create a software example more simple and working??

I thank you again...

Happy new year!!!

Lestroso :-[

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 268
Re: How to get the Computer Name of the computer i'm using?
« Reply #4 on: December 28, 2015, 03:07:43 pm »
Dear Jurassic Pork,

your software is too complicated for me
Actualy it's quite simple, I believe you are just too much under stress. This line :
Code: Pascal  [Select][+][-]
  1. writeln(OutputLines.Text);
Can be used as :
Code: Pascal  [Select][+][-]
  1. ordinary_string_variable:=OutputLines.Text;
And your code also works for me, I develop under windows7 using Laz 1.4.2. I just have a sysutils prefix :
Code: Pascal  [Select][+][-]
  1.   showmessage(SysUtils.GetEnvironmentVariable('COMPUTERNAME'));    

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: How to get the Computer Name of the computer i'm using?
« Reply #5 on: December 28, 2015, 04:44:28 pm »
Code: Pascal  [Select][+][-]
  1. program hostname;
  2.  
  3. uses
  4.     {$IFDEF UNIX} unix,{$ENDIF}
  5.     {$IFDEF WINDOWS} windows,{$ENDIF}
  6.     sysutils;
  7.  
  8. function GetMySweetComputerName: string;
  9. {$IfDef WINDOWS}
  10. var
  11.    l: DWORD;
  12. {$EndIf}
  13. begin
  14.     {$IfDef LINUX}
  15.     Result := GetHostName;
  16.     {$EndIf}
  17.     {$IfDef WINDOWS}
  18.     l := 255;
  19.     SetLength(Result, l);
  20.     GetComputerName(PChar(Result), l);
  21.     SetLength(Result, l);
  22.     {$EndIf}
  23.     {$IfDef appleOS}
  24.     {$EndIf}
  25. end;
  26.  
  27. begin
  28.     try
  29.         Writeln(GetMySweetComputerName);
  30.     except
  31.         on e: Exception do
  32.             Writeln(e.Message);
  33.     end;
  34.     Readln;
  35. end.
  36.  

and output:
Code: Text  [Select][+][-]
  1. ~/Projects/tests $ ./hostname
  2. home
  3.  
  4. ~/Projects/tests $ wine hostname.exe
  5. home
  6.  
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: How to get the Computer Name of the computer i'm using?
« Reply #6 on: December 28, 2015, 05:35:50 pm »
Code: Pascal  [Select][+][-]
  1.  ShowMessage(GetEnvironmentVariable('COMPUTERNAME')) ;  <--------THIS DON'T WORK!!!
  2.  

For some magical reasons there is no 'COMPUTERNAME' or 'HOSTNAME' or something in the at least Ubuntu-based OSes:
Code: Bash  [Select][+][-]
  1. nd@nd-home ~ $ echo $COMPUTERNAME
  2.  
  3. nd@nd-home ~ $ echo $HOSTNAME
  4. nd-home
  5. #But! There is no environment variable 'HOSTNAME':
  6. nd@nd-home ~ $ printenv | grep NAME
  7. USERNAME=nd
  8. LOGNAME=nd
  9. LC_NAME=uk_UA.UTF-8
  10.  

So for linux-based systems try unix.GetHostName function.

Have fun and HNY *~<;o)
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: How to get the Computer Name of the computer i'm using?
« Reply #7 on: December 28, 2015, 05:49:22 pm »
Quote
For some magical reasons there is no 'COMPUTERNAME' or 'HOSTNAME' or something in the at least Ubuntu-based OSes:

Not magical  :-*
The posix standard enumerates the environment variables you should expect on a posix-compliant systems, and HOSTNAME is not in the list: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: How to get the Computer Name of the computer i'm using?
« Reply #8 on: December 28, 2015, 06:03:34 pm »
Not magical  :-*

With such avatar the kissing smile looks extremely :D

Honestly I was confused about that

Code: Bash  [Select][+][-]
  1. echo $PATH
  2. echo $HOSTNAME
works in same way, but
Code: Pascal  [Select][+][-]
  1. writeln(GetEnvironmentVariable('PATH'));
  2. writeln(GetEnvironmentVariable('HOSTNAME'));

returns different results.
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: How to get the Computer Name of the computer i'm using?
« Reply #9 on: December 28, 2015, 06:15:00 pm »
HOSTNAME is a Bash variable that's set automatically when you are in a bash session. It isn't an "environment variable" (type in a bash the command env  or printenv to see all the environment variables).  ;)
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12706
  • FPC developer.
Re: How to get the Computer Name of the computer i'm using?
« Reply #10 on: December 28, 2015, 06:16:05 pm »
Unix: baseunix.fpuname to get the hostname

On Windows, the netbios name is more commonly used, and according to msdn there is a function for that:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724301%28v=vs.85%29.aspx

allows to query several different hostname types. (dns, netbios, FQDN etc)

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: How to get the Computer Name of the computer i'm using?
« Reply #11 on: December 28, 2015, 06:30:23 pm »
Ok. In any case it is strange at least for me that the RTL have not such cross-platform function (or I miss something). I understand that for Android or iOS the `hostname` is something strange, but for desktop OSes it is must. IMO surely.

PS: I am not contributor of FPC and moreover I am too stupid for now to find where to register to provide FPC tickets  :-[ Maybe new year - new possibilities.
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

jwdietrich

  • Hero Member
  • *****
  • Posts: 1274
    • formatio reticularis
Re: How to get the Computer Name of the computer i'm using?
« Reply #12 on: December 29, 2015, 01:16:48 am »
@lestroso, you need a version for Mac OS X and Windows?

Both suggestions by @Jurassic Pork and @Abelisto work well on the Mac. In @Abelisto's code you should slightly modify the function GetMySweetComputerName to get the following form:

Code: Pascal  [Select][+][-]
  1. function GetMySweetComputerName: string;
  2. {$IfDef WINDOWS}
  3. var
  4.    l: DWORD;
  5. {$EndIf}
  6. begin
  7.     {$IfDef UNIX}
  8.     Result := unix.GetHostName;
  9.     {$EndIf}
  10.     {$IfDef WINDOWS}
  11.     l := 255;
  12.     SetLength(Result, l);
  13.     GetComputerName(PChar(Result), l);
  14.     SetLength(Result, l);
  15.     {$EndIf}
  16. end;
  17.  

and you should add

Code: Pascal  [Select][+][-]
  1. {$IFDEF UNIX}
  2.   , Unix
  3. {$ENDIF}
  4.  

to the uses clause in the beginning of your unit.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

lestroso

  • Full Member
  • ***
  • Posts: 134
    • FaSasoftware
Re: How to get the Computer Name of the computer i'm using?
« Reply #13 on: December 31, 2015, 08:22:19 am »
Dear friends,

I tryed your software without success....but jurassicpork help  me a lot....because he make me understand that the command "printenv" in macosx in the terminal , i can see few  Variables that i can use.....

Below i post my software working for Macosx.....but i can use only the command "User", because in the "printenv" there's no commands to retrieve the machine that i'm using.....

Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,unix;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.  
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.  ShowMessage(GetEnvironmentVariable('USER')) ;
  37. end;
  38. end.
  39.  
  40.  

Thanks a lot to Everybody, Lestroso :D
« Last Edit: December 31, 2015, 08:25:56 am by lestroso »

jwdietrich

  • Hero Member
  • *****
  • Posts: 1274
    • formatio reticularis
Re: How to get the Computer Name of the computer i'm using?
« Reply #14 on: December 31, 2015, 03:01:08 pm »
I don't understand, why it doesn't work for you. I have tested both algorithms on different Macs with four different versions of Mac OS X (10.5.8 Leopard, 10.6.8 Snow Leopard, 10.9.5 Mavericks and 10.11.2 El Capitan) and different processor architectures (PPC and Intel), and with different versions of FPC (2.6.4 and 3.0.0) and Lazarus (1.4.0, 1.4.4 and 1.6.0RC1), and they work in every configuration.

Please find a test project with both algorithms (you can switch between them with the compiler switch JurassicAlgorithm) in the attachment. Does it work on your machine?
« Last Edit: January 01, 2016, 04:28:57 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018