tags:

views:

79

answers:

5

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.

+4  A: 

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

All it expands to is a struct initializer.

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

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.

Goz
+4  A: 

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.

sblom
did not know that unicode_string was a structure. thanks for the answers all!
bakra
+1  A: 

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.

Jens Gustedt