Use WinInet::InternetSetOption() always returns false and GetLastError() returns 12018

StackOverflow https://stackoverflow.com/questions/20971660

  •  25-09-2022
  •  | 
  •  

سؤال

I want to let the ie's IWebBrowser didn't need to select the certificate, so I use InternetSetOption() to do that. The code is:

HCERTSTORE hMyStore = CertOpenSystemStore(0, _T("MY");
PCCERT_CONTEXT pDesiredCert = NULL;
pDesiredCert=CertFindCertificateInStore( 
    hMyStore, 
    X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
    CERT_FIND_SUBJECT_STR,
    L"cnstr",
    NULL);

if (InternetSetOption (NULL, INTERNET_OPTION_CLIENT_CERT_CONTEXT, 
        (LPVOID)pDesiredCert, sizeof(CERT_CONTEXT)) == FALSE) { 
    DOWRD i = GetLastError(); 
} 
...

I get the certificate in the "MY" store. When I use InternetSetOption() to set the certificate, it return false, the error code is 12018. I searched some infomation about this error code:

"12018 ERROR_INTERNET_INCORRECT_HANDLE_TYPE The type of handle supplied is incorrect for this operation."

I want to set the global setting, so I set NULL to the first parameter, does the first parameter in the function InternetSetOption() couldn't be NULL? And how could I set the option correctly?

هل كانت مفيدة؟

المحلول

As you already told, you can not use NULL for INTERNET_OPTION_CLIENT_CERT_CONTEXT option. Although, some option can possessNULL value, such as INTERNET_OPTION_CONNECT_RETRIES or INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, it can't.

Option Flags

You have to use a valid HINTERNET by using InternetOpen() or InternetConnect(). Below link will guide you for valid HINTERNET on 'Scope of HINTERNET Handle' section.

Setting and Retrieving Internet Options

And, you have to check hMyStore and pDesiredCert values whether those are NULL, also.

**EDIT : **

You can not get HINTERNET by using IWebBrowser2. It just exposes methods and property of WebBrowser control. So, you have to use InternetConnect().

For example, client applications that require a proxy with authentication, probably do not require setting the proxy user name and password every time the application attempts to access an Internet resource. If all requests on a given connection are handled by the same proxy, setting the proxy user name and password on a connection type HINTERNET handle, that is, a handle created by a call to InternetConnect, would allow any calls derived from this HINTERNET handle to use the same proxy user name and password. Setting the proxy user name and password every time an HINTERNET handle is created by HttpOpenRequest would require extra and unnecessary overhead. Be aware that if the application uses a proxy that requires authentication, it should set the proxy credentials on every new connection.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa385384(v=vs.85).aspx

I also wrote some useful reference link about how to use InternetConnect().

C++ WinHTTP InternetReadFile not retrieving entire source

SSL with WinHTTP

نصائح أخرى

I believe the applied answer is correct. Though there exists another problem, which reveals itself in a similar manner.
You can get error "INCORRECT_HANDLE_TYPE", if the constant INTERNET_OPTION_CLIENT_CERT_CONTEXT is incorrectly defined. In this case you just will be passing the wrong option. The right value is 84 (dec) in WinInet

#define INTERNET_OPTION_CLIENT_CERT_CONTEXT 84

the corresponding constant in WinHttp should be

#define WINHTTP_OPTION_CLIENT_CERT_CONTEXT 47

Do not mix them. Be cautious and have a good time coding!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top