C++ Header File Errors

2019-07-26 20:14发布

问题:

I have been tasked to create a C# interface with some of the methods that are being using in the Open Source CrytoLib C++ project. I am trying to create a managed wrapper for the LIB file... however I am getting some errors already and cannot figure out what I am doing wrong as this seems pretty straightforward to this point.

My header file:

// CryptoLibWrapper.h

#pragma once

using namespace System;

namespace CryptoLibWrapper {

public ref class DefaultDecryptorWithMAC
{
public:
    BOOL Decrypt(BYTE const* pEncrypted, UINT uLength, BYTE** ppBuffer, DWORD* pdwLength);
};
}

The errors I am getting...

error C2061: syntax error : identifier 'BYTE'

error C2146: syntax error : missing ';' before identifier 'Decrypt'

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

It has been a while since I have done any C++ and even that was limited, hoping this is easy and I am just being brain dead.

Thanks again!

EDIT: Note... all of the errors are at the "BOOL Decrypt..." line

回答1:

Seems like you're probably getting the first error because you're missing some type definitions. The other errors are probably just as a result of those missing definitions.

You need to include a file that defines BYTE. Putting this at the top of your file should do the job:

#include <windows.h>

or if you don't care about pulling in the whole of the windows headers, you could try:

#include <windef.h>


回答2:

It's telling you that it doesn't recognize the type BYTE - that could be causing the errors after that. So you need to either define BYTE before your class declaration or you need to #include the header file that defines BYTE. The rest of your definition looks fine to me.



回答3:

BYTE is user-defined type, therefore you need to include a header file that defines it. The header file that defines Windows data types is <WinDef.h>,



回答4:

You are a C# programmer, aren't you ;-). It looks like you just copied the C# style "using System;" If this is the case, you need to

#using <mscorlib.dll>

before

using namespace System;

See also http://en.wikipedia.org/wiki/C%2B%2B/CLI

But please, never use "using namespace" inside a header file.