ERROR_INTERNET_INCORRECT_HANDLE_TYPE with InternetQueryOption/InternetSetOption

Posted on 2008-11-12
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
    LVL 24

    Expert Comment

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


    Author Comment

    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    ShiftLeft challenge 21 50
    withoutTen challenge 14 69
    squareUp  challenge 22 81
    java continue statement 10 52
    Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    This video teaches viewers about errors in exception handling.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now