How to use certselectcertificate on windows 2000

From: scott (sjumsdn_at_newsgroup.nospam)
Date: 04/23/05


Date: Fri, 22 Apr 2005 15:15:02 -0700

The following program gets an access violation on the call to
cryptselectcertificate at line 145. I think I have all the parameters
correct. Can you tell me what I am doing wrong?

#include <winsock2.h>
#include <windows.h>
#include <wincon.h>
#include <wincrypt.h>
#ifndef _WIN32_WINNT
   #define _WIN32_WINNT 0x0500 //GetConsoleWindow needs it
#endif
#include <CryptuiAPI.h>
#include <Cryptdlg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
long CallCertSelectCertificate(HCERTSTORE hCertStore,
                                                                HWND handWindow,
                                                                HINSTANCE* phinstcryptdlgLib,
                                                                PCCERT_CONTEXT* ppCertContext,
                                                                short* psLoadFailure,
                                                                char* szError);
 
long SelectCertificateFromStore(
                                                         char* szCertStoreName,
                                                         /* in */
                                                         HWND lFormHandle,
                                                         /* in */
                             char* szError
                                                         /* out */);
 
void GetSystemErrorString(DWORD dwError,
                                                  char* szErrBuf,
                                                  DWORD nSize);
 
#define PROCESSLASTERROR(func) { \
                err = GetLastError(); \
                sprintf(szError,"%s failed with error %d : ",func,err); \
                GetSystemErrorString(err,&szError[strlen(szError)],500); \
                lRetval = 1; \
        goto cleanup; \
                }
 
typedef PCCERT_CONTEXT (WINAPI
*DefptrCryptUIDlgSelectCertificateFromStore)(HCERTSTORE,HWND,LPCWSTR,LPCWSTR,DWORD,DWORD,void *);
 
#ifdef UNICODE
typedef BOOL (WINAPI *DefptrCertSelectCertificate)(PCERT_SELECT_STRUCT_W
pCertSelectInfo);
#else
typedef BOOL (WINAPI *DefptrCertSelectCertificate)(PCERT_SELECT_STRUCT_A
pCertSelectInfo);
#endif
 
int main()
{
    long lRetval;
    char szError[1024];
    /*HWND myhndl;*/
    
/* myhndl = GetConsoleWindow(); */
    lRetval=SelectCertificateFromStore("CA",NULL,szError);
 
    return 0;
}
 
long SelectCertificateFromStore(
                                                         char* szCertStoreName,
                                                         /* in */
                                                         HWND lFormHandle,
                                                         /* in */
                             char* szError
                                                         /* out */)
{
        long lRetval = 0;
        HCERTSTORE hCertStore = NULL;
        PCCERT_CONTEXT pCertContext = NULL;
        DWORD err;
        HWND handWindow;
        short sLoadFailure = 0;
        HINSTANCE hinstCryptdlgLib = NULL;
 
        if (!( hCertStore = CertOpenSystemStore(
                 NULL,
                 szCertStoreName)))
                 PROCESSLASTERROR("CertOpenSystemStore()")
 
        if (lFormHandle)
                handWindow = lFormHandle;
        else
                handWindow = NULL;
 
        lRetval =
CallCertSelectCertificate(hCertStore,handWindow,&hinstCryptdlgLib,&pCertContext,&sLoadFailure,szError);
        if (lRetval || pCertContext == NULL)
                goto cleanup;
 
cleanup:
        if(pCertContext)
           CertFreeCertificateContext(pCertContext);
        if(hCertStore)
        {
                if (!CertCloseStore(hCertStore,0))
                        err = GetLastError(); /* ignore close errors */
        }
        if (hinstCryptdlgLib) FreeLibrary(hinstCryptdlgLib);
    return lRetval;
}
 
 
long CallCertSelectCertificate(HCERTSTORE hCertStore,
                                                                HWND handWindow,
                                                                HINSTANCE* phinstcryptdlgLib,
                                                                PCCERT_CONTEXT* ppCertContext,
                                                                short* psLoadFailure,
                                                                char* szError)
{
        long lRetval = 0;
        DWORD err;
        DefptrCertSelectCertificate ptrCertSelectCertificate;
        CERT_SELECT_STRUCT struCertSelect;
    HCERTSTORE cStoreArray[1];
 
        *psLoadFailure = 0;
        *phinstcryptdlgLib = NULL;
        memset(&struCertSelect,0,sizeof(struCertSelect));
        *phinstcryptdlgLib = LoadLibrary("cryptdlg");
        if (*phinstcryptdlgLib == NULL)
        {
                *psLoadFailure = 1;
                PROCESSLASTERROR("LoadLibrary(cryptdlg)")
        }
        #ifdef UNICODE
        ptrCertSelectCertificate =
                (DefptrCertSelectCertificate) GetProcAddress(*phinstcryptdlgLib,
"CertSelectCertificateW");
        #else
        ptrCertSelectCertificate =
                (DefptrCertSelectCertificate) GetProcAddress(*phinstcryptdlgLib,
"CertSelectCertificateA");
        #endif
        if (ptrCertSelectCertificate == NULL)
        {
                *psLoadFailure = 1;
                PROCESSLASTERROR("GetProcAddress(CertSelectCertificate)")
        }
        struCertSelect.dwSize = sizeof(struCertSelect);
        struCertSelect.hwndParent = NULL; /*handWindow;*/
    struCertSelect.szTitle="Your Title";
        struCertSelect.cCertStore = (DWORD)1;
    cStoreArray[0] = hCertStore;
        struCertSelect.arrayCertStore = cStoreArray;
        if((ptrCertSelectCertificate)(&struCertSelect) == FALSE)
                PROCESSLASTERROR("CertSelectCertificate")
        *ppCertContext = *struCertSelect.arrayCertContext;
 
cleanup:
        return lRetval;
}
 
void GetSystemErrorString(DWORD dwError,
                                                  char* szErrBuf,
                                                  DWORD nSize)
{
        *szErrBuf = 0;
        FormatMessage(
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                dwError,
                0, // Default language
                (LPTSTR) szErrBuf,
                nSize,
                NULL
        );
}
 



Relevant Pages

  • Re: Cant find TouchCalibrate()
    ... typedef BOOL (WINAPI* PTouchCalibrate); ... int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: IcmpCloseHandle
    ... about specifing calling convencion explicitly, ... typedef BOOL (WINAPI *PIcmpCloseHandle)(HANDLE IcmpHandle); ...
    (microsoft.public.win32.programmer.networks)