#define 带参数...为什么这样有效?

发布于 2024-09-24 12:46:59 字数 326 浏览 2 评论 0原文

这是怎么回事?

#define CONSTANT_UNICODE_STRING(s)   \
                    { sizeof( s ) - sizeof( WCHAR ), sizeof(s), s }
.
.
.
.
UNICODE_STRING gInsufficientResourcesUnicode
             = CONSTANT_UNICODE_STRING(L"[-= Insufficient Resources =-]");

这段代码正在运行。

我需要查看预处理器扩展, 宏定义中的逗号怎么了?

What is going on here?

#define CONSTANT_UNICODE_STRING(s)   \
                    { sizeof( s ) - sizeof( WCHAR ), sizeof(s), s }
.
.
.
.
UNICODE_STRING gInsufficientResourcesUnicode
             = CONSTANT_UNICODE_STRING(L"[-= Insufficient Resources =-]");

This code is working.

I need to see the pre-processors expansion,
and whats up with commas in macro definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇见了你 2024-10-01 12:46:59

UNICODE_STRING 中定义为具有两个大小的类型,后跟一个指向字符串的指针。

typedef struct _UNICODE_STRING {
    USHORT Length;
    USHORT MaximumLength;
    PWSTR  Buffer;
} UNICODE_STRING;

宏中的逗号分隔结构中字段的值。

UNICODE_STRING is defined in <winternl.h> as a type that has two sizes followed by a pointer to a string.

typedef struct _UNICODE_STRING {
    USHORT Length;
    USHORT MaximumLength;
    PWSTR  Buffer;
} UNICODE_STRING;

The commas in the macro separate the values for the fields in the structure.

就此别过 2024-10-01 12:46:59

宏不是作为“函数”运行的;而是作为“函数”运行的。逗号在那里是因为它是结构初始化。

大概有一个名为 UNICODE_STRING 的结构,其中定义了三个字段。该宏允许您根据正在使用的字符串一次性初始化结构,并适当地填写大小字段。

最后一条语句相当于写:

UNICODE_STRING gInsufficientResourcesUnicode = {
    sizeof(L"[-= Insufficient Resources =-]") - sizeof(WCHAR),
    sizeof(L"[-= Insufficient Resources =-]"),
    L"[-= Insufficient Resources =-]"
};

The macro isn't functioning as a "function"; the commas are there because it's a struct initialization.

Presumably there is a structure somewhere called UNICODE_STRING defined with three fields in it. The macro allows you to initialize the struct in one go based on the string you're using, and fills out the size fields appropriately.

The last statement is equivalent to writing:

UNICODE_STRING gInsufficientResourcesUnicode = {
    sizeof(L"[-= Insufficient Resources =-]") - sizeof(WCHAR),
    sizeof(L"[-= Insufficient Resources =-]"),
    L"[-= Insufficient Resources =-]"
};
心如荒岛 2024-10-01 12:46:59

通常,您可以通过使用 gcc 向编译器提供 -E 选项而不是 -c 选项来获得源文件的预处理器扩展举个例子:

gcc -Wall [your other options go here] -E myfile.c

在类 UNIX 系统(linux、OS X)上,您通常还有一个名为 cpp 的独立预处理器。

Usually you may get the preprocessor expansion of a source file by giving the -E option to the compiler instead of the -c option, with gcc as an example:

gcc -Wall [your other options go here] -E myfile.c

on unix like systems (linux, OS X) you often also have a stand-alone preprocessor called cpp.

有深☉意 2024-10-01 12:46:59

它扩展为一个结构初始值设定项。

UNICODE_STRING gInsufficientResourcesUnicode = { sizeof(L"[-= Insufficient Resources =-]") - sizeof ( WCHAR ), sizeof(L"[-= Insufficient Resources =-]"), L"[-= Insufficient Resources =-]" };

All it expands to is a struct initializer.

UNICODE_STRING gInsufficientResourcesUnicode = { sizeof(L"[-= Insufficient Resources =-]") - sizeof ( WCHAR ), sizeof(L"[-= Insufficient Resources =-]"), L"[-= Insufficient Resources =-]" };
狼性发作 2024-10-01 12:46:59

它扩展为:

UNICODE_STRING gInsufficientResourcesUnicode = { sizeof( L"[-= Insufficient Resources =-]" ) - sizeof( WCHAR ), 
                                                 sizeof( L"[-= Insufficient Resources =-]" ), 
                                                 L"[-= Insufficient Resources =-]" 
                                               };

基本上它初始化一个包含 3 个成员的结构。一是不带空终止符的常量字符串的长度。接下来是带有空终止符的字符串的长度,最后一个是实际字符串。逗号只是结构初始化形式的一部分。

Well it expands to:

UNICODE_STRING gInsufficientResourcesUnicode = { sizeof( L"[-= Insufficient Resources =-]" ) - sizeof( WCHAR ), 
                                                 sizeof( L"[-= Insufficient Resources =-]" ), 
                                                 L"[-= Insufficient Resources =-]" 
                                               };

Basically its initialising a struct that contains 3 members. One is the length of the constant string without the null terminator. The next is the length of the string WITH the null terminator and the final is the actual string. The commas are just part of the struct initialisation form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文