以下是一个示例代码:
#include <windows.h>
#include <appmodel.h>
int main() {
HANDLE hProcess = GetCurrentProcess();
UINT32 bufferLength = 0;
PACKAGE_INFO_REFERENCE packageInfoReference = NULL;
// 获取缓冲区大小
HRESULT result = GetPackageInfo(hProcess, PACKAGE_FILTER_HEAD, &bufferLength, NULL, &packageInfoReference);
if (result == ERROR_INSUFFICIENT_BUFFER) {
// 为缓冲区分配内存
BYTE* buffer = (BYTE*)malloc(bufferLength);
if (buffer != NULL) {
result = GetPackageInfo(hProcess, PACKAGE_FILTER_HEAD, &bufferLength, buffer, &packageInfoReference);
if (SUCCEEDED(result)) {
// 获取包信息
PACKAGE_INFO* packageInfo = (PACKAGE_INFO*)buffer;
wprintf(L"Current Package Info: Name=%s, Version=%s\n",
packageInfo->packageName,
packageInfo->packageVersion);
// 释放 Package Info 句柄
ClosePackageInfo(packageInfoReference);
} else {
// 处理错误
wprintf(L"Error: 0x%x\n", result);
}
// 释放分配的内存
free(buffer);
} else {
// 处理内存分配错误
wprintf(L"Memory allocation error\n");
}
} else {
// 处理错误
wprintf(L"Error: 0x%x\n", result);
}
return 0;
}
在此示例中,使用 GetPackageInfo 函数来获取 Package Info,然后提取所需的信息。请确保在调用任何 Win32 API 函数之前,您已经正确链接到相应的库。
转载请注明出处:http://www.zyzy.cn/article/detail/23831/Win32 API/Appmodel.h/GetCurrentPackageInfo