Skip to content

Commit 5bc3664

Browse files
committed
Return success from HandleApplicationLaunch. Don't show error dialog if file was launched
1 parent 78645c6 commit 5bc3664

File tree

4 files changed

+26
-48
lines changed

4 files changed

+26
-48
lines changed

src/Files.Launcher/MessageHandlers/ApplicationLaunchHandler.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using Vanara.PInvoke;
1414
using Vanara.Windows.Shell;
15+
using Windows.Foundation.Collections;
1516

1617
namespace FilesFullTrust.MessageHandlers
1718
{
@@ -21,7 +22,7 @@ public void Initialize(PipeStream connection)
2122
{
2223
}
2324

24-
public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object> message, string arguments)
25+
public async Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object> message, string arguments)
2526
{
2627
switch (arguments)
2728
{
@@ -38,12 +39,11 @@ public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object
3839
if (message.ContainsKey("Application"))
3940
{
4041
var application = (string)message["Application"];
41-
HandleApplicationLaunch(application, message);
42-
}
43-
else if (message.ContainsKey("ApplicationList"))
44-
{
45-
var applicationList = JsonConvert.DeserializeObject<IEnumerable<string>>((string)message["ApplicationList"]);
46-
HandleApplicationsLaunch(applicationList, message);
42+
var success = await HandleApplicationLaunch(application, message);
43+
await Win32API.SendMessageAsync(connection, new ValueSet()
44+
{
45+
{ "Success", success }
46+
}, message.Get("RequestID", (string)null));
4747
}
4848
break;
4949

@@ -53,22 +53,13 @@ public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object
5353
var afPath = Path.Combine(Path.GetTempPath(), "CompatibilityTroubleshooterAnswerFile.xml");
5454
File.WriteAllText(afPath, string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Answers Version=\"1.0\"><Interaction ID=\"IT_LaunchMethod\"><Value>CompatTab</Value></Interaction><Interaction ID=\"IT_BrowseForFile\"><Value>{0}</Value></Interaction></Answers>", filePath));
5555
message["Parameters"] = $"/id PCWDiagnostic /af \"{afPath}\"";
56-
HandleApplicationLaunch("msdt.exe", message);
56+
await HandleApplicationLaunch("msdt.exe", message);
5757
}
5858
break;
5959
}
60-
return Task.CompletedTask;
61-
}
62-
63-
private void HandleApplicationsLaunch(IEnumerable<string> applications, Dictionary<string, object> message)
64-
{
65-
foreach (var application in applications)
66-
{
67-
HandleApplicationLaunch(application, message);
68-
}
6960
}
7061

