GetApplicationUserModelIdFromToken, fonction (appmodel.h)

Obtient l’ID de modèle utilisateur de l’application pour le jeton spécifié.

Syntaxe

LONG GetApplicationUserModelIdFromToken(
  [in]      HANDLE token,
  [in, out] UINT32 *applicationUserModelIdLength,
  [out]     PWSTR  applicationUserModelId
);

Paramètres

[in] token

Jeton qui contient l’identité de l’application. Ce handle doit avoir le droit d’accès PROCESS_QUERY_LIMITED_INFORMATION . Pour plus d’informations, consultez Droits d’accès et de sécurité des processus.

[in, out] applicationUserModelIdLength

Lors de l’entrée, la taille de la mémoire tampon applicationUserModelId , en caractères larges. En cas de réussite, la taille de la mémoire tampon utilisée, y compris la marque de fin Null.

[out] applicationUserModelId

Pointeur vers une mémoire tampon qui reçoit l’ID de modèle utilisateur de l’application.

Valeur retournée

Si la fonction réussit, elle retourne ERROR_SUCCESS. Sinon, la fonction retourne un code d’erreur. Les codes d’erreur possibles sont les suivants.

Code de retour Description
APPMODEL_ERROR_NO_APPLICATION
Le jeton n’a pas d’identité d’application.
ERROR_INSUFFICIENT_BUFFER
La mémoire tampon n’est pas assez grande pour contenir les données. La taille requise est spécifiée par applicationUserModelIdLength.

Remarques

Pour plus d’informations sur les limites de taille de chaîne, consultez Constantes d’identité.

Exemples

/***************************************************
*                                                  *
*   Copyright (C) Microsoft. All rights reserved.  *
*                                                  *
***************************************************/

#define _UNICODE 1
#define UNICODE 1

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

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

int ShowUsage()
{
    wprintf(L"Usage: GetApplicationUserModelIdFromToken <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
            {
                HANDLE token;
                if (!OpenProcessToken(process, TOKEN_QUERY, &token))
                    wprintf(L"Error %d in OpenProcessToken (pid=%u)\n", GetLastError(), pid);
                else
                {
                    ShowProcessApplicationUserModelId(pid, token);
                    CloseHandle(token);
                }
                CloseHandle(process);
            }
        }
    }
    return 0;
}

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

    UINT32 length = 0;
    LONG rc = GetApplicationUserModelIdFromToken(token, &length, NULL);
    if (rc != ERROR_INSUFFICIENT_BUFFER)
    {
        if (rc == APPMODEL_ERROR_NO_PACKAGE)
            wprintf(L"Token has no package identity\n");
        else
            wprintf(L"Error %d in GetApplicationUserModelIdFromToken\n", rc);
        return;
    }

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

    rc = GetApplicationUserModelIdFromToken(token, &length, applicationUserModelId);
    if (rc != ERROR_SUCCESS)
        wprintf(L"Error %d retrieving ApplicationUserModelId\n", rc);
    else
        wprintf(L"%s\n", applicationUserModelId);

    free(applicationUserModelId);
}

Configuration requise

Condition requise Valeur
Plateforme cible Windows
En-tête appmodel.h
Bibliothèque Kernel32.lib
DLL Kernel32.dll