From 65230d8eac69504585b7f62bfc8a0e3726372f64 Mon Sep 17 00:00:00 2001 From: dahall Date: Tue, 5 Oct 2021 15:43:16 -0600 Subject: [PATCH] Updated and fixed attributes for appmodel.h functions, adding GetPackageFullName function. --- PInvoke/Kernel32/AppModel.cs | 110 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 88 insertions(+), 22 deletions(-) diff --git a/PInvoke/Kernel32/AppModel.cs b/PInvoke/Kernel32/AppModel.cs index 6ab4db36..50dd4485 100644 --- a/PInvoke/Kernel32/AppModel.cs +++ b/PInvoke/Kernel32/AppModel.cs @@ -453,7 +453,8 @@ namespace Vanara.PInvoke // PWSTR applicationUserModelId ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "F48D19C2-6373-41FC-A99D-E3CCB68D6C6C")] - public static extern Win32Error FormatApplicationUserModelId(string packageFamilyName, string packageRelativeApplicationId, ref uint applicationUserModelIdLength, StringBuilder applicationUserModelId); + public static extern Win32Error FormatApplicationUserModelId(string packageFamilyName, string packageRelativeApplicationId, + ref uint applicationUserModelIdLength, [Optional] StringBuilder applicationUserModelId); /// /// Gets the application user model ID for the specified process. @@ -501,7 +502,8 @@ namespace Vanara.PInvoke // GetApplicationUserModelId( HANDLE hProcess, UINT32 *applicationUserModelIdLength, PWSTR applicationUserModelId ); [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("appmodel.h", MSDNShortId = "FE4E0818-F548-494B-B3BD-FB51DC748451")] - public static extern Win32Error GetApplicationUserModelId(HPROCESS hProcess, ref uint applicationUserModelIdLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder applicationUserModelId); + public static extern Win32Error GetApplicationUserModelId(HPROCESS hProcess, ref uint applicationUserModelIdLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder applicationUserModelId); /// /// Gets the application user model ID for the specified token. @@ -549,7 +551,8 @@ namespace Vanara.PInvoke // GetApplicationUserModelIdFromToken( HANDLE token, UINT32 *applicationUserModelIdLength, PWSTR applicationUserModelId ); [DllImport(Lib.KernelBase, SetLastError = true, ExactSpelling = true)] [PInvokeData("appmodel.h", MSDNShortId = "80036518-927E-4CD0-B499-8EA472AB7E5A")] - public static extern Win32Error GetApplicationUserModelIdFromToken(HTOKEN token, ref uint applicationUserModelIdLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder applicationUserModelId); + public static extern Win32Error GetApplicationUserModelIdFromToken(HTOKEN token, ref uint applicationUserModelIdLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder applicationUserModelId); /// /// Gets the application user model ID for the current process. @@ -591,7 +594,7 @@ namespace Vanara.PInvoke // GetCurrentApplicationUserModelId( UINT32 *applicationUserModelIdLength, PWSTR applicationUserModelId ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "562BB225-0922-4FE7-92C0-573A2CCE3195")] - public static extern Win32Error GetCurrentApplicationUserModelId(ref uint applicationUserModelIdLength, StringBuilder applicationUserModelId); + public static extern Win32Error GetCurrentApplicationUserModelId(ref uint applicationUserModelIdLength, [Optional] StringBuilder applicationUserModelId); /// /// Gets the package family name for the calling process. @@ -636,7 +639,7 @@ namespace Vanara.PInvoke // GetCurrentPackageFamilyName( UINT32 *packageFamilyNameLength, PWSTR packageFamilyName ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "39DBFBDD-A1CC-45C3-A5DD-5ED9697F9AFE")] - public static extern Win32Error GetCurrentPackageFamilyName(ref uint packageFamilyNameLength, StringBuilder packageFamilyName); + public static extern Win32Error GetCurrentPackageFamilyName(ref uint packageFamilyNameLength, [Optional] StringBuilder packageFamilyName); /// /// Gets the package full name for the calling process. @@ -681,7 +684,7 @@ namespace Vanara.PInvoke // GetCurrentPackageFullName( UINT32 *packageFullNameLength, PWSTR packageFullName ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "D5B00C53-1FBF-4245-92D1-FA39713A9EE7")] - public static extern Win32Error GetCurrentPackageFullName(ref uint packageFullNameLength, StringBuilder packageFullName); + public static extern Win32Error GetCurrentPackageFullName(ref uint packageFullNameLength, [Optional] StringBuilder packageFullName); /// /// Gets the package identifier (ID) for the calling process. @@ -863,7 +866,7 @@ namespace Vanara.PInvoke // *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "46CE81DF-A9D5-492E-AB5E-4F043DC326E2")] - public static extern Win32Error GetCurrentPackagePath(ref uint pathLength, StringBuilder path); + public static extern Win32Error GetCurrentPackagePath(ref uint pathLength, [Optional] StringBuilder path); /// /// Gets the package path for the calling process, with the option to specify the type of folder path to retrieve for the package. @@ -915,7 +918,8 @@ namespace Vanara.PInvoke // LONG GetCurrentPackagePath2( PackagePathType packagePathType, UINT32 *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("appmodel.h")] - public static extern Win32Error GetCurrentPackagePath2(PackagePathType packagePathType, ref uint pathLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); + public static extern Win32Error GetCurrentPackagePath2(PackagePathType packagePathType, ref uint pathLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); /// /// Gets the IDs of apps in the specified package. @@ -1013,7 +1017,7 @@ namespace Vanara.PInvoke // hProcess, UINT32 *packageFamilyNameLength, PWSTR packageFamilyName ); [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "AC239898-9924-4193-9072-7A7EEC2D03E9")] - public static extern Win32Error GetPackageFamilyName(HPROCESS hProcess, ref uint packageFamilyNameLength, StringBuilder packageFamilyName); + public static extern Win32Error GetPackageFamilyName(HPROCESS hProcess, ref uint packageFamilyNameLength, [Optional] StringBuilder packageFamilyName); /// /// Gets the package family name for the specified token. @@ -1062,7 +1066,61 @@ namespace Vanara.PInvoke // GetPackageFamilyNameFromToken( HANDLE token, UINT32 *packageFamilyNameLength, PWSTR packageFamilyName ); [DllImport(Lib.KernelBase, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "C4FAF5DE-DF1F-4AFA-813B-5D80C786031B")] - public static extern Win32Error GetPackageFamilyNameFromToken(HTOKEN token, ref uint packageFamilyNameLength, StringBuilder packageFamilyName); + public static extern Win32Error GetPackageFamilyNameFromToken(HTOKEN token, ref uint packageFamilyNameLength, [Optional] StringBuilder packageFamilyName); + + /// Gets the package full name for the specified process. + /// + /// Type: HANDLE + /// + /// A handle to the process that has the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right. + /// For more information, see Process Security and Access Rights. + /// + /// + /// + /// Type: UINT32* + /// + /// On input, the size of the packageFullName buffer, in characters. On output, the size of the package full name returned, in + /// characters, including the null terminator. + /// + /// + /// + /// Type: PWSTR + /// The package full name. + /// + /// + /// 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 code + /// Description + /// + /// + /// APPMODEL_ERROR_NO_PACKAGE + /// The process has no package identity. + /// + /// + /// ERROR_INSUFFICIENT_BUFFER + /// The buffer is not large enough to hold the data. The required size is specified by packageFullNameLength. + /// + /// + /// + /// + /// For info about string size limits, see Identity constants. + /// Examples + /// + /// #define _UNICODE 1 #define UNICODE 1 #include <Windows.h> #include <appmodel.h> #include <malloc.h> #include <stdlib.h> #include <stdio.h> int ShowUsage(); void ShowProcessPackageFullName(__in const UINT32 pid, __in HANDLE process); int ShowUsage() { wprintf(L"Usage: GetPackageFullName <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 { ShowProcessPackageFullName(pid, process); CloseHandle(process); } } } return 0; } void ShowProcessPackageFullName(__in const UINT32 pid, __in HANDLE process) { wprintf(L"Process %u (handle=%p)\n", pid, process); UINT32 length = 0; LONG rc = GetPackageFullName(process, &length, NULL); if (rc != ERROR_INSUFFICIENT_BUFFER) { if (rc == APPMODEL_ERROR_NO_PACKAGE) wprintf(L"Process has no package identity\n"); else wprintf(L"Error %d in GetPackageFullName\n", rc); return; } PWSTR fullName = (PWSTR) malloc(length * sizeof(*fullName)); if (fullName == NULL) { wprintf(L"Error allocating memory\n"); return; } rc = GetPackageFullName(process, &length, fullName); if (rc != ERROR_SUCCESS) wprintf(L"Error %d retrieving PackageFullName\n", rc); else wprintf(L"%s\n", fullName); free(fullName); } + /// + /// + // https://webcache.googleusercontent.com/search?q=cache:IqzT6kD4rycJ:https://docs.microsoft.com/en-us/windows/win32/api/appmodel/nf-appmodel-getpackagefullname+&cd=2&hl=en&ct=clnk&gl=us + // LONG GetPackageFullName( HANDLE hProcess, UINT32 *packageFullNameLength, PWSTR packageFullName ); + [DllImport(Lib_Kernel32, SetLastError = false, ExactSpelling = true)] + [PInvokeData("appmodel.h", MSDNShortId = "NF:appmodel.GetPackageFullName")] + public static extern Win32Error GetPackageFullName(HPROCESS hProcess, ref uint packageFullNameLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFullName); /// /// Gets the package full name for the specified token. @@ -1105,9 +1163,9 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/desktop/api/appmodel/nf-appmodel-getpackagefullnamefromtoken LONG // GetPackageFullNameFromToken( HANDLE token, UINT32 *packageFullNameLength, PWSTR packageFullName ); - [DllImport(Lib.KernelBase, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] + [DllImport(Lib.KernelBase, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "7B0D574E-A2F5-4D08-AEFB-9E040BBC729F")] - public static extern Win32Error GetPackageFullNameFromToken(HTOKEN token, ref uint packageFullNameLength, StringBuilder packageFullName); + public static extern Win32Error GetPackageFullNameFromToken(HTOKEN token, ref uint packageFullNameLength, [Optional] StringBuilder packageFullName); /// /// Gets the package information for the specified package. @@ -1255,7 +1313,7 @@ namespace Vanara.PInvoke // *packageId, const UINT32 reserved, UINT32 *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "BDA0DD87-A36D-486B-BF89-EA5CC105C742")] - public static extern Win32Error GetPackagePath(ref PACKAGE_ID packageId, uint reserved, ref uint pathLength, StringBuilder path); + public static extern Win32Error GetPackagePath(in PACKAGE_ID packageId, uint reserved, ref uint pathLength, [Optional] StringBuilder path); /// /// Gets the path of the specified package. @@ -1299,7 +1357,7 @@ namespace Vanara.PInvoke // PCWSTR packageFullName, UINT32 *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "9C25708C-1464-4C59-9740-E9F105116385")] - public static extern Win32Error GetPackagePathByFullName(string packageFullName, ref uint pathLength, StringBuilder path); + public static extern Win32Error GetPackagePathByFullName(string packageFullName, ref uint pathLength, [Optional] StringBuilder path); /// Gets the path of the specified package, with the option to specify the type of folder path to retrieve for the package. /// @@ -1352,7 +1410,8 @@ namespace Vanara.PInvoke // LONG GetPackagePathByFullName2( PCWSTR packageFullName, PackagePathType packagePathType, UINT32 *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("appmodel.h")] - public static extern Win32Error GetPackagePathByFullName2([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, PackagePathType packagePathType, ref uint pathLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); + public static extern Win32Error GetPackagePathByFullName2([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, PackagePathType packagePathType, + ref uint pathLength, [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); /// /// Gets the packages with the specified family name for the current user. @@ -1492,7 +1551,8 @@ namespace Vanara.PInvoke // GetStagedPackagePathByFullName( PCWSTR packageFullName, UINT32 *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("appmodel.h", MSDNShortId = "F0A37D77-6262-44B1-BEC5-083E41BDE139")] - public static extern Win32Error GetStagedPackagePathByFullName([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, ref uint pathLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); + public static extern Win32Error GetStagedPackagePathByFullName([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, ref uint pathLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); /// /// Gets the path of the specified staged package, with the option to specify the type of folder path to retrieve for the package. @@ -1547,7 +1607,8 @@ namespace Vanara.PInvoke // LONG GetStagedPackagePathByFullName2( PCWSTR packageFullName, PackagePathType packagePathType, UINT32 *pathLength, PWSTR path ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("appmodel.h")] - public static extern Win32Error GetStagedPackagePathByFullName2([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, PackagePathType packagePathType, ref uint pathLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); + public static extern Win32Error GetStagedPackagePathByFullName2([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, PackagePathType packagePathType, + ref uint pathLength, [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder path); /// /// Opens the package information of the specified package. @@ -1630,7 +1691,8 @@ namespace Vanara.PInvoke // PackageFamilyNameFromFullName( PCWSTR packageFullName, UINT32 *packageFamilyNameLength, PWSTR packageFamilyName ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "98E95CE5-E970-4A19-BAD3-994DAEC4BEA0")] - public static extern Win32Error PackageFamilyNameFromFullName(string packageFullName, ref uint packageFamilyNameLength, StringBuilder packageFamilyName); + public static extern Win32Error PackageFamilyNameFromFullName(string packageFullName, ref uint packageFamilyNameLength, + [Optional] StringBuilder packageFamilyName); /// /// Gets the package family name for the specified package identifier. @@ -1675,7 +1737,8 @@ namespace Vanara.PInvoke // const PACKAGE_ID *packageId, UINT32 *packageFamilyNameLength, PWSTR packageFamilyName ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("appmodel.h", MSDNShortId = "198DAB6B-21D2-4ACB-87DF-B3F4EFBEE323")] - public static extern Win32Error PackageFamilyNameFromId(ref PACKAGE_ID packageId, ref uint packageFamilyNameLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFamilyName); + public static extern Win32Error PackageFamilyNameFromId(in PACKAGE_ID packageId, ref uint packageFamilyNameLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFamilyName); /// /// Gets the package full name for the specified package identifier (ID). @@ -1720,7 +1783,8 @@ namespace Vanara.PInvoke // PACKAGE_ID *packageId, UINT32 *packageFullNameLength, PWSTR packageFullName ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("appmodel.h", MSDNShortId = "0024AF55-295E-49B1-90C2-9144D336529B")] - public static extern Win32Error PackageFullNameFromId(ref PACKAGE_ID packageId, ref uint packageFullNameLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFullName); + public static extern Win32Error PackageFullNameFromId(in PACKAGE_ID packageId, ref uint packageFullNameLength, + [Optional, MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFullName); /// /// Gets the package identifier (ID) for the specified package full name. @@ -1871,7 +1935,8 @@ namespace Vanara.PInvoke // *packagePublisherIdLength, PWSTR packagePublisherId ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "4AA5BD75-F865-40D6-9C10-E54C197D47C4")] - public static extern Win32Error PackageNameAndPublisherIdFromFamilyName(string packageFamilyName, ref uint packageNameLength, StringBuilder packageName, ref uint packagePublisherIdLength, StringBuilder packagePublisherId); + public static extern Win32Error PackageNameAndPublisherIdFromFamilyName(string packageFamilyName, ref uint packageNameLength, + [Optional] StringBuilder packageName, ref uint packagePublisherIdLength, [Optional] StringBuilder packagePublisherId); /// /// Deconstructs an application user model ID to its package family name and package relative application ID (PRAID). @@ -1939,7 +2004,8 @@ namespace Vanara.PInvoke // *packageRelativeApplicationIdLength, PWSTR packageRelativeApplicationId ); [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("appmodel.h", MSDNShortId = "03B29E82-611F-47D1-8CB6-047B9BEB4D9E")] - public static extern Win32Error ParseApplicationUserModelId(string applicationUserModelId, ref uint packageFamilyNameLength, StringBuilder packageFamilyName, ref uint packageRelativeApplicationIdLength, StringBuilder packageRelativeApplicationId); + public static extern Win32Error ParseApplicationUserModelId(string applicationUserModelId, ref uint packageFamilyNameLength, + [Optional] StringBuilder packageFamilyName, ref uint packageRelativeApplicationIdLength, [Optional] StringBuilder packageRelativeApplicationId); /// /// Represents package identification information, such as name, version, and publisher.