【问题标题】:'CryptQueryObject' was not declared in this scope'CryptQueryObject' 未在此范围内声明
【发布时间】:2021-01-07 03:33:57
【问题描述】:

#include <wincrypt.h>已经添加了,为什么GCC-mingw32编译器会报这个错误?

'CryptQueryObject' 未在此范围内声明

我正在开发 Window 10。

这是我的代码:

#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);

【问题讨论】:

  • 尝试在windows.h之后包含wincrypt.h;切换这两个#includes的顺序。
  • 谢谢,但它不起作用
  • 你定义了WIN32_LEAN_AND_MEAN(可能是编译器命令行)?此外,我认为您不需要明确指定wincrypt.h
  • 我在 wincrypt.h 中看到的代码中没有 CryptQueryObject 函数。但是文档SDK在wincrypt.h中有CryptQueryObject函数。为什么?
  • @tung 这意味着您的 Windows SDK 已经过时并且您需要一个新的,或者您实际上正在使用一个最新的 SDK,它已经删除了 CryptQueryObject,因为它是已弃用,根据documentation:“重要此 API 已弃用。新的和现有的软件应开始使用 Cryptography Next Generation API。Microsoft 可能会在未来的版本中删除此 API。 "

标签: c++ winapi certificate window


【解决方案1】:
  • windows.h之后包含wincrypt.h,否则使用Visual Studio编译时也会报错。
  • 链接crypt32.lib

编译命令如下:

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

修改后的代码如下:

#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);
}

【讨论】:

  • 我执行了上述步骤,但仍然出现旧错误。我意识到我在 wincrypt.h 中缺少一些函数,例如 CryptQueryObject 函数,......但不是全部。和我遇到的错误有关系吗?
  • @tung 你的wincrypt.h来自哪里?您使用哪个 Windows SDK 版本?
  • 我使用的是新版本的 Windows SDK
  • @tung 最新的SDK 19041wincrypt.h中定义了CryptQueryObject函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2016-08-09
  • 2019-02-17
  • 2016-10-27
  • 2016-06-03
  • 2014-09-11
相关资源
最近更新 更多