Expand Minimize

RoGetMetaDataFile function

Locates and retrieves the metadata file that describes the Application Binary Interface (ABI) for the specified typename.

Syntax


HRESULT WINAPI RoGetMetaDataFile(
  _In_       const HSTRING name,
  _In_opt_   IMetaDataDispenserEx *metaDataDispenser,
  _Out_opt_  HSTRING *metaDataFilePath,
  _Out_opt_  IMetaDataImport2 **metaDataImport,
  _Out_opt_  mdTypeDef *typeDefToken
);

Parameters

name [in]

Type: const HSTRING

The name to resolve, either a typename or a namespace. The name input string must be non-empty and must not contain embedded NUL characters. If the name is a dot-separated string, then the substring to the left of the last dot and the substring to the right of the last dot must be non-empty.

metaDataDispenser [in, optional]

Type: IMetaDataDispenserEx*

A metadata dispenser that the caller can optionally pass in for the RoGetMetaDataFile function to be able to open the metadata files through the provided IMetaDataDispenserEx::OpenScope method. If the metadata dispenser parameter is set to nullptr, the function creates an internal instance of the refactored metadata reader (RoMetadata.dll) and uses its IMetaDataDispenserEx::OpenScope method.

metaDataFilePath [out, optional]

Type: HSTRING*

The absolute path of the metadata (.winmd) file that describes the ABI, unless set to nullptr. The caller is responsible for freeing the HSTRING by calling the WindowsDeleteString method.

metaDataImport [out, optional]

Type: IMetaDataImport2**

A pointer to the metadata file reader object. If the caller passes in a nullptr , the function releases the IMetaDataImport2 reference, otherwise the caller must release the reference. The value is set to nullptr on failure.

typeDefToken [out, optional]

Type: mdTypeDef*

If the name input string is resolved successfully as a typename, this parameter is set to the token of the typename.

On failure, this parameter is set to mdTypeDefNil.

Return value

Type: HRESULT

This function can return one of these values.

Return codeDescription
S_OK

Resolution was successful, which means that the input string represents a type defined in a .winmd file.

E_INVALIDARG

At least one of the following properties of the input name string does not hold:

  • Not null, not empty
  • Does not contain embedded null characters.
  • If a dot-separated string, the substring to the left of the last dot and the substring to the right of the last dot must be non-empty.
RO_E_METADATA_NAME_NOT_FOUND

The input string is not a type defined in any examined .winmd file.

RO_E_METADATA_NAME_IS_NAMESPACE

The input string is an existing namespace rather than a typename.

HR_RESULT_FROM_WIN32(ERROR_NO_PACKAGE)

The function was called from a process that is not in a Windows Store app.

 

Remarks

The caller can optionally pass-in a metadata dispenser for the RoGetMetaDataFile function to open the metadata files through the IMetaDataDispenserEx::OpenScope method.

If the metadata dispenser parameter is set to nullptr, the function creates an internal instance of the refactored metadata reader and uses that reader’s IMetaDataDispenserEx::OpenScope method.

The RoGetMetaDataFile function is guaranteed to be thread-safe if you pass-in nullptr to the metadata dispenser parameter, as the function creates an internal read-only metadata reader. This guarantee also holds if you pass in the read-only metadata reader, like RoMetadata to the function.

All three output parameters are optional and none of them needs to be specified. Calling the RoGetMetaDataFile function with nullptr for all output parameters is equivalent to asking whether the input typename or namespace is defined.

The metadata reader object reference and the TypeDef token parameters paired, so both must be set together or be set to nullptr.

There are three possible type resolution scenarios:

Scenario #1

Typename input string is a type defined in a WinMD file.

  • Return Value

    S_OK

  • Metadata file path output parameter

    This is an optional output parameter. If not set to nullptr by caller, it holds the absolute path of the .winmd file that describes the given type's ABI. The caller is responsible for freeing the HSTRING by calling WindowsDeleteString.

  • Reference to the metadata reader object output parameter

    This is an optional output parameter. If not nullptr, it holds a reference to the metadata reader object (IMetaDataImport2) and the caller is responsible for releasing it. If the caller passes nullptr for this parameter, it means that the caller does not want the metadata reader object, so the function releases the internal reference.

  • Typedef token output parameter

    This is an optional output parameter. If not set to nullptr by caller, it is set to the token of the type’s typedef entry. Language projections can use this token to call IMetaDataImport::GetTypeDefProps to get metadata about the type.

