对_imp__CryptProtectData@28的未定义引用

Undefined reference to _imp__CryptProtectData@28

本文关键字:未定义 引用 CryptProtectData@28 imp      更新时间:2023-10-16

我正在尝试使用windows.h和wincrypt.h库构建一个简单的应用程序,以便加密一些字符串。

当我调用函数CryptProtectData(&input, NULL, NULL, NULL, NULL, 0, &output);时,我得到错误:

error: undefined reference to `_imp__CryptProtectData@28'

我在网上搜索了很多,但没有很多。我还意识到Chromium浏览器使用与我类似的代码来加密和解密其登录,我并没有做什么不同的事情。

我正在使用QtCreator IDE编译我的代码。

我的一些代码:

  std::string plaintext="Some plain text";
  DATA_BLOB input;
  input.pbData = const_cast<BYTE*>(
      reinterpret_cast<const BYTE*>(plaintext.data()));
  input.cbData = static_cast<DWORD>(plaintext.length());
  DATA_BLOB output;
  BOOL result = CryptProtectData(&input, NULL, NULL, NULL, NULL,
                                 0, &output);

编辑:忘了说我已经包括了windows.h和wincrypt.h库,当然。

这是由于您没有提供CryptProtectData函数所需的库文件而导致的链接器错误。您需要将Crypt32库传递给链接器。

此信息包含在该函数的MSDN文档中。向下滚动到主题的底部以查看信息。

作为一般规则,为了使用API函数,您需要查看函数文档中的Requirements部分。它列出了以下信息:
  • 最小支持的Windows版本。
  • 需要包含的头文件。
  • 需要传递给链接器的库文件。

您是否链接到Crypt32.dllCrypt32.lib ?将.lib添加到依赖项中。遇到此类错误时,请参阅文档