使用 C++ 和 bcrypt 头时出现编译错误

jww*_*jww 3 c++ windows compiler-errors cng

我正在尝试测试 Windows Bcrypt。我有一个测试程序:

#include <bcrypt.h>
#include <iostream>
#include <string>

#pragma comment (lib, "bcrypt.lib")

int main(int argc, char* argv[])
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

尝试编译它:

>cl.exe /DWINVER=0x0600 /TP /GR /EHsc bcrypt-test.cpp /link /out:bcrypt-test.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

bcrypt-test.cpp
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(39):
 error C2059: syntax error: 'return'
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(40):
 error C2143: syntax error: missing ';' before '*'
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(40):
 error C4430: missing type specifier - int assumed. Note: C++ does not support d
efault-int
...
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(681)
: error C3646: 'cbKeyLength': unknown override specifier
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(681)
: fatal error C1003: error count exceeds 100; stopping compilation
Run Code Online (Sandbox Code Playgroud)

我正在使用Visual C++ x64 构建工具命令提示符。据我了解,Bcrypt 需要针对 Vista 或更高版本。WINVER=0x0600应满足要求。我在 MSDN 论坛上发现了类似的问题bcrypt.h build error? ,并且它说使用现代 SDK。我相信Windows Kit SDK应该可以满足要求。

为什么我会遇到编译错误,如何修复它?


第 39 行bcrypt.h是下面的第一个 typedef。为了简洁起见,跳过了序言,如版权和标题保护。

#ifndef WINAPI
#define WINAPI __stdcall
#endif

#ifndef _NTDEF_
typedef _Return_type_success_(return >= 0) LONG NTSTATUS;
typedef NTSTATUS *PNTSTATUS;
#endif

#ifndef BCRYPT_SUCCESS
#define BCRYPT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
Run Code Online (Sandbox Code Playgroud)

Fro*_*yne 5

你缺少一个包含。

#include <Windows.h> // <- Added this
#include <bcrypt.h>
#include <iostream>
#include <string>

#pragma comment (lib, "bcrypt.lib")

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。微软的天才们怎么忘记包含他们自己的 Windows 标头???怎么通过QA的???微软的质量正变得和苹果一样糟糕。 (3认同)