查詢應用程式套件指令清單資訊 (C++)
瞭解如何使用 封裝 API 從 Windows 應用程式的應用程式套件指令清單取得資訊。
建立套件指令清單讀取器
若要建立套件指令清單讀取器,請呼叫 IAppxFactory::CreatePackageReader 來建立套件讀取器 。 第一個參數是封裝的輸入數據流(.appx檔案)。 第二個參數是輸出參數,可接收IAppxPackageReader指標的指標。 接下來,呼叫 IAppxPackageReader::GetManifest 以取得指令清單讀取器。 參數是輸出參數,可接收IAppxManifestReader指標的指標。
int wmain(
_In_ int argc,
_In_reads_(argc) wchar_t** argv)
{
HRESULT hr = S_OK;
if (argc != 2)
{
wprintf(L"Usage: DescribeAppx.exe inputFile\n");
wprintf(L" inputFile: Path to the app package to read\n");
return 2;
}
// Specify the appropriate COM threading model
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
IAppxPackageReader* packageReader = NULL;
IAppxManifestReader* manifestReader = NULL;
// Create a package reader using the file name given in command line
hr = GetPackageReader(argv[1], &packageReader);
// Get manifest reader for the package and read from the manifest
if (SUCCEEDED(hr))
{
hr = packageReader->GetManifest(&manifestReader);
}
if (SUCCEEDED(hr))
{
hr = ReadManifest(manifestReader);
}
}
}
//
// Creates an app package reader.
//
// Parameters:
// inputFileName
// Fully qualified name of the app package (.appx file) to be opened.
// reader
// On success, receives the created instance of IAppxPackageReader.
//
HRESULT GetPackageReader(
_In_ LPCWSTR inputFileName,
_Outptr_ IAppxPackageReader** reader)
{
HRESULT hr = S_OK;
IAppxFactory* appxFactory = NULL;
IStream* inputStream = NULL;
// Create a new factory
hr = CoCreateInstance(
__uuidof(AppxFactory),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IAppxFactory),
(LPVOID*)(&appxFactory));
// Create a stream over the input app package
if (SUCCEEDED(hr))
{
hr = SHCreateStreamOnFileEx(
inputFileName,
STGM_READ | STGM_SHARE_EXCLUSIVE,
0, // default file attributes
FALSE, // do not create new file
NULL, // no template
&inputStream);
}
// Create a new package reader using the factory. For
// simplicity, we don't verify the digital signature of the package.
if (SUCCEEDED(hr))
{
hr = appxFactory->CreatePackageReader(
inputStream,
reader);
}
// Clean up allocated resources
if (inputStream != NULL)
{
inputStream->Release();
inputStream = NULL;
}
if (appxFactory != NULL)
{
appxFactory->Release();
appxFactory = NULL;
}
return hr;
}
讀取套件身分識別資訊
套件識別是使用 指令清單中的 Identity 元素來指定。 使用 IAppxManifestReader::GetPackageId 取得 IAppxManifestPackageId 來讀取套件身分識別資訊,如下所示。
此程式代碼會使用IAppxManifestPackageId::GetPackageFullName 取得套件完整名稱、IAppxManifestPackageId::GetName 來取得套件名稱,而 IAppxManifestPackageId::GetVersion 則取得套件版本。
//
// Reads a subset of the manifest Identity element.
//
//
HRESULT ReadManifestPackageId(
_In_ IAppxManifestReader* manifestReader)
{
HRESULT hr = S_OK;
IAppxManifestPackageId* packageId = NULL;
// Get elements and attributes from the manifest reader
hr = manifestReader->GetPackageId(&packageId);
if (SUCCEEDED(hr))
{
LPWSTR packageFullName = NULL;
LPWSTR packageName = NULL;
UINT64 packageVersion = 0;
hr = packageId->GetPackageFullName(&packageFullName);
if (SUCCEEDED(hr))
{
hr = packageId->GetName(&packageName);
}
if (SUCCEEDED(hr))
{
hr = packageId->GetVersion(&packageVersion);
}
if (SUCCEEDED(hr))
{
wprintf(L"Package full name: %s\n", packageFullName);
wprintf(L"Package name: %s\n", packageName);
wprintf(L"Package version: ");
// Convert version number from 64-bit integer to dot-quad form
for (int bitPosition = 0x30; bitPosition >= 0; bitPosition -= 0x10)
{
UINT64 versionWord = (packageVersion >> bitPosition) & 0xFFFF;
wprintf(L"%llu.", versionWord);
}
wprintf(L"\n");
}
// Free all string buffers returned from the manifest API
CoTaskMemFree(packageFullName);
CoTaskMemFree(packageName);
}
// Clean up allocated resources
if (packageId != NULL)
{
packageId->Release();
packageId = NULL;
}
return hr;
}
讀取將套件描述給使用者的元數據
屬性是使用 指令清單中的 Properties 元素來指定。 使用IAppxManifestReader::GetProperties 取得IAppxManifestProperties來讀取此節點,如下所示。
此程式代碼會使用 IAppxManifestProperties::GetStringValue 來取得套件顯示名稱和套件描述。
//
// Reads a subset of the manifest Properties element.
//
//
HRESULT ReadManifestProperties(
_In_ IAppxManifestReader* manifestReader)
{
HRESULT hr = S_OK;
IAppxManifestProperties* properties = NULL;
// Get elements and attributes from the manifest reader
hr = manifestReader->GetProperties(&properties);
if (SUCCEEDED(hr))
{
LPWSTR displayName = NULL;
LPWSTR description = NULL;
hr = properties->GetStringValue(L"DisplayName", &displayName);
if (SUCCEEDED(hr))
{
hr = properties->GetStringValue(L"Description", &description);
}
if (SUCCEEDED(hr))
{
wprintf(L"Package display name: %s\n", displayName);
wprintf(L"Package description: %s\n", description);
}
// Free all string buffers returned from the manifest API
CoTaskMemFree(displayName);
CoTaskMemFree(description);
}
// Clean up allocated resources
if (properties != NULL)
{
properties->Release();
properties = NULL;
}
return hr;
}
閱讀套件中包含的應用程式相關元數據
應用程式是使用 指令清單中的Applications 元素來指定。 使用 IAppxManifestReader::GetApplications 取得 IAppxManifestApplicationsEnumerator 來讀取此節點。 使用 IAppxManifestApplicationsEnumerator::GetCurrent 和 IAppxManifestApplicationsEnumerator::MoveNext 取得套件中每個應用程式的 IAppxManifestApplication。
此程式代碼會使用 IAppxManifestApplication::GetStringValue 來取得每個應用程式的顯示名稱。
//
// Reads a subset of the manifest Applications element.
//
//
HRESULT ReadManifestApplications(
_In_ IAppxManifestReader* manifestReader)
{
HRESULT hr = S_OK;
BOOL hasCurrent = FALSE;
UINT32 applicationsCount = 0;
IAppxManifestApplicationsEnumerator* applications = NULL;
// Get elements and attributes from the manifest reader
hr = manifestReader->GetApplications(&applications);
if (SUCCEEDED(hr))
{
hr = applications->GetHasCurrent(&hasCurrent);
while (SUCCEEDED(hr) && hasCurrent)
{
IAppxManifestApplication* application = NULL;
LPWSTR applicationName = NULL;
hr = applications->GetCurrent(&application);
if (SUCCEEDED(hr))
{
application->GetStringValue(L"DisplayName", &applicationName);
}
if (SUCCEEDED(hr))
{
applicationsCount++;
wprintf(L"App #%u: %s\n", applicationsCount, applicationName);
}
if (SUCCEEDED(hr))
{
hr = applications->MoveNext(&hasCurrent);
}
if (application != NULL)
{
application->Release();
application = NULL;
}
CoTaskMemFree(applicationName);
}
wprintf(L"Count of apps in the package: %u\n", applicationsCount);
}
// Clean up allocated resources
if (applications != NULL)
{
applications->Release();
applications = NULL;
}
return hr;
}
清除套件指令清單讀取器
從 wmain
函式傳回之前,請先呼叫 Release 方法來清除套件指令清單讀取器,並呼叫 CoUninitialize 函式 。
// Clean up allocated resources
if (manifestReader != NULL)
{
manifestReader->Release();
manifestReader = NULL;
}
if (packageReader != NULL)
{
packageReader->Release();
packageReader = NULL;
}
CoUninitialize();
相關主題