C++: structs: get struct sizes, get member offsets/sizes

Discuss other programming languages besides AutoHotkey
User avatar
jeeswg
Posts: 5956
Joined: 19 Dec 2016, 01:58
Location: UK

C++: structs: get struct sizes, get member offsets/sizes

12 Feb 2019, 20:48

In this link I list various Winapi structs and information about them:
list of structs with parameters (sizes and types) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=74&t=30497

Below I demonstrate some C++ code (for Visual Studio) to get struct sizes, and get member offsets/sizes. Cheers.

Code: Select all

// ConsoleApplicationStructsGetSizes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream> //for std::cout

#include <DbgHelp.h>
#include <ShlObj.h>
#include <Richedit.h>
#include <Shlwapi.h>
#include <gdiplus.h>
#include <WinInet.h>
#include <HtmlHelp.h>
#include <in6addr.h>
#include <TlHelp32.h>
#include <winternl.h>

//note: GdiplusStartupInput requires leading 'Gdiplus::'
//note: DLGTEMPLATEEX is not handled, '?' is output instead of sizeof(DLGTEMPLATEEX)

int _tmain(int argc, _TCHAR* argv[])
{
	//some struct definitions:

	//DLGTEMPLATEEX structure - Windows applications | Microsoft Docs
	//https://docs.microsoft.com/en-us/windows/desktop/dlgbox/dlgtemplateex

	//_TIME_ZONE_INFORMATION | Microsoft Docs
	//https://docs.microsoft.com/en-us/windows/desktop/api/timezoneapi/ns-timezoneapi-_time_zone_information
	typedef struct _REG_TZI_FORMAT
	{
		LONG Bias;
		LONG StandardBias;
		LONG DaylightBias;
		SYSTEMTIME StandardDate;
		SYSTEMTIME DaylightDate;
	} REG_TZI_FORMAT;

	//Shell Tray Info - Arrange your system tray icons - CodeProject
	//https://www.codeproject.com/Articles/10807/%2FArticles%2F10807%2FShell-Tray-Info-Arrange-your-system-tray-icons
	//The dwData member of each TBBUTTON structure of the toolbar points to an undocumented structure. The first few bytes of the structure are as follows (on XP anyway) :-
	struct TRAYDATA
	{
		HWND hwnd;
		UINT uID;
		UINT uCallbackMessage;
		DWORD Reserved[2];
		HICON hIcon;
	};

	//==================================================

	//getting the size of various structs:

	std::cout << sizeof(ACCEL) << " ACCEL" << std::endl;
	std::cout << sizeof(ACCESSTIMEOUT) << " ACCESSTIMEOUT" << std::endl;
	std::cout << sizeof(ANIMATIONINFO) << " ANIMATIONINFO" << std::endl;
	std::cout << sizeof(API_VERSION) << " API_VERSION" << std::endl;
	std::cout << sizeof(APPBARDATA) << " APPBARDATA" << std::endl;
	std::cout << sizeof(AUDIODESCRIPTION) << " AUDIODESCRIPTION" << std::endl;
	std::cout << sizeof(BIND_OPTS) << " BIND_OPTS" << std::endl;
	std::cout << sizeof(BITMAP) << " BITMAP" << std::endl;
	std::cout << sizeof(BITMAPINFO) << " BITMAPINFO" << std::endl;
	std::cout << sizeof(BITMAPINFOHEADER) << " BITMAPINFOHEADER" << std::endl;
	std::cout << sizeof(BLENDFUNCTION) << " BLENDFUNCTION" << std::endl;
	std::cout << sizeof(BROWSEINFO) << " BROWSEINFO" << std::endl;
	std::cout << sizeof(CHARFORMAT2) << " CHARFORMAT2" << std::endl;
	std::cout << sizeof(CHARFORMAT) << " CHARFORMAT" << std::endl;
	std::cout << sizeof(CLIENTCREATESTRUCT) << " CLIENTCREATESTRUCT" << std::endl;
	std::cout << sizeof(CM_COLUMNINFO) << " CM_COLUMNINFO" << std::endl;
	std::cout << sizeof(COMBOBOXINFO) << " COMBOBOXINFO" << std::endl;
	std::cout << sizeof(COMDLG_FILTERSPEC) << " COMDLG_FILTERSPEC" << std::endl;
	std::cout << sizeof(COPYDATASTRUCT) << " COPYDATASTRUCT" << std::endl;
	std::cout << sizeof(COSERVERINFO) << " COSERVERINFO" << std::endl;
	std::cout << sizeof(CREATESTRUCT) << " CREATESTRUCT" << std::endl;
	std::cout << sizeof(CURSORINFO) << " CURSORINFO" << std::endl;
	std::cout << sizeof(DEVMODE) << " DEVMODE" << std::endl;
	std::cout << sizeof(DIBSECTION) << " DIBSECTION" << std::endl;
	std::cout << sizeof(DISPLAY_DEVICE) << " DISPLAY_DEVICE" << std::endl;
	std::cout << sizeof(DISPPARAMS) << " DISPPARAMS" << std::endl;
	std::cout << "?" << " DLGTEMPLATEEX" << std::endl;
	std::cout << sizeof(DLLVERSIONINFO2) << " DLLVERSIONINFO2" << std::endl;
	std::cout << sizeof(DLLVERSIONINFO) << " DLLVERSIONINFO" << std::endl;
	std::cout << sizeof(DRAWTEXTPARAMS) << " DRAWTEXTPARAMS" << std::endl;
	std::cout << sizeof(EDITSTREAM) << " EDITSTREAM" << std::endl;
	std::cout << sizeof(EVENTLOGRECORD) << " EVENTLOGRECORD" << std::endl;
	std::cout << sizeof(FILETIME) << " FILETIME" << std::endl;
	std::cout << sizeof(FILTERKEYS) << " FILTERKEYS" << std::endl;
	std::cout << sizeof(FORMATETC) << " FORMATETC" << std::endl;
	std::cout << sizeof(Gdiplus::GdiplusStartupInput) << " GdiplusStartupInput" << std::endl;
	std::cout << sizeof(GETTEXTEX) << " GETTEXTEX" << std::endl;
	std::cout << sizeof(GETTEXTLENGTHEX) << " GETTEXTLENGTHEX" << std::endl;
	std::cout << sizeof(GLYPHSET) << " GLYPHSET" << std::endl;
	std::cout << sizeof(GOPHER_FIND_DATA) << " GOPHER_FIND_DATA" << std::endl;
	std::cout << sizeof(GUID) << " GUID" << std::endl;
	std::cout << sizeof(GUITHREADINFO) << " GUITHREADINFO" << std::endl;
	std::cout << sizeof(HDITEM) << " HDITEM" << std::endl;
	std::cout << sizeof(HH_AKLINK) << " HH_AKLINK" << std::endl;
	std::cout << sizeof(HIGHCONTRAST) << " HIGHCONTRAST" << std::endl;
	std::cout << sizeof(HOSTENT) << " HOSTENT" << std::endl;
	std::cout << sizeof(ICONINFO) << " ICONINFO" << std::endl;
	std::cout << sizeof(ICONMETRICS) << " ICONMETRICS" << std::endl;
	std::cout << sizeof(IMAGELISTDRAWPARAMS) << " IMAGELISTDRAWPARAMS" << std::endl;
	std::cout << sizeof(IN6_ADDR) << " IN6_ADDR" << std::endl;
	std::cout << sizeof(IN_ADDR) << " IN_ADDR" << std::endl;
	std::cout << sizeof(INITCOMMONCONTROLSEX) << " INITCOMMONCONTROLSEX" << std::endl;
	std::cout << sizeof(ITEMIDLIST) << " ITEMIDLIST" << std::endl;
	std::cout << sizeof(LIST_ENTRY) << " LIST_ENTRY" << std::endl;
	std::cout << sizeof(LOADED_IMAGE) << " LOADED_IMAGE" << std::endl;
	std::cout << sizeof(LOGFONT) << " LOGFONT" << std::endl;
	std::cout << sizeof(LOGFONTA) << " LOGFONTA" << std::endl;
	std::cout << sizeof(LOGFONTW) << " LOGFONTW" << std::endl;
	std::cout << sizeof(LOGPALETTE) << " LOGPALETTE" << std::endl;
	std::cout << sizeof(LOGPEN) << " LOGPEN" << std::endl;
	std::cout << sizeof(LUID) << " LUID" << std::endl;
	std::cout << sizeof(LUID_AND_ATTRIBUTES) << " LUID_AND_ATTRIBUTES" << std::endl;
	std::cout << sizeof(LVITEM) << " LVITEM" << std::endl;
	std::cout << sizeof(MDICREATESTRUCT) << " MDICREATESTRUCT" << std::endl;
	std::cout << sizeof(MEASUREITEMSTRUCT) << " MEASUREITEMSTRUCT" << std::endl;
	std::cout << sizeof(MEMORY_BASIC_INFORMATION) << " MEMORY_BASIC_INFORMATION" << std::endl;
	std::cout << sizeof(MENUBARINFO) << " MENUBARINFO" << std::endl;
	std::cout << sizeof(MENUINFO) << " MENUINFO" << std::endl;
	std::cout << sizeof(MENUITEMINFO) << " MENUITEMINFO" << std::endl;
	std::cout << sizeof(MIDIOUTCAPS) << " MIDIOUTCAPS" << std::endl;
	std::cout << sizeof(MINIMIZEDMETRICS) << " MINIMIZEDMETRICS" << std::endl;
	std::cout << sizeof(MINMAXINFO) << " MINMAXINFO" << std::endl;
	std::cout << sizeof(MODULEENTRY32) << " MODULEENTRY32" << std::endl;
	std::cout << sizeof(MONITORINFO) << " MONITORINFO" << std::endl;
	std::cout << sizeof(MONITORINFOEX) << " MONITORINFOEX" << std::endl;
	std::cout << sizeof(MOUSEKEYS) << " MOUSEKEYS" << std::endl;
	std::cout << sizeof(MSG) << " MSG" << std::endl;
	std::cout << sizeof(MSGBOXPARAMS) << " MSGBOXPARAMS" << std::endl;
	std::cout << sizeof(MULTI_QI) << " MULTI_QI" << std::endl;
	std::cout << sizeof(NMHDR) << " NMHDR" << std::endl;
	std::cout << sizeof(NMLVKEYDOWN) << " NMLVKEYDOWN" << std::endl;
	std::cout << sizeof(NONCLIENTMETRICS) << " NONCLIENTMETRICS" << std::endl;
	std::cout << sizeof(NOTIFYICONDATA) << " NOTIFYICONDATA" << std::endl;
	std::cout << sizeof(OLECMD) << " OLECMD" << std::endl;
	std::cout << sizeof(OPENFILENAMEW) << " OPENFILENAMEW" << std::endl;
	std::cout << sizeof(OSVERSIONINFOEX) << " OSVERSIONINFOEX" << std::endl;
	std::cout << sizeof(OVERLAPPED) << " OVERLAPPED" << std::endl;
	std::cout << sizeof(PALETTEENTRY) << " PALETTEENTRY" << std::endl;
	std::cout << sizeof(PEB) << " PEB" << std::endl;
	std::cout << sizeof(POINT) << " POINT" << std::endl;
	std::cout << sizeof(PROCESS_INFORMATION) << " PROCESS_INFORMATION" << std::endl;
	std::cout << sizeof(PROPERTYKEY) << " PROPERTYKEY" << std::endl;
	std::cout << sizeof(REBARBANDINFO) << " REBARBANDINFO" << std::endl;
	std::cout << sizeof(RECT) << " RECT" << std::endl;
	std::cout << sizeof(REG_TZI_FORMAT) << " REG_TZI_FORMAT" << std::endl;
	std::cout << sizeof(RGBQUAD) << " RGBQUAD" << std::endl;
	std::cout << sizeof(SAFEARRAY) << " SAFEARRAY" << std::endl;
	std::cout << sizeof(SAFEARRAYBOUND) << " SAFEARRAYBOUND" << std::endl;
	std::cout << sizeof(SECURITY_ATTRIBUTES) << " SECURITY_ATTRIBUTES" << std::endl;
	std::cout << sizeof(SETTEXTEX) << " SETTEXTEX" << std::endl;
	std::cout << sizeof(SHARDAPPIDINFOLINK) << " SHARDAPPIDINFOLINK" << std::endl;
	std::cout << sizeof(SHELLEXECUTEINFO) << " SHELLEXECUTEINFO" << std::endl;
	std::cout << sizeof(SHELLSTATE) << " SHELLSTATE" << std::endl;
	std::cout << sizeof(SHFILEINFO) << " SHFILEINFO" << std::endl;
	std::cout << sizeof(SHFILEOPSTRUCT) << " SHFILEOPSTRUCT" << std::endl;
	std::cout << sizeof(SHITEMID) << " SHITEMID" << std::endl;
	std::cout << sizeof(SHQUERYRBINFO) << " SHQUERYRBINFO" << std::endl;
	std::cout << sizeof(SIZE) << " SIZE" << std::endl;
	std::cout << sizeof(SOUNDSENTRY) << " SOUNDSENTRY" << std::endl;
	std::cout << sizeof(STARTUPINFO) << " STARTUPINFO" << std::endl;
	std::cout << sizeof(STARTUPINFOEX) << " STARTUPINFOEX" << std::endl;
	std::cout << sizeof(STGMEDIUM) << " STGMEDIUM" << std::endl;
	std::cout << sizeof(STICKYKEYS) << " STICKYKEYS" << std::endl;
	std::cout << sizeof(STRRET) << " STRRET" << std::endl;
	std::cout << sizeof(SYSTEM_INFO) << " SYSTEM_INFO" << std::endl;
	std::cout << sizeof(SYSTEM_POWER_STATUS) << " SYSTEM_POWER_STATUS" << std::endl;
	std::cout << sizeof(SYSTEMTIME) << " SYSTEMTIME" << std::endl;
	std::cout << sizeof(TBBUTTON) << " TBBUTTON" << std::endl;
	std::cout << sizeof(TBBUTTONINFO) << " TBBUTTONINFO" << std::endl;
	std::cout << sizeof(TCITEM) << " TCITEM" << std::endl;
	std::cout << sizeof(TEXTMETRIC) << " TEXTMETRIC" << std::endl;
	std::cout << sizeof(THREADENTRY32) << " THREADENTRY32" << std::endl;
	std::cout << sizeof(TIME_ZONE_INFORMATION) << " TIME_ZONE_INFORMATION" << std::endl;
	std::cout << sizeof(TIMECAPS) << " TIMECAPS" << std::endl;
	std::cout << sizeof(TOGGLEKEYS) << " TOGGLEKEYS" << std::endl;
	std::cout << sizeof(TOKEN_PRIVILEGES) << " TOKEN_PRIVILEGES" << std::endl;
	std::cout << sizeof(TOOLINFO) << " TOOLINFO" << std::endl;
	std::cout << sizeof(TPMPARAMS) << " TPMPARAMS" << std::endl;
	std::cout << sizeof(TRAYDATA) << " TRAYDATA" << std::endl;
	std::cout << sizeof(TVINSERTSTRUCT) << " TVINSERTSTRUCT" << std::endl;
	std::cout << sizeof(TVITEM) << " TVITEM" << std::endl;
	std::cout << sizeof(TVITEMEX) << " TVITEMEX" << std::endl;
	std::cout << sizeof(VARIANT) << " VARIANT" << std::endl;
	std::cout << sizeof(VS_FIXEDFILEINFO) << " VS_FIXEDFILEINFO" << std::endl;
	std::cout << sizeof(WAVEFORMATEX) << " WAVEFORMATEX" << std::endl;
	std::cout << sizeof(WCRANGE) << " WCRANGE" << std::endl;
	std::cout << sizeof(WIN32_FIND_DATA) << " WIN32_FIND_DATA" << std::endl;
	std::cout << sizeof(WINDOWINFO) << " WINDOWINFO" << std::endl;
	std::cout << sizeof(WNDCLASS) << " WNDCLASS" << std::endl;
	std::cout << sizeof(WNDCLASSEX) << " WNDCLASSEX" << std::endl;
	std::cout << sizeof(WSADATA) << " WSADATA" << std::endl;
	std::cout << "" << std::endl;

	//==================================================

	//examples of listing the member offsets/sizes for a struct:

	INT_PTR ptr;

	RECT myRECT;
	ptr = (INT_PTR)&myRECT;
	std::cout << "RECT: offsets" << std::endl;
	std::cout << (INT_PTR)&myRECT.left - ptr << std::endl;
	std::cout << (INT_PTR)&myRECT.top - ptr << std::endl;
	std::cout << (INT_PTR)&myRECT.right - ptr << std::endl;
	std::cout << (INT_PTR)&myRECT.bottom - ptr << std::endl;
	std::cout << "" << std::endl;
	std::cout << "RECT: sizes" << std::endl;
	std::cout << sizeof(myRECT.left) << std::endl;
	std::cout << sizeof(myRECT.top) << std::endl;
	std::cout << sizeof(myRECT.right) << std::endl;
	std::cout << sizeof(myRECT.bottom) << std::endl;
	std::cout << "" << std::endl;

	Gdiplus::GdiplusStartupInput myGdiplusStartupInput;
	ptr = (INT_PTR)&myGdiplusStartupInput;
	std::cout << "GdiplusStartupInput: offsets" << std::endl;
	std::cout << (INT_PTR)&myGdiplusStartupInput.GdiplusVersion - ptr << std::endl;
	std::cout << (INT_PTR)&myGdiplusStartupInput.DebugEventCallback - ptr << std::endl;
	std::cout << (INT_PTR)&myGdiplusStartupInput.SuppressBackgroundThread - ptr << std::endl;
	std::cout << (INT_PTR)&myGdiplusStartupInput.SuppressExternalCodecs - ptr << std::endl;
	std::cout << "" << std::endl;
	std::cout << "GdiplusStartupInput: sizes" << std::endl;
	std::cout << sizeof(myGdiplusStartupInput.GdiplusVersion) << std::endl;
	std::cout << sizeof(myGdiplusStartupInput.DebugEventCallback) << std::endl;
	std::cout << sizeof(myGdiplusStartupInput.SuppressBackgroundThread) << std::endl;
	std::cout << sizeof(myGdiplusStartupInput.SuppressExternalCodecs) << std::endl;
	std::cout << "" << std::endl;

	//==================================================

	std::getchar();
	return 0;
}
The MSDN page for each struct will tell you which .h file to include.

Thanks to Helgef for this link, with example C++ code, that helped me figure out how to include/use GdiplusStartupInput.
Drawing a Line - Windows applications | Microsoft Docs
https://docs.microsoft.com/en-us/windows/desktop/gdiplus/-gdiplus-drawing-a-line-use

For some information about GdiplusStartupInput and the constructor parameter:
GdiplusStartupInput | Microsoft Docs
https://docs.microsoft.com/en-us/windows/desktop/api/gdiplusinit/ns-gdiplusinit-gdiplusstartupinput
c++ - struct constructor will take space within the struct space? - Stack Overflow
https://stackoverflow.com/questions/30564517/struct-constructor-will-take-space-within-the-struct-space
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 1 guest