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()); }
 
  dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
  ret = InternetSetOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
                          &dwFlags, sizeof (dwFlags) );
  if (!ret) { printf("InternetSetOption Error: %d", GetLastError()); }
 
  InternetOpenURL(hReq, ...)

Open in new window

slyphAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fridomCEO/ProgrammerCommented:
Have you initialized the TCP/IP System? Read was  WsaStartup  called?
http://msdn.microsoft.com/en-us/library/ms742213(VS.85).aspx

Regards
Friedrich
0
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).
0
slyphAuthor 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.