Go to Home Page

Creates an object that can query registry values


HRESULT WINAPI QuerySourceCreateFromKeyEx (
    HKEY hKey,
    LPCWSTR pwszSubKey,
    BOOL shouldCreate,
    ACCESS_MASK amDesired,
    REFIID riid,
    LPVOID* ppInterface


A handle to an open registry key or a predefined key
The name of a subkey to open or create
TRUE if the function creates the registry key (uses RegCreateKeyEx) or opens it (uses RegOpenKeyEx)
Desired access to registry key. This value has KEY_READ added to it
The id of the interface pointer to return
A pointer to a pointer that receives the returned interface

Return Value

S_OK on success, standard COM error code otherwise


riid can be either IID_IQuerySource or IID_IObjectWithRegistryKey, these interfaces are defined as follows:

IObjectWithRegistryKey : public IUnknown
   // sets the source registry key
   // can only be set if the current key is zero otherwise returns E_UNEXPECTED
   STDMETHODIMP SetKey(HKEY hKey) = 0;

   // returns a copy of the source key with the specified access
   STDMETHODIMP GetKey(ACCESS_MASK accessDesired, HKEY* phKey) = 0;

// for all of these methods, NULL for the pwszSubkey parameter means the current key
IQuerySource : public IUnknown
   // Creates an enumerator for values in this key (pThis->hKey)
   STDMETHODIMP EnumValues(IEnumString** ppEnum) = 0;

   // reads the string at subkey\value and creates a copy in *ppString. Must be freed with
   // CoTaskMemFree(). If the value isn't as string 
   STDMETHODIMP QueryValueString(LPCWSTR pwszSubKey, LPCWSTR pwszValue, LPWSTR* ppString) = 0;
   // as above but for REG_DWORD values
   STDMETHODIMP QueryValueDword(LPCWSTR pwszSubKey, LPCWSTR pwszValue, DWORD* pData) = 0;

   // as above, but for GUIDs in REG_SZ values
   STDMETHODIMP QueryValueGuid(LPCWSTR pwszSubkey, LPCWSTR pwszValue, GUID* pGuid) = 0;

   // returns S_OK if it does, error otherwise
   STDMETHODIMP QueryValueExists(LPCWSTR pwszSubkey, LPCWSTR pwszValue) = 0;

   // FLAGGED_BYTE_BLOB is defined in the SDK. The fFlags member is the registry type of the data. The returned pointer must be freed with CoTaskMemFree
   STDMETHODIMP QueryValueDirect(LPCWSTR pwszSubkey, LPCWSTR pwszValue, FLAGGED_BYTE_BLOB** ppBlob) = 0;

   // returns an enumerator that enumerates the subkeys of pThis->hKey
   STDMETHODIMP EnumSources(IEnumString** pppEnum) = 0;

   // calls QuerySourceCreateFromKey(pThis->hKey, pwszSubkey, FALSE, IID_IObjectWithRegistryKey, ppInterface)
   // to create an interface pointer for pwszSubKey.
   // This can be queried for a IQuerySource interface
   STDMETHODIMP OpenSource(LPCWSTR pwszSubkey, IObjectWithRegistryKey* ppInterface) = 0;

This function is also known as _QuerySourceCreateFromKeyEx@24.