On Wed Oct 1 14:51:55 2025 +0000, Rémi Bernon wrote:
Hmm... What about splitting this into separate helpers like this:
static HRESULT devpropcompkeys_init( DEVPROPCOMPKEY **keys, ULONG *keys_len, const DEVPROPCOMPKEY *values, ULONG len ) { if (!(*keys = calloc( len, sizeof( **keys ) ))) return E_OUTOFMEMORY; memcpy( *keys, values, len * sizeof( **keys ) ); *keys_len = len; return S_OK; } static HRESULT count_iterable( IIterable_HSTRING *iterable, UINT *ret ) { IIterator_HSTRING *iter; UINT count = 0; boolean valid; HRESULT hr; if (FAILED(hr = IIterable_HSTRING_First( iterable, &iter ))) return hr; for (hr = IIterator_HSTRING_get_HasCurrent( iter, &valid ); SUCCEEDED(hr) && valid; hr = IIterator_HSTRING_MoveNext( iter, &valid )) count++; IIterator_HSTRING_Release( iter ); *ret = count; return hr; } static HRESULT devpropcompkeys_append_names( DEVPROPCOMPKEY **keys, ULONG *keys_len, IIterable_HSTRING *names_iterable ) { IIterator_HSTRING *names; UINT len = *keys_len; DEVPROPCOMPKEY *tmp; boolean valid; HRESULT hr; UINT count; if (FAILED(hr = count_iterable( names_iterable, &count ))) return hr; if (!(tmp = realloc( *keys, (len + count) * sizeof( **keys ) ))) return E_OUTOFMEMORY; *keys = tmp; if (FAILED(hr = IIterable_HSTRING_First( names_iterable, &names ))) return hr; for (hr = IIterator_HSTRING_get_HasCurrent( names, &valid ); SUCCEEDED(hr) && valid; hr = IIterator_HSTRING_MoveNext( names, &valid )) { DEVPROPCOMPKEY key = {0}; const WCHAR *buf; HSTRING name; if (FAILED(hr = IIterator_HSTRING_get_Current( names, &name ))) break; buf = WindowsGetStringRawBuffer( name, NULL ); if (buf[0] == '{') hr = PSPropertyKeyFromString( buf, (PROPERTYKEY *)&key.Key ); else hr = PSGetPropertyKeyFromName( buf, (PROPERTYKEY *)&key.Key ); WindowsDeleteString( name ); if (FAILED(hr)) break; if (devpropcompkey_buf_find_devpropkey( tmp, len, key.Key )) continue; (*keys)[len++] = key; } IIterator_HSTRING_Release( names ); *keys_len = len; return hr; }Then `devpropcompkeys_init` can be used to initialize from the static default properties, but `(keys, keys_len) == (NULL, 0)` would also be a valid initial value and can be passed to `devpropcompkeys_append_names`. The `devpropcompkey_buf_find_devpropkey` call might or not be removed, depending on the other discussion above about duplicate values.
Yes, that's cleaner. Thanks.