Skip to content

Commit

Permalink
Added retrieval of App Package ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
AtlasHackert committed Apr 12, 2017
1 parent 1ce53d9 commit 0817c99
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions Common/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using System.Text;
using System.Management;

namespace Wokhan.WindowsFirewallNotifier.Common.Helpers
Expand Down Expand Up @@ -118,6 +119,16 @@ private enum SC_ENUM_TYPE : uint

private const uint ERROR_MORE_DATA = 234;

//Note: Only exists on Windows 8 and higher
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern uint GetPackageFullName(IntPtr hProcess, ref uint packageFullNameLength, StringBuilder packageFullName);

private const uint ERROR_SUCCESS = 0;
private const uint APPMODEL_ERROR_NO_PACKAGE = 15700;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(ProcessHelper.ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);

public static string[] GetProcessOwnerWMI(int owningPid, ref Dictionary<int, string[]> previousCache)
{
if (previousCache == null)
Expand Down Expand Up @@ -390,8 +401,40 @@ private static string getServiceDesc(string service)

public static string getAppPkgId(int pid)
{
//@
return String.Empty;
if (Environment.OSVersion.Version <= new System.Version(6, 2))
{
//Not Windows 8 or higher, there are no Apps
return String.Empty;
}

IntPtr hProcess = OpenProcess(ProcessHelper.ProcessAccessFlags.QueryLimitedInformation, false, (uint)pid);
if (hProcess == IntPtr.Zero)
{
LogHelper.Warning("Unable to retrieve process package id: process cannot be found!");
return String.Empty;
}

//Based on: https://github.com/jimschubert/clr-profiler/blob/master/src/CLRProfiler45Source/WindowsStoreAppHelper/WindowsStoreAppHelper.cs
uint packageFullNameLength = 0;
StringBuilder packageFullNameBld = new StringBuilder();

uint ret = GetPackageFullName(hProcess, ref packageFullNameLength, packageFullNameBld);
if ((ret == APPMODEL_ERROR_NO_PACKAGE) || (packageFullNameLength == 0))
{
// Not a WindowsStoreApp process
return String.Empty;
}

// Call again, now that we know the size
packageFullNameBld = new StringBuilder((int)packageFullNameLength);
ret = GetPackageFullName(hProcess, ref packageFullNameLength, packageFullNameBld);
if (ret != ERROR_SUCCESS)
{
LogHelper.Warning("Unable to retrieve process package id: failed to retrieve full package name!");
return String.Empty;
}

return packageFullNameBld.ToString();
}

public static bool getProcessFeedback(string cmd, string args)
Expand Down

0 comments on commit 0817c99

Please sign in to comment.