I found a way to determine the endianness using the C preprocessor.
Please be aware that I did not test this code on a big endian machine
It uses some defines of the registry value types [1].
REG_DWORD
is defined there as the endianness of the architecture Windows is running on.
Add the _byteswap_TYPE
functions to the mix and we can write a preprocessor definition [2].
#include <Windows.h>
#if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
# define le_to_host_ulong(VAL) VAL
# define be_to_host_ulong(VAL) _byteswap_ulong(VAL)
# define le_to_host_ushort(VAL) VAL
# define be_to_host_ushort(VAL) _byteswap_ushort(VAL)
# define le_to_host_uint64(VAL) VAL
# define be_to_host_uint64(VAL) _byteswap_uint64(VAL)
#else
# define le_to_host_ulong(VAL) _byteswap_ulong(VAL)
# define be_to_host_ulong(VAL) VAL
# define le_to_host_ushort(VAL) _byteswap_ushort(VAL)
# define be_to_host_ushort(VAL) VAL
# define le_to_host_uint64(VAL) _byteswap_uint64(VAL)
# define be_to_host_uint64(VAL) VAL
#endif
[1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms724884(v=vs.85).aspx
[2] https://msdn.microsoft.com/en-us/library/a3140177.aspx