Win32 API Macros
This section provides documentation for macros used within the Win32 API. Macros are powerful tools that can simplify code, improve readability, and offer performance benefits.
Commonly Used Macros
MAKEPATH
Constructs a full path from its components.
#define MAKEPATH(str,drive,dir,name,ext) \
(void)((str)[0] = (drive)[0], \
(str)[1] = ':', \
(void)((str)[2] = ('\\' == (dir)[0] || '/' == (dir)[0]) ? (dir)[0] : '\\'), \
(void)strcpy((str) + 3, (dir) + 1), \
(void)strcat((str), (name)), \
(void)strcat((str), (ext)))
Parameters:
str
: Pointer to the buffer that will receive the constructed path.drive
: Drive letter (e.g., "C").dir
: Directory path (e.g., "\Users\Public").name
: Filename without extension.ext
: File extension (e.g., ".txt").
Return Value: None. The constructed path is placed in the str
buffer.
SPLITPATH
Splits a full path into its individual components.
#define SPLITPATH(path,drive,dir,name,ext) \
(void)((drive)[0] = (path)[0], \
(drive)[1] = ':', \
(drive)[2] = '\0', \
(path) += 2, \
(void)strncpy((dir), (path), _MAX_DIR), \
(void)strncat((dir), "\\", _MAX_DIR - strlen((dir)) - 1), \
(void)strncpy((name), (path), _MAX_PATH), \
(void)strrchr((name), '.'), \
(void)strncpy((ext), (path), _MAX_PATH), \
(void)strrchr((ext), '.'), \
(void)strtok(NULL, "\\/") )
Parameters:
path
: Pointer to the full path string.drive
: Buffer to receive the drive letter.dir
: Buffer to receive the directory path.name
: Buffer to receive the filename without extension.ext
: Buffer to receive the file extension.
Return Value: None. The components are placed in the provided buffers.
OFFSETOF
Calculates the offset of a member within a structure.
#define OFFSETOF(type,member) \
((size_t)(char *)&((type *)0)->member - (char *)(type *)0)
Parameters:
type
: The structure type.member
: The member within the structure.
Return Value: The byte offset of the member from the beginning of the structure.
CONTAINEROF
Gets a pointer to the containing structure given a pointer to a member within that structure.
#define CONTAINEROF(ptr,type,member) \
((type *)((char *)(ptr) - OFFSETOF(type,member)))
Parameters:
ptr
: Pointer to the member.type
: The type of the containing structure.member
: The name of the member.
Return Value: A pointer to the containing structure.
MIN
Returns the minimum of two values.
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
Parameters:
a
: The first value.b
: The second value.
Return Value: The smaller of the two values.
MAX
Returns the maximum of two values.
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
Parameters:
a
: The first value.b
: The second value.
Return Value: The larger of the two values.