2

I'm trying to test Windows Bcrypt. I have a test program:

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

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

int main(int argc, char* argv[])
{
    return 0;
}

Attempting to compile it:

>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

I am using a Visual C++ x64 Build Tools command prompt. As I understand things, Bcrypt needs to target Vista or above. WINVER=0x0600 should satisfy the requirement. I found a similar question on the MSDN forums at bcrypt.h build error?, and it says to use a modern SDK. I believe the Windows Kit SDK should satisfy the requirement.

Why am I experiencing compile errors, and how do I fix it?


Line 39 in bcrypt.h is the first typedef below. The preamble, like copyright and header guards, were skipped for brevity.

#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
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    Unless Visual Studio have some very weird language extension (which isn't unusual unfortunately) the `typedef _Return_type_success_...` line is invalid. – Some programmer dude Aug 13 '19 at 07:18
  • This error looks like a replay of [How do you compile WinCrypt.h under Visual C++?](https://stackoverflow.com/q/5665707/608639). I'd mark it as a dup except it is a different header. Why does Microsoft leave these bugs to fester for a decade? – jww Sep 17 '19 at 04:41

1 Answers1

3

You are missing an include.

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

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

int main()
{
    return 0;
}
Frodyne
  • 3,547
  • 6
  • 16
  • 1
    Thanks. How did the geniuses at Microsoft forget to include their own windows header??? How did it get past QA??? Microsoft is becoming as bad as Apple with their quality. – jww Aug 13 '19 at 11:55
  • @jww This looks intentional. Maybe its a way to enforce good include practices. [Header files and include best practice](https://stackoverflow.com/questions/15420891/header-files-and-include-best-practice) – Alex Wolski Apr 05 '22 at 22:41