IPortableDeviceManager::GetDevices output guarantees after a failing HRESULT

Vancsisin László 0 Reputation points
2026-11-26T22:31:54.37+00:00

I am implementing read-only WPD enumeration on Windows 11 on Arm. This question concerns the native COM contract for IPortableDeviceManager::GetDevices with a caller-managed pointer array.

For the fill call, the caller passes a zero-initialized LPWSTR array and a valid DWORD* whose initial value is the array capacity. The documentation describes a count-only call with a null array and a valid pointer to a DWORD initialized to zero. It also documents S_FALSE for an insufficient buffer with some entries written. S_FALSE is a success HRESULT and falls outside the FAILED(hr) cases below.

  1. E_POINTER. Which argument conditions can produce this result? For each condition, what guarantees apply to any output storage whose address was valid?
  2. Output count and array entries. If a fill call with valid arguments returns a failing HRESULT, can the caller rely on *pcPnPDeviceIDs? Is it unchanged, reset, or a count of initialized entries? Which array entries, if any, may the caller inspect?
  3. Ownership and cleanup. If GetDevices allocates strings before returning a failing HRESULT, who frees them? If any ownership transfers to the caller, how should it identify the entries to pass to CoTaskMemFree? If the count is unusable, is it safe to inspect non-null entries within the original capacity?
  4. Scope of the COM rule. Do the general COM failure rules for [in,out] parameters apply to each individual LPWSTR entry in this caller-allocated array? Are there any HRESULT-specific exceptions?

Could you confirm the supported cleanup procedure and point to any GetDevices-specific guarantee?

GetDevices documentation · COM Memory Management Rules

Windows development | Windows API - Win32
0 comments No comments

1 answer

Sort by: Most helpful
  1. Zack Nguyen (WICLOUD CORPORATION) 485 Reputation points Microsoft External Staff Moderator
    2026-11-27T02:06:32.3066667+00:00

    Hi @Vancsisin László ,

    Thanks for the detailed question. Based on the available Microsoft documentation, the following information may help clarify the behavior of IPortableDeviceManager::GetDevices. 

    The IPortableDeviceManager::GetDevices documentation does not explicitly describe its behavior after a failing HRESULT. The general COM Memory Management Rules provide the closest documented guidance. 

    Regarding E_POINTER, the documentation states that at least one required argument is NULL. Since the count-only call allows a NULL array when *pcPnPDeviceIDs = 0, a NULL pcPnPDeviceIDs is a clearly documented invalid argument. Other conditions and their effects on output storage are not explicitly documented. 

    For the output count and array entries, *pcPnPDeviceIDs is defined as the number of entries written, but its value after a failure is not specified. According to the COM Memory Management Rules, an in/out parameter should remain unchanged or be set to a value requiring no cleanup when a call fails. Therefore, neither the returned count nor the array entries should be treated as valid results after FAILED(hr). 

    Regarding ownership and cleanup, the API allocates each string on success, and the caller is responsible for freeing it using CoTaskMemFree. The recommended procedure is to zero-initialize the array before calling GetDevices. On S_OK or S_FALSE, the caller should free the returned strings based on the reported count. On FAILED(hr), the general COM rules indicate that no caller cleanup should be required, and the output count and array entries should not be relied upon. Scanning the original array capacity for non-NULL entries after a failure is not covered by a documented guarantee. 

    The general COM rules also require output pointers to be set to NULL on failure, including pointers within caller-allocated structures. Individual LPWSTR array entries are similar, but the documentation does not explicitly address this array scenario or identify any HRESULT-specific exceptions. 

    Overall, the public documentation does not provide GetDevices-specific guarantees about output values or ownership after a failing HRESULT. Since this forum primarily provides guidance based on publicly available documentation and does not have access to internal implementation details or engineering-level diagnostics, these behaviors cannot be authoritatively confirmed here. For further clarification and confirmation of the supported contract, contacting Windows Developer Support is recommended, as they can help route the question to the appropriate engineering team. 


    I hope this helps clarifying your requirements and answering your questions. If you find my response helpful, I would greatly appreciate it if you could follow the guide here so others experiencing similar behavior can benefit from it as well. 

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.