jww
jww

Reputation: 102205

Compile errors when using C++ and bcrypt header

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

Upvotes: 4

Views: 4337

Answers (1)

Frodyne
Frodyne

Reputation: 3973

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

Upvotes: 5

Related Questions