getApplicationUserModelId 函数 (appmodel.h)

获取指定进程的 应用程序用户模型 ID

语法

LONG GetApplicationUserModelId(
  [in]      HANDLE hProcess,
  [in, out] UINT32 *applicationUserModelIdLength,
  [out]     PWSTR  applicationUserModelId
);

参数

[in] hProcess

进程的句柄。 此句柄必须具有 PROCESS_QUERY_LIMITED_INFORMATION 访问权限。 有关详细信息,请参阅 处理安全和访问权限

[in, out] applicationUserModelIdLength

输入时, applicationUserModelId 缓冲区的大小(以宽字符为单位)。 成功时,使用的缓冲区大小,包括 null 终止符。

[out] applicationUserModelId

指向接收应用程序用户模型 ID 的缓冲区的指针。

返回值

如果该函数成功,则返回 ERROR_SUCCESS。 否则,该函数将返回错误代码。 可能的错误代码包括以下内容。

返回代码 说明
APPMODEL_ERROR_NO_APPLICATION
进程没有应用程序标识。
ERROR_INSUFFICIENT_BUFFER
缓冲区不够大,无法保存数据。 所需大小由 applicationUserModelIdLength 指定。

注解

有关字符串大小限制的信息,请参阅 标识常量

示例

#define _UNICODE 1
#define UNICODE 1

#include <Windows.h>
#include <appmodel.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

int ShowUsage();
void ShowProcessApplicationUserModelId(__in const UINT32 pid, __in HANDLE process);

int ShowUsage()
{
    wprintf(L"Usage: GetApplicationUserModelId <pid> [<pid>...]\n");
    return 1;
}

int __cdecl wmain(__in int argc, __in_ecount(argc) WCHAR * argv[])
{
    if (argc <= 1)
        return ShowUsage();

    for (int i=1; i<argc; ++i)
    {
        UINT32 pid = wcstoul(argv[i], NULL, 10);
        if (pid > 0)
        {
            HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
            if (process == NULL)
                wprintf(L"Error %d in OpenProcess (pid=%u)\n", GetLastError(), pid);
            else
            {
                ShowProcessApplicationUserModelId(pid, process);
                CloseHandle(process);
            }
        }
    }
    return 0;
}

void ShowProcessApplicationUserModelId(__in const UINT32 pid, __in HANDLE process)
{
    wprintf(L"Process %u (handle=%p)\n", pid, process);

    UINT32 length = 0;
    LONG rc = GetApplicationUserModelId(process, &length, NULL);
    if (rc != ERROR_INSUFFICIENT_BUFFER)
    {
        if (rc == APPMODEL_ERROR_NO_APPLICATION)
            wprintf(L"Desktop application\n");
        else
            wprintf(L"Error %d in GetApplicationUserModelId\n", rc);
        return;
    }

    PWSTR fullName = (PWSTR) malloc(length * sizeof(*fullName));
    if (fullName == NULL)
    {
        wprintf(L"Error allocating memory\n");
        return;
    }

    rc = GetApplicationUserModelId(process, &length, fullName);
    if (rc != ERROR_SUCCESS)
        wprintf(L"Error %d retrieving ApplicationUserModelId\n", rc);
    else
        wprintf(L"%s\n", fullName);

    free(fullName);
}

要求

要求
目标平台 Windows
标头 appmodel.h
Library Kernel32.lib
DLL Kernel32.dll

另请参阅

GetCurrentApplicationUserModelId

GetPackageFamilyName

GetPackageId

GetPackageInfo

GetPackagePath