Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a portable way in standard C++ to retrieve hostname?

Tags:

c++

c

linux

windows

I'm working on a C++ program that needs to use the hostname of the computer it is running on. My current method of retrieving this is by mangling a C API like this:

char *host = new char[1024];
gethostname(host,1024);
auto hostname = std::string(host);
delete host;

Is there a portable modern C++ method for doing this, without including a large external library (e.g., boost)?

like image 682
FrigidDev Avatar asked Nov 18 '25 19:11

FrigidDev


1 Answers

No, there is no standard C++ support for this. You'll either have to make your own function, or get a library that has this functionality.

like image 67
NathanOliver Avatar answered Nov 21 '25 08:11

NathanOliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!