Link to home
Start Free TrialLog in
Avatar of jatinps
jatinps

asked on

How to retrieve hostname where application is running using C programming language?

I want to retrieve the hostname where my application is running to put into log file.

How can I do it in 'C'? Any library/function ??
Avatar of dragonlord
dragonlord

you can use gethostname() under both windows and unix.

under windows:

gethostname()
Description
Return the standard host name for the local machine.

#include <winsock.h>

int PASCAL FAR gethostname ( char FAR * name, int namelen );

name
A pointer to a buffer that will receive the host name. namelen
The length of the buffer.
Remarks
This routine returns the name of the local host into the buffer specified by the name parameter. The host name is returned as a null-terminated string. The form of the host name is dependent on the Windows Sockets implementation--it may be a simple host name, or it may be a fully qualified domain name. However, it is guaranteed that the name returned will be successfully parsed by gethostbyname() and WSAAsyncGetHostByName().

Return ValueIf no error occurs, gethostname() reutrns 0, otherwise it returns SOCKET_ERROR and a specific error code may be retrieved by calling WSAGetLastError().

Error Codes

WSAEFAULT
The namelen parameter is too small WSAENOTINITIALISED
A successful WSAStartup() must occur before using this API. WSAENETDOWN
The Windows Sockets implementation has detected that the network subsystem has failed. WSAEINPROGRESS
A blocking Windows Sockets operation is in progress.


Under unix:

man 3 gethostname

Hope this helps.



delete.The.Duplicate
Shell to command prompt and place output of SET command into file named: Set.Log

This file can be input as text, and using pattern matching, values can be extruded, retrieved, and placed in variables according to format desired. Then output NewString to file named: HostName.Log

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial