Board index » delphi » Getting host name of local machine

Getting host name of local machine

Greetings in Christ our Saviour.

My machine's name is "meteora".  I connect to the internet through "optina".
How can I programatically get my machine's name -- i.e. "meteora" --
preferably a way that will work in both Delphi and Kylix, or a solution for
each?

I have tried almost every component in D6, and everything else that I have
been able to find on the web.  They all give me "optina".

Any help will be greatly appreciated.

In Christ,

Peter, hierodeacon

----
Dormition Skete
A Monastery of the Russian Orthodox Autonomous Church
http://www.BuenaVistaCO.com/GOC
http://www.ROACAmerica.org
----

 

Re:Getting host name of local machine


With Indy:

uses IdStack;

...
  GStack := TIdStack.CreateStack;
  try
    LocalHostName := GStack.WSGetHostName;
  finally
    FreeAndNil(GStack);
  end;
  LocalHostName := GStack.WSGetHostName;
...

Good luck,
Stephane

Re:Getting host name of local machine


Thank you, but this still gives me "optina", which is the computer I connect
to the internet through.  Did I do something wrong here?

I would greatly appreciate any help in getting my machine name
programatically.

uses IdStack

procedure TForm1.Button1Click(Sender: TObject);
var
  GStack : TIdStack;
  LocalHostName : string;
begin
  GStack := TIdStack.CreateStack;
  try
    LocalHostName := GStack.WSGetHostName;
  finally
    FreeAndNil(GStack);
  end;
  label1.caption := LocalHostName;
end;

In Christ,

Peter, hierodeacon

Re:Getting host name of local machine


Dormition Skete schrieb:

Quote
> My machine's name is "meteora".  I connect to the internet through "optina".
> How can I programatically get my machine's name -- i.e. "meteora" --
> preferably a way that will work in both Delphi and Kylix, or a solution for
> each?

For Windows, try the GetComputerName() API function. Don't know for
Linux.

-Michael

Re:Getting host name of local machine


Thank you very, very much!  This works.  I'm halfway there.  I really
appreciate it.

Re:Getting host name of local machine


You probably have something wrong with your config: that call to the stack
will return the mnain (first) host name of the local machine.

Good luck,
Stephane

Re:Getting host name of local machine


You are absolutely right.  In the TCP/IP Properties, DNS Configuration, I
had the Host set to "optina", thinking it needed to be this way to connect
to the internet through it.  I changed it to "meteora", and now the code you
gave me works, and the internet connection also still works.

Thank you very much.  I think this is a better way of going about it since
it should work on both platforms.

Again, thank you.

In Christ,

Peter, hierodeacon

Re:Getting host name of local machine


Just one thing: The code I gave you supposes you don't already creates an
instance of the stack automatically. To be sure, uses a different variable
than GStack (which is global).

Good luck,
Stephane

Other Threads