2012-05-19

Rant: #pragma(pack) evilness

Wasting time on stuff like can ruin a otherwise great day:

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
int main() 
{ 
  printf("size: %d\n", (int)sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION)); 
  return 0; 
}
cl test.cpp && test.exe
size: 44 (wrong)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
int main() 
{ 
  printf("size: %d\n", (int)sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION)); 
  return 0; 
}
cl test.cpp && test.exe
size: 48 (right)

I have just spent a few hours trying to figure out why SetInformationJobObject() kept failing in one of my projects.
And when it's because Microsoft can't decide if LARGE_INTEGER should be aligned to 4 or 8 bytes, I can't help but cursing their name.

The problem apears to be that winsock2.h does a "pragma pack(4)" before including windows.h, this will mess up some of the types defined in windows.h, including JOBOBJECT_BASIC_LIMIT_INFORMATION.

This was tested with VC2010 and the v7.0A sdk.