luac
luac

Reputation: 1

C++ Lookup hostname for IP without #include <winsocket2.h>

How can i retrieve the IP related to a hostname without #include <winsocket2.h>? I can't use the <winsocket2.h> methods because my DLL crashs without any compile errors, the following code doesn't work ...

hostent *h = gethostbyname(hostName);
      LogAlways((const char*)inet_ntoa(*(reinterpret_cast<in_addr*>(h->h_addr))));

Upvotes: 0

Views: 782

Answers (1)

Mike Dinescu
Mike Dinescu

Reputation: 55720

Without an include (or a dependency on an external library) there's almost no way.

In order to get the hostname that maps to an IP you need to do a RARP request which pretty much requires you to be able to use sockets. So one way or another you need to be able to create a socket. Which is what the winsock2.lib provides as a library.

So, you're better off getting winsock2.lib to work. When you say your DLL crashes without any compile errors I'm assuming you are talking about your own code which you're trying to compile into a DLL.

What about debugging? Have you tried stepping through the code to see where it fails? What is the type of hostName? Check to make sure you're not trying to access members on a null pointer. And that you've allocated memory correctly before you're using the variables. Otherwise, let us know more specifically what error message you get in the debugger.

Upvotes: 1

Related Questions