71-
private async void HandleApplicationLaunch(string application, Dictionary<string, object> message)
62+
private async Task<bool> HandleApplicationLaunch(string application, Dictionary<string, object> message)
7263
{
7364
var arguments = message.Get("Parameters", "");
7465
var workingDirectory = message.Get("WorkingDirectory", "");
@@ -77,8 +68,7 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
7768
if (new[] { ".vhd", ".vhdx" }.Contains(Path.GetExtension(application).ToLowerInvariant()))
7869
{
7970
// Use powershell to mount vhds as this requires admin rights
80-
Win32API.MountVhdDisk(application);
81-
return;
71+
return Win32API.MountVhdDisk(application);
8272
}
8373

8474
try
@@ -123,6 +113,7 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
123113
process.StartInfo.WorkingDirectory = workingDirectory;
124114
process.Start();
125115
Win32API.BringToForeground(currentWindows);
116+
return true;
126117
}
127118
catch (Win32Exception)
128119
{
@@ -136,12 +127,13 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
136127
{
137128
process.Start();
138129
Win32API.BringToForeground(currentWindows);
130+
return true;
139131
}
140132
catch (Win32Exception)
141133
{
142134
try
143135
{
144-
await Win32API.StartSTATask(() =>
136+
return await Win32API.StartSTATask(() =>
145137
{
146138
var split = application.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x));
147139
if (split.Count() == 1)
@@ -172,16 +164,19 @@ await Win32API.StartSTATask(() =>
172164
catch (Win32Exception)
173165
{
174166
// Cannot open file (e.g DLL)
167+
return false;
175168
}
176169
catch (ArgumentException)
177170
{
178171
// Cannot open file (e.g DLL)
172+
return false;
179173
}
180174
}
181175
}
182176
catch (InvalidOperationException)
183177
{
184178
// Invalid file path
179+
return false;
185180
}
186181
}
187182

src/Files.Launcher/Win32API.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ public static void SetVolumeLabel(string driveName, string newLabel)
354354
RunPowershellCommand($"-command \"$Signature = '[DllImport(\\\"kernel32.dll\\\", SetLastError = false)]public static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);'; $SetVolumeLabel = Add-Type -MemberDefinition $Signature -Name \"Win32SetVolumeLabel\" -Namespace Win32Functions -PassThru; $SetVolumeLabel::SetVolumeLabel('{driveName}', '{newLabel}')\"", true);
355355
}
356356

357-
public static void MountVhdDisk(string vhdPath)
357+
public static bool MountVhdDisk(string vhdPath)
358358
{
359359
// mounting requires elevation
360-
RunPowershellCommand($"-command \"Mount-DiskImage -ImagePath '{vhdPath}'\"", true);
360+
return RunPowershellCommand($"-command \"Mount-DiskImage -ImagePath '{vhdPath}'\"", true);
361361
}
362362

363363
public static Bitmap GetBitmapFromHBitmap(HBITMAP hBitmap)

src/Files/Helpers/Win32Helpers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public static async Task<bool> InvokeWin32ComponentsAsync(IEnumerable<string> ap
3232
{
3333
{ "Arguments", "LaunchApp" },
3434
{ "WorkingDirectory", string.IsNullOrEmpty(workingDirectory) ? associatedInstance?.FilesystemViewModel?.WorkingDirectory : workingDirectory },
35-
{ "Application", applicationPaths.FirstOrDefault() },
36-
{ "ApplicationList", JsonConvert.SerializeObject(applicationPaths) },
35+
{ "Application", applicationPaths.FirstOrDefault() }
3736
};
3837

3938
if (runAsAdmin)

src/Files/ViewModels/NavToolbarViewModel.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -934,30 +934,11 @@ public async Task CheckPathInput(string currentInput, string currentSelectedPath
934934
? CommonPaths.HomePath
935935
: shellPage.FilesystemViewModel.WorkingDirectory;
936936

937-
// Launch terminal application if possible
938-
foreach (var terminal in App.TerminalController.Model.Terminals)
937+
if (await LaunchApplicationFromPath(currentInput, workingDir))
939938
{
940-
if (terminal.Path.Equals(currentInput, StringComparison.OrdinalIgnoreCase)
941-
|| terminal.Path.Equals(currentInput + ".exe", StringComparison.OrdinalIgnoreCase) || terminal.Name.Equals(currentInput, StringComparison.OrdinalIgnoreCase))
942-
{
943-
var connection = await AppServiceConnectionHelper.Instance;
944-
if (connection != null)
945-
{
946-
var value = new ValueSet()
947-
{
948-
{ "Arguments", "LaunchApp" },
949-
{ "WorkingDirectory", workingDir },
950-
{ "Application", terminal.Path },
951-
{ "Parameters", string.Format(terminal.Arguments, workingDir) }
952-
};
953-
await connection.SendMessageAsync(value);
954-
}
955-
return;
956-
}
939+
return;
957940
}
958941

959-
await LaunchApplicationFromPath(currentInput, workingDir);
960-
961942
try
962943
{
963944
if (!await Launcher.LaunchUriAsync(new Uri(currentInput)))
@@ -979,7 +960,7 @@ await DialogDisplayHelper.ShowDialogAsync("InvalidItemDialogTitle".GetLocalized(
979960
}
980961
}
981962

982-
private static async Task LaunchApplicationFromPath(string currentInput, string workingDir)
963+
private static async Task<bool> LaunchApplicationFromPath(string currentInput, string workingDir)
983964
{
984965
var trimmedInput= currentInput.Trim();
985966
var fileName = trimmedInput;
@@ -1001,8 +982,11 @@ private static async Task LaunchApplicationFromPath(string currentInput, string
1001982
{ "Application", fileName },
1002983
{ "Parameters", arguments }
1003984
};
1004-
await connection.SendMessageAsync(value);
985+
var (status, response) = await connection.SendMessageForResponseAsync(value);
986+
return status == Windows.ApplicationModel.AppService.AppServiceResponseStatus.Success && response.Get("Success", false);
1005987
}
988+
989+
return false;
1006990
}
1007991

1008992
public async void SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage shellpage, int maxSuggestions = 7)

0 commit comments

Comments
 (0)