MSDN Documentation

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

MAKE​​​P​A​T​H

Constructs a full path from its components.

#define MAKE​​​P​A​T​H(​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.

SPLIT​​​P​A​T​H

Splits a full path into its individual components.

#define SPLIT​​​P​A​T​H(​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.

​OFF​S​E​T​O​F

Calculates the offset of a member within a structure.

#define OFFSET​​​O​F(​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.

C​O​N​T​A​I​N​E​R​O​F

Gets a pointer to the containing structure given a pointer to a member within that structure.

#define C​O​N​T​A​I​N​E​R​O​F(​ptr,​type,​member) \
((type *)((char *)(ptr) - OFFSET​​​O​F(​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.

​M​I​N

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.

​M​A​X

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.