Click to See Complete Forum and Search --> : Visual C++ Network: How to get the local hostname?


Andreas Masur
February 27th, 2003, 06:18 PM
Q: How to get the local hostname?

A:


#include <winsock2.h>

// Add ws2_32.lib to your linker options

WSADATA WSAData;

// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData) == FALSE)
// Error handling

// Get local host name
char szHostName[128] = "";

if(::gethostname(szHostName, sizeof(szHostName) - 1))
// Error -> call 'WSAGetLastError()'

// Cleanup
WSACleanup();

<br>