Search code examples
c++winapicertificatewindow

'CryptQueryObject' was not declared in this scope


#include <wincrypt.h> has already been added, so why does the GCC-mingw32 compiler report this error?

'CryptQueryObject' was not declared in this scope

I'm working on Window 10.

Here is my code:

#include <iostream>
#include <wincrypt.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
//#pragma comment(lib, "crypt32.lib")
#define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
using namespace std;

int _tmain(int argc, TCHAR *argv[])
{
    BOOL bIsSuccess;
    DWORD dwEncoding, dwContentType, dwFormatType;
    HCERTSTORE hStore = NULL;
    HCRYPTMSG hMsg =NULL;
    PVOID pvContext = NULL;
    string szFileName;
    szFileName ="C:\\WINDOWS\\System32\\lsass.exe";
    bIsSuccess = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
                               szFileName,
                               CERT_QUERY_CONTENT_FLAG_ALL,
                               CERT_QUERY_FORMAT_FLAG_ALL,
                               0,
                               &dwEncoding,
                               &dwContentType,
                               &dwFormatType,
                               &hStore,
                               &hMsg,
                               &pvContext);

Solution

    • Including wincrypt.h after windows.h otherwise it also report errors when compiling with Visual Studio.
    • Link crypt32.lib

    Compile command like this:

    i686-w64-mingw32-g++ test.cpp -lcrypt32
    

    Edited code as below:

    #include <iostream>
    #include <windows.h>
    #include <wincrypt.h>
    #include <tchar.h>
    #include <stdio.h>
    //#pragma comment(lib, "crypt32.lib")
    #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[])
    {
        BOOL bIsSuccess;
        DWORD dwEncoding, dwContentType, dwFormatType;
        HCERTSTORE hStore = NULL;
        HCRYPTMSG hMsg = NULL;
        PVOID pvContext = NULL;
        string szFileName;
        szFileName = "C:\\WINDOWS\\System32\\lsass.exe";
        bIsSuccess = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
            (const void*)szFileName.c_str(),
            CERT_QUERY_CONTENT_FLAG_ALL,
            CERT_QUERY_FORMAT_FLAG_ALL,
            0,
            &dwEncoding,
            &dwContentType,
            &dwFormatType,
            &hStore,
            &hMsg,
            (const void**)&pvContext);
    }