Matt
Matt

Reputation: 359

Syntax error C2059 when building building C application using C/C++ DLL header

I'm attempting to translate a C++ DLL header file into a C/C++ compatible header. While I've gotten most of the major constructs in, I'm running into one last compiler issue I can't seem to explain. The following code works fine in C++ but when I attempt to compile a C application which just includes this file I get errors for my function definitions in my header file.

Code.h:

typedef void *PVOID;
typedef PVOID HANDLE;
#define WINAPI  __stdcall

#ifdef LIB_EXPORTS
    #define LIB_API __declspec(dllexport)
#else
    #define LIB_API __declspec(dllimport)
#endif

struct ToolState
{
    HANDLE DriverHandle;
    HANDLE Mutex;
    int LockEnabled;
    int Type;
};

#ifdef __cplusplus
extern "C" {
#endif

(LIB_API) int SetRate(ToolState *Driver, int rate);

(LIB_API) void EnableLock(ToolState *Driver) ;

(LIB_API) int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);

//These also give me the same error:
//LIB_API WINAPI int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//__declspec(dllimport) WINAPI int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);

//Original C++ call that works fine with C++ but has multiple issues in C
//LIB_API int SetRate(ToolState *Driver, int rate);

#ifdef __cplusplus
}
#endif

Errors:

error C2059: syntax error : 'type'
error C2059: syntax error : 'type'
error C2059: syntax error : 'type'

Google searching hasn't generated any relevant results. The following threads were close but don't exactly answer my question:

C2059 syntax error using declspec macro for one function; compiles fine without it

http://support.microsoft.com/kb/117687/en-us

Why is this syntax error occuring?

Upvotes: 1

Views: 1811

Answers (1)

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17434

In C, structs are not types, so you must use struct Foo and enum Bar where in C++ you are able to use Foo and Bar.

Notes:

  • In C++, you can still use the old syntax even when the type is a class.
  • In C, people often use typedef struct Foo Foo which allows the same syntax as in C++ then.

Upvotes: 2

Related Questions