bakra
bakra

Reputation: 397

#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.

Upvotes: 0

Views: 336

Answers (5)

Jens Gustedt
Jens Gustedt

Reputation: 78903

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.

Upvotes: 1

sblom
sblom

Reputation: 27343

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.

Upvotes: 4

Goz
Goz

Reputation: 62323

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.

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

All it expands to is a struct initializer.

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

Upvotes: 0

Ben Zotto
Ben Zotto

Reputation: 71008

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

Upvotes: 4

Related Questions