ERROR_INTERNET_INCORRECT_HANDLE_TYPE with InternetQueryOption/InternetSetOption

What would cause a ERROR_INTERNET_INCORRECT_HANDLE_TYPE (12018) error when using the InternetQueryOption/InternetSetOption combination under WinInet?  The handle that I'm using comes straight from InternetOpen (see code) and can be used with InternetOpenURL to download a file from an HTTP server.  I need to set options to allow InternetOpenURL to download from a HTTPS server with a bad SSL certificate, and thus need to set options on the handle to allow this to happen (otherwise I get a ERROR_INTERNET_INVALID_CA (12045) error).  When I do the InternetQueryOption with the handle, however, I get a ERROR_INTERNET_INCORRECT_HANDLE_TYPE (12018) error.
HINTERNET hReq = InternetOpen(...)
  if (!hReq) { printf("InternetOpen Error: %d", GetLastError()); }
  DWORD dwFlags = 0;
  DWORD dwBuffLen = sizeof(dwFlags);
  BOOL ret = false;
  ret = InternetQueryOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
                            (LPVOID)&dwFlags, &dwBuffLen);
  if (!ret) { printf("InternetQueryOption Error: %d", GetLastError()); }
  ret = InternetSetOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
                          &dwFlags, sizeof (dwFlags) );
  if (!ret) { printf("InternetSetOption Error: %d", GetLastError()); }
  InternetOpenURL(hReq, ...)

Open in new window

Who is Participating?
slyphConnect With a Mentor Author Commented:
Well, for anyone who has a similar problem in the future, the answer is that the handle returned by InternetOpen (hReq in the above code) cannot be used with InternetQueryOption/InternetSetOption, at least not in my case.  Its an open question as to why this is the case, since Microsoft obviously thinks that it should work.  Anyway, you need to do use the handle returned by HttpOpenRequest (after an InternetConnect) and Query/Set the options on that handle.  This means that I can not use the (easier) InternetOpenUrl function, and instead had to do an InternetCrackUrl, InternetOpen, InternetConnect, HttpOpenRequest, HttpSendRequest.
Have you initialized the TCP/IP System? Read was  WsaStartup  called?

slyphAuthor Commented:
Couple of comments/questions:
  - No, WSAStartup was not called before this point, but it did not matter (see last comment)
  - Is WSAStartup necessary to call before WinInet functions, or does the WInInet dll implicitly call it for you?
  - Doesn't the fact that I can download a file from an HTTP server indicate that the WSA subsystem is "working"?
  - Just in case, I did call WSAStartup before my attempts, and InternetQueryOption fails in the same way (error 12018).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.