[Webinar] Learn how to a build a cloud-first strategyRegister Now


ERROR_INTERNET_INCORRECT_HANDLE_TYPE with InternetQueryOption/InternetSetOption

Posted on 2008-11-12
Medium Priority
Last Modified: 2013-12-04
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

Question by:slyph
  • 2
LVL 24

Expert Comment

ID: 22941125
Have you initialized the TCP/IP System? Read was  WsaStartup  called?


Author Comment

ID: 22941650
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).

Accepted Solution

slyph earned 0 total points
ID: 22953281
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.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question