Enhanced SizeOf Utility with Sample Application and WinAPI Tutorial Demo & Tests

More
5 months 1 week ago #280 by John Fauss
John Fauss created the code: Enhanced SizeOf Utility with Sample Application and WinAPI Tutorial Demo & T
PB does not currently have a "SizeOf" function for returning the amount of memory required by a variable of a standard datatype or a structure. The ability to dynamically determine the size of a structure is very helpful when setting the "size" member required in some WIndows API (WinAPI) structures, particularly if it could take into account member boundary alignment padding and other conventions for both the 32-bit and 64-bit environments.

Roland Smith provides a free code sample (named "SizeOf") on his TopWizProgramming.com website for calculating the size of a structure. The calculation logic is contained within a single non-visual object that was originally authored many years ago by Rui Cruz. Roland enhanced the original object to handle the datatypes added to PB in version 12.6. With Roland's permission, I've coded several additional enhancements to the object to address some features needed for the 64-bit environment and I'm making the enhanced version available to the Appeon Community. These enhancements include:

  • It recognizes the process bitness and uses that value as the default to determine member and structure size.
  • Either bitness can be optionally requested prior to requesting a SizeOf calculation on a structure. A sample application is included that uses this feature to display the size of several types of structures for 32-bit and 64-bit.
  • It can determine boundary alignment for structure members and nested structures using either the progma_pack( 8 ) or progma_pack( 1 ) rules and includes padding bytes in the size calculations to address alignment issues when needed.
  • It can calculate member and structure size for either Unicode or ANSI characters/strings.
  • It can produce a report that describes the layout of a structure in memory, including nested structures.

The Structure Layout sample application demonstrates how the structure SizeOf calculator works by determining the size of approximately thirty structures (mostly WinAPI structures) for both the 32-bit and 64-bit environments. Some WinAPI structures are large and some are small. Some grow in size in a 64-bit environment and others do not.

The sample application also can perform several test suites related to a four-part tutorial titled "Interfacing PB Applications with the Windows API", which will soon be published in the Appeon Community "Articles & Blogs" section.

The enhanced SizeOf non-visual object and the Structure Layout sample application were developed using PB 2017 R2 and can be compiled into either a 32-bit or 64-bit executable. It has been successfully migrated and tested in PB 2019 R2.

This message has an attachment file.
Please log in or register to see it.

Please Log in or Create an account to join the conversation.

Moderators: Appeon Administrator