using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using Windows.System.Update; namespace NativeHelpers { // CLSID_StartMenuCacheAndAppResolver {660b90c8-73a9-4b58-8cae-355b7f55341b} [ComImport, ClassInterface(ClassInterfaceType.None), Guid("660b90c8-73a9-4b58-8cae-355b7f55341b")] public class CStartMenuCacheAndAppResolver { } public class AppResolver { IAppResolver_7 app7 = null; IAppResolver_8 app8 = null; public AppResolver() { Exception last = null; try { app8 = (IAppResolver_8)new CStartMenuCacheAndAppResolver(); } catch (Exception e) { last = e; app8 = null; } if (app8 == null) { try { app7 = (IAppResolver_7)new CStartMenuCacheAndAppResolver(); } catch (Exception e) { last = e; app7 = null; } } if (app7 == null && app8 == null) { throw last; } } public string GetAppIDForWindow(IntPtr hWnd) { IntPtr output = IntPtr.Zero; if (app7 != null) { app7.GetAppIDForWindow(hWnd, ref output, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } else if (app8 != null) { app8.GetAppIDForWindow(hWnd, ref output, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } if (output != IntPtr.Zero) { return Marshal.PtrToStringUni(output); } return null; } public string GetAppIDForProcess(UInt32 processId) { IntPtr output = IntPtr.Zero; if (app7 != null) { app7.GetAppIDForProcess(processId, ref output, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } else if (app8 != null) { app8.GetAppIDForProcess(processId, ref output, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } if (output != IntPtr.Zero) { return Marshal.PtrToStringUni(output); } return null; } } // IID_IAppResolver_7 {46a6eeff-908e-4dc6-92a6-64be9177b41c} [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("46a6eeff-908e-4dc6-92a6-64be9177b41c")] public interface IAppResolver_7 { uint GetAppIDForShortcut(); uint GetAppIDForWindow(IntPtr hWnd, ref IntPtr pszAppId, IntPtr pUnknown1, IntPtr pUnknown2, IntPtr pUnknown3); uint GetAppIDForProcess(UInt32 dwProcessId, ref IntPtr pszAppId, IntPtr pUnknown1, IntPtr pUnknown2, IntPtr pUnknown3); } // IID_IAppResolver_8 {de25675a-72de-44b4-9373-05170450c140} [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("de25675a-72de-44b4-9373-05170450c140")] public interface IAppResolver_8 { uint GetAppIDForShortcut(); uint GetAppIDForShortcutObject(); uint GetAppIDForWindow(IntPtr hWnd, ref IntPtr pszAppId, IntPtr pUnknown1, IntPtr pUnknown2, IntPtr pUnknown3); uint GetAppIDForProcess(UInt32 dwProcessId, ref IntPtr pszAppId, IntPtr pUnknown1, IntPtr pUnknown2, IntPtr pUnknown3); } }