c - 为什么我不能声明一个已经定义了结构的变量?

标签 c windows compiler-errors macros c-preprocessor

这个问题可能不像你最初想的那么容易解决。

FILTER_MESSAGE_HEADER 是头文件 fltUserStructures.h 中定义的结构,它是位于 SDK 包含路径中的标准 Windows SDK 头文件,即

"C:\Program Files (x86)\Windows Kits\8.0\Include\shared\fltUserStructures.h".

typedef struct _FILTER_MESSAGE_HEADER {

    //
    //  OUT
    //
    //  Total buffer length in bytes, including the FILTER_REPLY_HEADER, of
    //  the expected reply.  If no reply is expected, 0 is returned.
    //

    ULONG ReplyLength;

    //
    //  OUT
    //
    //  Unique Id for this message.  This will be set when the kernel message
    //  satifies this FilterGetMessage or FilterInstanceGetMessage request.
    //  If replying to this message, this is the MessageId that should be used.
    //

    ULONGLONG MessageId;

    //
    //  General filter-specific buffer data follows...
    //

} FILTER_MESSAGE_HEADER, *PFILTER_MESSAGE_HEADER;

但是,VC++ 2012无法编译以下代码。

#include <fltUserStructures.h>

int main()
{
    //
    // error C2065: 'FILTER_MESSAGE_HEADER' : undeclared identifier
    //
    FILTER_MESSAGE_HEADER v; 
}

#define NTDDI_VERSION 0x06000000 // Vista or later
#include <FltUser.h>

int main()
{
    //
    // fltuserstructures.h(27): fatal error C1012:
    // unmatched parenthesis : missing ')'
    //
    FILTER_MESSAGE_HEADER v; 
}

虽然我试过很多方法,编译器总是拒绝上面的代码。根本原因是什么?

最佳答案

该结构(以及 fltUserStructures.h 中的大部分内容是基于 FLT_MGR_BASELINE 有条件编译的,而 fltUser.h 又在 #include <fltuser.h> 中设置:

#define FLT_MGR_BASELINE (((OSVER(NTDDI_VERSION) == NTDDI_WIN2K) && (SPVER(NTDDI_VERSION) >= SPVER(NTDDI_WIN2KSP4))) || \
                          ((OSVER(NTDDI_VERSION) == NTDDI_WINXP) && (SPVER(NTDDI_VERSION) >= SPVER(NTDDI_WINXPSP2))) || \
                          ((OSVER(NTDDI_VERSION) == NTDDI_WS03)  && (SPVER(NTDDI_VERSION) >= SPVER(NTDDI_WS03SP1))) ||  \
                          (NTDDI_VERSION >= NTDDI_VISTA))

因此,改为使用 NTDDI_VERSION,并确保正确设置 WINVER(例如,使用 ojit_code):

#define WINVER 0x0600
#include <windows.h>
#include <fltUser.h>

int main()
{
    //
    // error C2065: 'FILTER_MESSAGE_HEADER' : undeclared identifier
    //
    FILTER_MESSAGE_HEADER v; 
}

关于c - 为什么我不能声明一个已经定义了结构的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803233/

相关文章:

c - 通过结构体传递字符串

windows - 通过注册表检测已安装的程序

java - Android Studio 4构建失败,导致错误= 2没有此类文件或目录

c - strtok_r 造成的内存损坏

c - POSIX 生产者-消费者

c - 进程间通信的普通管道

java - 如何根据操作系统更改文件路径

用于从文件拖放进行打印和存档的 Windows 实用程序

ios - 没有已知的类- iOS

.net - 不支持的重复项目不断出现