0

Any guesses as to why this simple test code would show WinHttpConnect fails with error code 6 (invalid handle or ERROR_INVALID_HANDLE):

   HINTERNET internet = WinHttpOpen(L"test",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
   assert(internet == (HINTERNET)1);
   HINTERNET connect = WinHttpConnect(internet,L"www.microsoft.com",INTERNET_DEFAULT_HTTP_PORT,0);
   assert(!connect);
   DWORD err = GetLastError();
   assert(err == 6);

All of the above asserts pass. I've run this code on my Win8 PC and also on a Win2003 server. Two different PCs, same issue. My original code was more complex and runs as a service, but I reduced down to this code and just ran it in a simple test app in user mode (not service).

My app is compiled with an older compiler, Borland Builder 6, but not sure that should be a problem.

eselk
  • 6,764
  • 7
  • 60
  • 93
  • `WinHttpOpen()` is not guaranteed to return 1 on success. You should be asserting on `!= 0` instead, like you do with `WinHttpConnect()`. Also, `GetLastError()` is only meaningful when either function returns 0, so don't call it if they don't actually fail. – Remy Lebeau Aug 12 '14 at 21:30
  • True, but I wanted the question to be as detailed as possible, to show it was always returning 1 in my case. Seemed a bit odd, and maybe a clue to the problem, since most handles are larger values. – eselk Aug 24 '14 at 16:51

1 Answers1

0

I had already typed up my question and then figured this out just before I posted it. Instead of deleting or not posting my question, I figured I should share this because I know others are still stuck using this really old compiler also :(

C++ Borland Builder 6 does not include a WinHTTP.LIB file, so I used implib to create one, the same way I always do. Well, in this case it appears you need to use the -f flag to force it to import by name instead of ordinal, otherwise you get strange results.

implib -f winhttp.lib winhttp.dll

This worked for me anyway, and now my above code works on Win2003 server and also Win8 PC.

Sorry, I'm not sure where I got the WinHTTP.h file, probably from a newer version of a Borland/Inspire compiler, since the Microsoft SDK ones usually don't work.

eselk
  • 6,764
  • 7
  • 60
  • 93
  • This would imply that the exported ordinal of `WinHttpConnect()` has changed between Windows versions (not unheard of over the years), and why importing it by name solves it. You should always import by name when possible. – Remy Lebeau Aug 12 '14 at 21:27