Exemplo n.º 1
0
        private static bool HandleCommandLineArgs()
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            var arguments     = (string)localSettings.Values["Arguments"];

            if (!string.IsNullOrWhiteSpace(arguments))
            {
                localSettings.Values.Remove("Arguments");

                if (arguments == "ShellCommand")
                {
                    // Kill the process. This is a BRUTAL WAY to kill a process.
#if DEBUG
                    // In debug mode this kills this process too??
#else
                    var pid = (int)localSettings.Values["pid"];
                    Process.GetProcessById(pid).Kill();
#endif

                    Process process = new Process();
                    process.StartInfo.UseShellExecute = true;
                    process.StartInfo.FileName        = "explorer.exe";
                    process.StartInfo.CreateNoWindow  = false;
                    process.StartInfo.Arguments       = (string)localSettings.Values["ShellCommand"];
                    process.Start();

                    return(true);
                }
                else if (arguments == "StartupTasks")
                {
                    // Check QuickLook Availability
                    QuickLook.CheckQuickLookAvailability(localSettings);
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        private static async Task parseArguments(AppServiceRequestReceivedEventArgs args, AppServiceDeferral messageDeferral, string arguments, ApplicationDataContainer localSettings)
        {
            switch (arguments)
            {
            case "Terminate":
                // Exit fulltrust process (UWP is closed or suspended)
                appServiceExit.Set();
                messageDeferral.Complete();
                break;

            case "RecycleBin":
                var binAction = (string)args.Request.Message["action"];
                await parseRecycleBinAction(args, binAction);

                break;

            case "StartupTasks":
                // Check QuickLook Availability
                QuickLook.CheckQuickLookAvailability(localSettings);
                break;

            case "ToggleQuickLook":
                var path = (string)args.Request.Message["path"];
                QuickLook.ToggleQuickLook(path);
                break;

            case "ShellCommand":
                // Kill the process. This is a BRUTAL WAY to kill a process.
#if DEBUG
                // In debug mode this kills this process too??
#else
                var pid = (int)args.Request.Message["pid"];
                Process.GetProcessById(pid).Kill();
#endif

                Process process = new Process();
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName        = "explorer.exe";
                process.StartInfo.CreateNoWindow  = false;
                process.StartInfo.Arguments       = (string)args.Request.Message["ShellCommand"];
                process.Start();
                break;

            case "LoadMUIVerb":
                var responseSet = new ValueSet();
                responseSet.Add("MUIVerbString", Win32API.ExtractStringFromDLL((string)args.Request.Message["MUIVerbLocation"], (int)args.Request.Message["MUIVerbLine"]));
                await args.Request.SendResponseAsync(responseSet);

                break;

            case "ParseAguments":
                var responseArray  = new ValueSet();
                var resultArgument = Win32API.CommandLineToArgs((string)args.Request.Message["Command"]);
                responseArray.Add("ParsedArguments", JsonConvert.SerializeObject(resultArgument));
                await args.Request.SendResponseAsync(responseArray);

                break;

            case "Bitlocker":
                var bitlockerAction = (string)args.Request.Message["action"];
                if (bitlockerAction == "Unlock")
                {
                    var drive    = (string)args.Request.Message["drive"];
                    var password = (string)args.Request.Message["password"];
                    Win32API.UnlockBitlockerDrive(drive, password);
                    await args.Request.SendResponseAsync(new ValueSet()
                    {
                        { "Bitlocker", "Unlock" }
                    });
                }
                break;

            default:
                if (args.Request.Message.ContainsKey("Application"))
                {
                    var application = (string)args.Request.Message["Application"];
                    HandleApplicationLaunch(application, args);
                }
                else if (args.Request.Message.ContainsKey("ApplicationList"))
                {
                    var applicationList = JsonConvert.DeserializeObject <IEnumerable <string> >((string)args.Request.Message["ApplicationList"]);
                    HandleApplicationsLaunch(applicationList, args);
                }
                break;
            }
        }
Exemplo n.º 3
0
        private static async Task ParseArgumentsAsync(AppServiceRequestReceivedEventArgs args, AppServiceDeferral messageDeferral, string arguments, ApplicationDataContainer localSettings)
        {
            switch (arguments)
            {
            case "Terminate":
                // Exit fulltrust process (UWP is closed or suspended)
                appServiceExit.Set();
                messageDeferral.Complete();
                break;

            case "RecycleBin":
                var binAction = (string)args.Request.Message["action"];
                await ParseRecycleBinActionAsync(args, binAction);

                break;

            case "StartupTasks":
                // Check QuickLook Availability
                QuickLook.CheckQuickLookAvailability(localSettings);
                break;

            case "ToggleQuickLook":
                var path = (string)args.Request.Message["path"];
                QuickLook.ToggleQuickLook(path);
                break;

            case "ShellCommand":
                // Kill the process. This is a BRUTAL WAY to kill a process.
#if DEBUG
                // In debug mode this kills this process too??
#else
                var pid = (int)args.Request.Message["pid"];
                Process.GetProcessById(pid).Kill();
#endif

                Process process = new Process();
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName        = "explorer.exe";
                process.StartInfo.CreateNoWindow  = false;
                process.StartInfo.Arguments       = (string)args.Request.Message["ShellCommand"];
                process.Start();
                break;

            case "LoadContextMenu":
                var contextMenuResponse        = new ValueSet();
                var loadThreadWithMessageQueue = new Win32API.ThreadWithMessageQueue <ValueSet>(HandleMenuMessage);
                var cMenuLoad = await loadThreadWithMessageQueue.PostMessageAsync <Win32API.ContextMenu>(args.Request.Message);

                contextMenuResponse.Add("Handle", handleTable.AddValue(loadThreadWithMessageQueue));
                contextMenuResponse.Add("ContextMenu", JsonConvert.SerializeObject(cMenuLoad));
                await args.Request.SendResponseAsync(contextMenuResponse);

                break;

            case "ExecAndCloseContextMenu":
                var menuKey = (string)args.Request.Message["Handle"];
                var execThreadWithMessageQueue = handleTable.GetValue <Win32API.ThreadWithMessageQueue <ValueSet> >(menuKey);
                if (execThreadWithMessageQueue != null)
                {
                    await execThreadWithMessageQueue.PostMessage(args.Request.Message);
                }
                // The following line is needed to cleanup resources when menu is closed.
                // Unfortunately if you uncomment it some menu items will randomly stop working.
                // Resource cleanup is currently done on app closing,
                // if we find a solution for the issue above, we should cleanup as soon as a menu is closed.
                //handleTable.RemoveValue(menuKey);
                break;

            case "InvokeVerb":
                var filePath = (string)args.Request.Message["FilePath"];
                var split    = filePath.Split('|').Where(x => !string.IsNullOrWhiteSpace(x));
                using (var cMenu = Win32API.ContextMenu.GetContextMenuForFiles(split.ToArray(), Shell32.CMF.CMF_DEFAULTONLY))
                {
                    cMenu?.InvokeVerb((string)args.Request.Message["Verb"]);
                }
                break;

            case "Bitlocker":
                var bitlockerAction = (string)args.Request.Message["action"];
                if (bitlockerAction == "Unlock")
                {
                    var drive    = (string)args.Request.Message["drive"];
                    var password = (string)args.Request.Message["password"];
                    Win32API.UnlockBitlockerDrive(drive, password);
                    await args.Request.SendResponseAsync(new ValueSet()
                    {
                        { "Bitlocker", "Unlock" }
                    });
                }
                break;

            case "SetVolumeLabel":
                var driveName = (string)args.Request.Message["drivename"];
                var newLabel  = (string)args.Request.Message["newlabel"];
                Win32API.SetVolumeLabel(driveName, newLabel);
                break;

            case "FileOperation":
                await ParseFileOperationAsync(args);

                break;

            case "GetIconOverlay":
                var fileIconPath  = (string)args.Request.Message["filePath"];
                var thumbnailSize = (int)args.Request.Message["thumbnailSize"];
                var iconOverlay   = Win32API.GetFileIconAndOverlay(fileIconPath, thumbnailSize);
                await args.Request.SendResponseAsync(new ValueSet()
                {
                    { "Icon", iconOverlay.icon },
                    { "Overlay", iconOverlay.overlay },
                    { "HasCustomIcon", iconOverlay.isCustom }
                });

                break;

            case "GetOneDriveAccounts":
                using (var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts", false))
                {
                    var oneDriveAccounts = new ValueSet();
                    foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
                    {
                        var accountKeyName = @$ "{oneDriveAccountsKey.Name}\{account}";
                        var displayName    = (string)Registry.GetValue(accountKeyName, "DisplayName", null);
                        var userFolder     = (string)Registry.GetValue(accountKeyName, "UserFolder", null);
                        var accountName    = string.IsNullOrWhiteSpace(displayName) ? "OneDrive" : $"OneDrive - {displayName}";
                        if (!string.IsNullOrWhiteSpace(userFolder) && !oneDriveAccounts.ContainsKey(accountName))
                        {
                            oneDriveAccounts.Add(accountName, userFolder);
                        }
                    }
                    await args.Request.SendResponseAsync(oneDriveAccounts);
                }
                break;
Exemplo n.º 4
0
        private static async Task parseArguments(AppServiceRequestReceivedEventArgs args, AppServiceDeferral messageDeferral, string arguments, ApplicationDataContainer localSettings)
        {
            switch (arguments)
            {
            case "Terminate":
                // Exit fulltrust process (UWP is closed or suspended)
                appServiceExit.Set();
                messageDeferral.Complete();
                break;

            case "RecycleBin":
                var binAction = (string)args.Request.Message["action"];
                await parseRecycleBinAction(args, binAction);

                break;

            case "StartupTasks":
                // Check QuickLook Availability
                QuickLook.CheckQuickLookAvailability(localSettings);
                break;

            case "ToggleQuickLook":
                var path = (string)args.Request.Message["path"];
                QuickLook.ToggleQuickLook(path);
                break;

            case "ShellCommand":
                // Kill the process. This is a BRUTAL WAY to kill a process.
#if DEBUG
                // In debug mode this kills this process too??
#else
                var pid = (int)args.Request.Message["pid"];
                Process.GetProcessById(pid).Kill();
#endif

                Process process = new Process();
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName        = "explorer.exe";
                process.StartInfo.CreateNoWindow  = false;
                process.StartInfo.Arguments       = (string)args.Request.Message["ShellCommand"];
                process.Start();
                break;

            case "LoadContextMenu":
                var contextMenuResponse        = new ValueSet();
                var loadThreadWithMessageQueue = new Win32API.ThreadWithMessageQueue <ValueSet>(HandleMenuMessage);
                var cMenuLoad = await loadThreadWithMessageQueue.PostMessage <Win32API.ContextMenu>(args.Request.Message);

                contextMenuResponse.Add("Handle", handleTable.AddValue(loadThreadWithMessageQueue));
                contextMenuResponse.Add("ContextMenu", JsonConvert.SerializeObject(cMenuLoad));
                await args.Request.SendResponseAsync(contextMenuResponse);

                break;

            case "ExecAndCloseContextMenu":
                var menuKey = (string)args.Request.Message["Handle"];
                var execThreadWithMessageQueue = handleTable.GetValue <Win32API.ThreadWithMessageQueue <ValueSet> >(menuKey);
                if (execThreadWithMessageQueue != null)
                {
                    await execThreadWithMessageQueue.PostMessage(args.Request.Message);
                }
                // The following line is needed to cleanup resources when menu is closed.
                // Unfortunately if you uncomment it some menu items will randomly stop working.
                // Resource cleanup is currently done on app closing,
                // if we find a solution for the issue above, we should cleanup as soon as a menu is closed.
                //handleTable.RemoveValue(menuKey);
                break;

            case "InvokeVerb":
                var filePath = (string)args.Request.Message["FilePath"];
                var split    = filePath.Split('|').Where(x => !string.IsNullOrWhiteSpace(x));
                using (var cMenu = Win32API.ContextMenu.GetContextMenuForFiles(split.ToArray(), Shell32.CMF.CMF_DEFAULTONLY))
                {
                    cMenu?.InvokeVerb((string)args.Request.Message["Verb"]);
                }
                break;

            case "Bitlocker":
                var bitlockerAction = (string)args.Request.Message["action"];
                if (bitlockerAction == "Unlock")
                {
                    var drive    = (string)args.Request.Message["drive"];
                    var password = (string)args.Request.Message["password"];
                    Win32API.UnlockBitlockerDrive(drive, password);
                    await args.Request.SendResponseAsync(new ValueSet()
                    {
                        { "Bitlocker", "Unlock" }
                    });
                }
                break;

            case "FileOperation":
                await parseFileOperation(args);

                break;

            default:
                if (args.Request.Message.ContainsKey("Application"))
                {
                    var application = (string)args.Request.Message["Application"];
                    HandleApplicationLaunch(application, args);
                }
                else if (args.Request.Message.ContainsKey("ApplicationList"))
                {
                    var applicationList = JsonConvert.DeserializeObject <IEnumerable <string> >((string)args.Request.Message["ApplicationList"]);
                    HandleApplicationsLaunch(applicationList, args);
                }
                break;
            }
        }
Exemplo n.º 5
0
        private static async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            // Get a deferral because we use an awaitable API below to respond to the message
            // and we don't want this call to get cancelled while we are waiting.
            var messageDeferral = args.GetDeferral();

            if (args.Request.Message == null)
            {
                messageDeferral.Complete();
                return;
            }

            try
            {
                if (args.Request.Message.ContainsKey("Arguments"))
                {
                    // This replaces launching the fulltrust process with arguments
                    // Instead a single instance of the process is running
                    // Requests from UWP app are sent via AppService connection
                    var arguments = (string)args.Request.Message["Arguments"];

                    Logger.Info($"Argument: {arguments}");

                    if (arguments == "Terminate")
                    {
                        // Exit fulltrust process (UWP is closed or suspended)
                        appServiceExit.Set();
                        messageDeferral.Complete();
                    }
                    else if (arguments == "RecycleBin")
                    {
                        var action = (string)args.Request.Message["action"];
                        if (action == "Empty")
                        {
                            // Shell function to empty recyclebin
                            Vanara.PInvoke.Shell32.SHEmptyRecycleBin(IntPtr.Zero, null, Vanara.PInvoke.Shell32.SHERB.SHERB_NOCONFIRMATION | Vanara.PInvoke.Shell32.SHERB.SHERB_NOPROGRESSUI);
                        }
                        else if (action == "Query")
                        {
                            var responseQuery = new ValueSet();
                            Win32API.SHQUERYRBINFO queryBinInfo = new Win32API.SHQUERYRBINFO();
                            queryBinInfo.cbSize = (uint)Marshal.SizeOf(typeof(Win32API.SHQUERYRBINFO));
                            var res = Win32API.SHQueryRecycleBin("", ref queryBinInfo);
                            // TODO: use this when updated library is released
                            //Vanara.PInvoke.Shell32.SHQUERYRBINFO queryBinInfo = new Vanara.PInvoke.Shell32.SHQUERYRBINFO();
                            //Vanara.PInvoke.Shell32.SHQueryRecycleBin(null, ref queryBinInfo);
                            if (res == Vanara.PInvoke.HRESULT.S_OK)
                            {
                                var numItems = queryBinInfo.i64NumItems;
                                var binSize  = queryBinInfo.i64Size;
                                responseQuery.Add("NumItems", numItems);
                                responseQuery.Add("BinSize", binSize);
                                await args.Request.SendResponseAsync(responseQuery);
                            }
                        }
                        else if (action == "Enumerate")
                        {
                            // Enumerate recyclebin contents and send response to UWP
                            var responseEnum       = new ValueSet();
                            var folderContentsList = new List <ShellFileItem>();
                            foreach (var folderItem in recycler)
                            {
                                try
                                {
                                    folderItem.Properties.ReadOnly = true;
                                    folderItem.Properties.NoInheritedProperties = false;
                                    string recyclePath   = folderItem.FileSystemPath;         // True path on disk
                                    string fileName      = Path.GetFileName(folderItem.Name); // Original file name
                                    string filePath      = folderItem.Name;                   // Original file path + name
                                    var    dt            = (System.Runtime.InteropServices.ComTypes.FILETIME)folderItem.Properties[Vanara.PInvoke.Ole32.PROPERTYKEY.System.DateCreated];
                                    var    recycleDate   = dt.ToDateTime().ToLocalTime();     // This is LocalTime
                                    string fileSize      = folderItem.Properties.GetPropertyString(Vanara.PInvoke.Ole32.PROPERTYKEY.System.Size);
                                    ulong  fileSizeBytes = (ulong)folderItem.Properties[Vanara.PInvoke.Ole32.PROPERTYKEY.System.Size];
                                    string fileType      = (string)folderItem.Properties[Vanara.PInvoke.Ole32.PROPERTYKEY.System.ItemTypeText];
                                    bool   isFolder      = folderItem.IsFolder && Path.GetExtension(folderItem.Name) != ".zip";
                                    folderContentsList.Add(new ShellFileItem(isFolder, recyclePath, fileName, filePath, recycleDate, fileSize, fileSizeBytes, fileType));
                                }
                                catch (System.IO.FileNotFoundException)
                                {
                                    // Happens if files are being deleted
                                }
                                finally
                                {
                                    folderItem.Dispose();
                                }
                            }
                            responseEnum.Add("Enumerate", Newtonsoft.Json.JsonConvert.SerializeObject(folderContentsList));
                            await args.Request.SendResponseAsync(responseEnum);
                        }
                    }
                    else if (arguments == "ToggleQuickLook")
                    {
                        var path = (string)args.Request.Message["path"];
                        QuickLook.ToggleQuickLook(path);
                    }
                    else if (arguments == "ShellCommand")
                    {
                        // Kill the process. This is a BRUTAL WAY to kill a process.
#if DEBUG
                        // In debug mode this kills this process too??
#else
                        var pid = (int)args.Request.Message["pid"];
                        Process.GetProcessById(pid).Kill();
#endif

                        Process process = new Process();
                        process.StartInfo.UseShellExecute = true;
                        process.StartInfo.FileName        = "explorer.exe";
                        process.StartInfo.CreateNoWindow  = false;
                        process.StartInfo.Arguments       = (string)args.Request.Message["ShellCommand"];
                        process.Start();
                    }
                    else if (args.Request.Message.ContainsKey("Application"))
                    {
                        HandleApplicationLaunch(args);
                    }
                }
                else if (args.Request.Message.ContainsKey("Application"))
                {
                    HandleApplicationLaunch(args);
                }
            }
            finally
            {
                // Complete the deferral so that the platform knows that we're done responding to the app service call.
                // Note for error handling: this must be called even if SendResponseAsync() throws an exception.
                messageDeferral.Complete();
            }
        }