使用IA#CpplicationActivationManager在Wpf当前窗口中打开Store App

时间:2016-07-25 11:20:23

标签: c# wpf

我正在使用IApplicationActivationManager接口方法来启动我的WinStore包。但是期望的应用程序在单独的窗口中打开我需要在当前窗口中打开此应用程序。你能帮我一下吗?我的代码如下:

[ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]
class ApplicationActivationManager { }

static class MetroLauncher
{
    public static uint LaunchApp(string packageFullName, string arguments = null)
    {
        IntPtr pir = IntPtr.Zero;
        try
        {
            int error = OpenPackageInfoByFullName(packageFullName, 0, out pir);
            Debug.Assert(error == 0);
            if (error != 0)
                throw new Win32Exception(error);

            int length = 0, count;
            GetPackageApplicationIds(pir, ref length, null, out count);

            var buffer = new byte[length];
            error = GetPackageApplicationIds(pir, ref length, buffer, out count);
            Debug.Assert(error == 0);
            if (error != 0)
                throw new Win32Exception(error);

            var appUserModelId = Encoding.Unicode.GetString(buffer, IntPtr.Size * count, length - IntPtr.Size * count);

            var activation = (IApplicationActivationManager)new ApplicationActivationManager();
            uint pid;
            int hr = activation.ActivateApplication(appUserModelId, arguments ?? string.Empty, ActivateOptions.NoErrorUI, out pid);

            if (hr < 0)
                Marshal.ThrowExceptionForHR(hr);
            return pid;
        }
        finally
        {
            if (pir != IntPtr.Zero)
                ClosePackageInfo(pir);
        }

    }

    [DllImport("kernel32")]
    static extern int OpenPackageInfoByFullName([MarshalAs(UnmanagedType.LPWStr)] string fullName, uint reserved, out IntPtr packageInfo);

    [DllImport("kernel32")]
    static extern int GetPackageApplicationIds(IntPtr pir, ref int bufferLength, byte[] buffer, out int count);

    [DllImport("kernel32")]
    static extern int ClosePackageInfo(IntPtr pir);
}




[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("2e941141-7f97-4756-ba1d-9decde894a3d")]
interface IApplicationActivationManager {
    int ActivateApplication([MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, [MarshalAs(UnmanagedType.LPWStr)] string arguments, 
        ActivateOptions options, out uint processId);
    int ActivateForFile([MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, IntPtr pShelItemArray, 
        [MarshalAs(UnmanagedType.LPWStr)] string verb, out uint processId);
    int ActivateForProtocol([MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, IntPtr pShelItemArray, 
        [MarshalAs(UnmanagedType.LPWStr)] string verb, out uint processId);
}

0 个答案:

没有答案