开发者

#define with parameters...why is this working?

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.


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.


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 =-]"
};


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.


All it expands to is a struct initializer.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