Skip to content

Proposal: Refactor ITEMIDLIST to a formal Structure and improve IShellLink type safety #924

@junkmd

Description

@junkmd

Description / Problem

Currently, the ITEMIDLIST definition in comtypes.shelllink is treated as a simple c_int.

# fake these...
ITEMIDLIST = c_int
LPITEMIDLIST = LPCITEMIDLIST = POINTER(ITEMIDLIST)

While this works as an opaque handle in some cases, it presents several issues for robust development:

  1. Lack of Introspection: Developers cannot access internal fields of ITEMIDLIST and SHITEMID without manual casting, which is error-prone.
  2. Incomplete Type Hinting: Methods such as GetIDList and SetIDList use hints.Incomplete, providing no useful information for IDEs or static analysis tools (like mypy).
  3. Potential for Memory Errors: Without a formal structure, managing the memory of PIDLs is less intuitive.

Proposed Changes

  • Formal Type Definitions:

    • Define SHITEMID and ITEMIDLIST as proper ctypes.Structure classes.
    • Ensure LPITEMIDLIST and LPCITEMIDLIST correctly pointer to these structures.
  • Interface Enhancements:

    • Update IShellLinkA and IShellLinkW vtables to use these formal types.
    • Replace hints.Incomplete with _Pointer[ITEMIDLIST] and explicit HRESULT returns.
  • Added Test Coverage:

    • Introduce unit tests in test_shelllink.py to verify round-tripping (Set/Get) of ITEMIDLIST.

Benefits

  • Better autocompletion and type checking in modern IDEs.
  • The library will more accurately reflect the underlying Windows API structure.
  • Formal structures reduce the likelihood of pointer arithmetic errors or incorrect memory size assumptions.

Backward Compatibility

This change is expected to be highly backward compatible as LPITEMIDLIST remains a pointer.
Existing code treating it as a handle should continue to function without modification.

Metadata

Metadata

Assignees

No one assigned

    Labels

    debtenhancementNew feature or requesttestsenhance or fix tests

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions