GetPackagePath function

Gets the path for the specified package.

Syntax


LONG WINAPI GetPackagePath(
  _In_        const PACKAGE_ID *packageId,
  _Reserved_  const UINT32 reserved,
  _Inout_     UINT32 *pathLength,
  _Out_opt_   PWSTR path
);

Parameters

packageId [in]

Type: const PACKAGE_ID*

The package identifier.

reserved

Type: const UINT32

Reserved, do not use.

pathLength [in, out]

Type: UINT32*

On input, the size of the path buffer, in characters. On output, the size of the package path returned, in characters, including the null-terminator.

path [out, optional]

Type: PWSTR

The package path.

Return value

Type: LONG

If the function succeeds it returns ERROR_SUCCESS. Otherwise, the function returns an error code. The possible error codes include the following.

Return codeDescription
ERROR_INSUFFICIENT_BUFFER

The buffer specified by path is not large enough to hold the data. The required size is specified by pathLength.

 

Examples


#define _UNICODE 1
#define UNICODE 1

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

int ShowUsage();
bool ParseArchitecture(__in PCWSTR architectureString, __out UINT32 * architecture);
bool ParseVersion(__in PCWSTR versionString, __out PACKAGE_VERSION * version);

int ShowUsage()
{
    wprintf(L"Usage: GetPackagePath <name> <version> <arch> <resourceid> [*]<publisher>\n"
            L"where\n"
            L"  name          Package name\n"
            L"  version       Package version (e.g. 1.2.3.4)\n"
            L"  architecture  Package architecture <neutral|arm|x64|x86>\n"
            L"  resourceid    Package resource ID\n"
            L"  publisher     Package publisher (or publisher ID if prefixed with *)\n");
    return 1;
}

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

    PACKAGE_ID packageId;
    ZeroMemory(&packageId, sizeof(packageId));
    packageId.name = argv[1];
    if (!ParseVersion(argv[2], &packageId.version))
        return 2;
    if (!ParseArchitecture(argv[3], &packageId.processorArchitecture))
        return 3;
    packageId.resourceId = argv[4];
    if (argv[5][0] == L'*')
        packageId.publisherId = argv[5] + 1;
    else
        packageId.publisher = argv[5];

    UINT32 length = 0;
    LONG rc = GetPackagePath(&packageId, 0, &length, NULL);
    if (rc != ERROR_INSUFFICIENT_BUFFER)
    {
        wprintf(L"GetPackagePath failed with error %d\n", rc);
        return 4;
    }

    PWSTR path = (PWSTR) malloc((length + 1) * sizeof(WCHAR));
    if (path == NULL)
    {
        wprintf(L"Memory allocation failed\n");
        return 5;
    }

    rc = GetPackagePath(&packageId, 0, &length, path);
    if (rc != ERROR_SUCCESS)
        wprintf(L"GetPackagePath failed with error %d\n", rc);
    else
        wprintf(L"Path = %s\n", path);

    free(path);

    return rc == ERROR_SUCCESS ? 0 : 6;
}

bool ParseArchitecture(__in PCWSTR architectureString, __out UINT32 * architecture)
{
    if (_wcsicmp(architectureString, L"neutral") == 0)
        *architecture = PROCESSOR_ARCHITECTURE_NEUTRAL;
    else if (_wcsicmp(architectureString, L"x86") == 0)
        *architecture = PROCESSOR_ARCHITECTURE_INTEL;
    else if (_wcsicmp(architectureString, L"x64") == 0)
        *architecture = PROCESSOR_ARCHITECTURE_AMD64;
    else if (_wcsicmp(architectureString, L"arm") == 0)
        *architecture = PROCESSOR_ARCHITECTURE_ARM;
    else
    {
        wprintf(L"Architecture not valid\n");
        return false;
    }
    return true;
}

bool ParseVersion(__in PCWSTR versionString, __out PACKAGE_VERSION * version)
{
    PWSTR s = (PWSTR) versionString;

    ULONG n = wcstoul(s, &s, 10);
    if (((n == 0) || (n > 65535)) && (errno == ERANGE)) {
        wprintf(L"Major version not valid\n");
        return false;
    }
    version->Major = (USHORT) n;

    if (*s != L'.')
    {
        wprintf(L"Version not valid\n");
        return false;
    }

    n = wcstoul(++s, &s, 10);
    if (((n == 0) || (n > 65535)) && (errno == ERANGE)) {
        wprintf(L"Minor version not valid\n");
        return false;
    }
    version->Minor = (USHORT) n;

    if (*s != L'.')
    {
        wprintf(L"Version not valid\n");
        return false;
    }

    n = wcstoul(++s, &s, 10);
    if (((n == 0) || (n > 65535)) && (errno == ERANGE)) {
        wprintf(L"Revision version not valid\n");
        return false;
    }
    version->Revision = (USHORT) n;

    if (*s != L'.')
    {
        wprintf(L"Version not valid\n");
        return false;
    }

    n = wcstoul(++s, &s, 10);
    if (((n == 0) || (n > 65535)) && (errno == ERANGE)) {
        wprintf(L"Build version not build\n");
        return false;
    }
    version->Build = (USHORT) n;

    return true;
}

Requirements

Minimum supported client

Windows 8 [desktop apps only]

Minimum supported server

Windows Server 2012 [desktop apps only]

Header

AppModel.h

Library

Kernel32.lib

DLL

Kernel32.dll

See also

GetPackageInfo

 

 

Show:
© 2014 Microsoft