cURL / Mailing Lists / curl-library / Single Mail


Re: Windows people -- help needed!

From: <>
Date: Tue, 28 Jan 2003 13:51:57 -0500

Here's my test of #2 with some modificatiions to get it to compile. I
added a few comments along the way. Seems to work ok for me. You would
still need to address the TODO in reagrds to the returned hostent, probably
what curl already has to clone these structs.

#include <windows.h>
#include <stdio.h>

// a structure passed to gethostbyname_tp containing
// name resolution info
typedef struct dns_info
      char *name;
      struct hostent *dnsinfo;
      HANDLE hEventDns;
} dns_info;

// The thread proc that will run the gethostname resolutions
DWORD WINAPI gethostbyname_tp(LPVOID* p)
      dns_info* info = (dns_info*)p;
      info->dnsinfo = gethostbyname(info->name);
      return 0;

// Get hostbyname with a timeout
// returns < 0 if timeout occurred (or some other initialization issue)
// or zero if gethostbyname() completed (note: does not imply that name
// was resolved, only that gethostbyname() completed). Returns an
// hostent struct in pph (if applicable)
int gethostbyname_to(char *name, int timeout_millis, struct hostent** pph)
      int rc = 0;
      struct hostent* h = NULL;
      DWORD hThreadDns = 0;
      int eventresult = 0;

      dns_info myInfo; = strdup(name);
      myInfo.dnsinfo = NULL;

      // Create the event for the threadproc to signal upon completion
      myInfo.hEventDns = CreateEvent(NULL, FALSE, FALSE, "DNS Event");

      // start the name resolution thread
      // NOTE: if the threadproc ever includes and C runtime calls this
      // should be changes to using _beginthreadex()
      hThread = CreateThread(NULL, 0, gethostbyname_tp, &myInfo, 0,

      if(hThread != INVALID_HANDLE_VALUE)
            // wait for name resolution to either complete or timeout
            eventresult = WaitForSingleObject(myInfo.hEventDns,

            if ((eventresult==WAIT_TIMEOUT) || (eventresult==WAIT_FAILED))
                  // if timedout, kill the pending opertation
                  TerminateThread(hThread, -1);
                  rc = -1;
                  myInfo.dnsinfo = NULL;



      // TODO: need to copy the hostent* returned if not NULL
      // don't just return the gethostbyname allocated one
      *pph = myInfo.dnsinfo;

      return rc;

int main(int argc, char *argv[])
      int i = 0;
      WSADATA data;
      struct hostent* h = NULL;

      char* names[] = {

      int timeouts[] = {

      WSAStartup(0x0101, &data);

      for(i=0; i<4; i++)
            if(gethostbyname_to(names[i], timeouts[i], &h) < 0)
                  printf("gethostbyname timed out.\n");
                  printf("%s (%d)\n", h?"name resolved":"name not
resolved", WSAGetLastError());


      return 0;

                      Daniel Stenberg
                      <> To: libcurl Mailing list <>
                      Sent by: cc:
                      curl-library-admin_at_lists.sour Subject: Windows people -- help needed!
                      01/28/2003 11:22 AM
                      Please respond to


Bug report #607604 was fixed a long time ago for non-windows platforms, and
just want to ask if there's anyone out there who could possibly consider
helping us closing the report by fixing this in the windows version as


There is also this interesting patch in bug report #674095 submitted by
aschiffler, that offers a gethostbyname() that can be aborted with a
(for Windows). I would like someone to test it/comment it to become subject
for inclusion in an upcoming libcurl version:

 Daniel Stenberg -- curl, cURL, Curl, CURL. Groks URLs.
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
Received on 2003-01-28