使用 C/C++ DLL header 构建构建 C 应用程序时出现语法错误 C2059

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

我正在尝试将 C++ DLL header 文件转换为 C/C++ 兼容的 header。虽然我已经掌握了大部分主要构造,但我 运行 陷入了最后一个我似乎无法解释的编译器问题。以下代码在 C++ 中运行良好,但是当我尝试编译仅包含此文件的 C 应用程序时,我的 header 文件中的函数定义出现错误。

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

错误:

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

Google 搜索未生成任何相关结果。以下主题很接近,但没有完全回答我的问题:

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

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

为什么会出现这个语法错误?

在 C 中,结构不是类型,因此您必须使用 struct Fooenum Bar,而在 C++ 中您可以使用 FooBar

备注:

  • 在 C++ 中,即使类型为 class,您仍然可以使用旧语法。
  • 在 C 中,人们经常使用 typedef struct Foo Foo,它允许使用与 C++ 中相同的语法。