Scenario #2

Typename input string is actually an existing namespace rather than a typename.
  • Return value

    RO_E_METADATA_NAME_IS_NAMESPACE

  • Metadata file path output parameter

    This is an optional output parameter. If not set to nullptr by the caller, it is set to nullptr.

  • Reference to the metadata reader object output parameter

    This is an optional output parameter. If not set to nullptr by caller, it is set to nullptr.

  • Typedef token output parameter

    This is an optional output parameter. If not set to nullptr by caller, it will is to mdTypeDefNil.

Scenario #3

Input string is not a type defined in any examined WinMD file
  • Return value

    RO_E_METADATA_NAME_NOT_FOUND

  • Metadata file path output parameter

    This is an optional output parameter. If not set to nullptr by caller, it is set to nullptr

  • Reference to the metadata reader object output parameter

    This is an optional output parameter. If not set to nullptr by caller, it is set to nullptr

  • Typedef token output parameter

    This is an optional output parameter. If not set to nullptr by caller, it is set to mdTypeDefNil.

 

The RoGetMetaDataFile function resolves an interfacegroup, because the interfacegroup also is a namespace-qualified typename. The IInspectable::GetRuntimeClassName method returns the string in dot-separated string format for use by RoGetMetaDataFile.

Resolving 3rd-party types from a process that's not in a Windows Store app is not supported. In this case, the function returns error HRESULT_FROM_WIN32(ERROR_NO_PACKAGE) and sets output parameters to nullptr. But Windows types are resolved in a process that's not in a Windows Store app.

Windows Phone 8.1: This API is supported.

Examples

The following C++ example shows how to use the RoGetMetaDataFile function to find the metadata file for a specified type name.


#include <windows.h>
#include <stdio.h>
#include <WinRTString.h>
#include <TypeResolution.h>
#include <atlbase.h>

HRESULT PrintMetaDataFilePathForTypeName(PCWSTR pszTypename);

int ShowUsage()
{
    wprintf(L"Usage: RoGetMetaDataFileSample TypeName\n");
    return -1;
}

int __cdecl wmain(int argc, WCHAR **argv)
{
    if (argc != 2)
    {
        return ShowUsage();
    }

    HRESULT hr = PrintMetaDataFilePathForTypeName(argv[1]);

    if (SUCCEEDED(hr))
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

HRESULT PrintMetaDataFilePathForTypeName(PCWSTR pszTypename)
{
    HRESULT hr;
    HSTRING hstrTypeName = nullptr;
    HSTRING hstrMetaDataFilePath = nullptr;
    CComPtr<IMetaDataImport2> spMetaDataImport;
    mdTypeDef typeDef;

    hr = WindowsCreateString(
        pszTypename,
        static_cast<UINT32>(wcslen(pszTypename)),
        &hstrTypeName);

    if (SUCCEEDED(hr))
    {
        hr = RoGetMetaDataFile(
            hstrTypeName,
            nullptr,
            &hstrMetaDataFilePath,
            &spMetaDataImport,
            &typeDef);
    }

    if (SUCCEEDED(hr))
    {
        wprintf(L"Type %s was found in %s\n", pszTypename,  WindowsGetStringRawBuffer(hstrMetaDataFilePath, nullptr));
    }
    else if (hr == RO_E_METADATA_NAME_NOT_FOUND)
    {
        wprintf(L"Type %s was not found!\n", pszTypename);
    }
    else
    {
        wprintf(L"Error %x occured while trying to resolve %s!\n", hr, pszTypename);
    }

    // Clean up resources.
    if (hstrTypeName != nullptr)
    {
        WindowsDeleteString(hstrTypeName);
    }

    if (hstrMetaDataFilePath != nullptr)
    {
        WindowsDeleteString(hstrMetaDataFilePath);
    }

    return hr;
}


Requirements

Minimum supported client

Windows 8 [desktop apps | Windows Store apps]

Minimum supported server

Windows Server 2012 [desktop apps | Windows Store apps]

Header

Rometadataresolution.h

Library

WinTypes.lib

DLL

WinTypes.dll

 

 

Community Additions

ADD
Show:
© 2014 Microsoft