12
12
using System . Threading . Tasks ;
13
13
using Vanara . PInvoke ;
14
14
using Vanara . Windows . Shell ;
15
+ using Windows . Foundation . Collections ;
15
16
16
17
namespace FilesFullTrust . MessageHandlers
17
18
{
@@ -21,7 +22,7 @@ public void Initialize(PipeStream connection)
21
22
{
22
23
}
23
24
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 )
25
26
{
26
27
switch ( arguments )
27
28
{
@@ -38,12 +39,11 @@ public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object
38
39
if ( message . ContainsKey ( "Application" ) )
39
40
{
40
41
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 ) ) ;
47
47
}
48
48
break ;
49
49
@@ -53,22 +53,13 @@ public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object
53
53
var afPath = Path . Combine ( Path . GetTempPath ( ) , "CompatibilityTroubleshooterAnswerFile.xml" ) ;
54
54
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 ) ) ;
55
55
message [ "Parameters" ] = $ "/id PCWDiagnostic /af \" { afPath } \" ";
56
- HandleApplicationLaunch ( "msdt.exe" , message ) ;
56
+ await HandleApplicationLaunch ( "msdt.exe" , message ) ;
57
57
}
58
58
break ;
59
59
}
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
- }
69
60
}
70
61
71
- private async void HandleApplicationLaunch ( string application , Dictionary < string , object > message )
62
+ private async Task < bool > HandleApplicationLaunch ( string application , Dictionary < string , object > message )
72
63
{
73
64
var arguments = message . Get ( "Parameters" , "" ) ;
74
65
var workingDirectory = message . Get ( "WorkingDirectory" , "" ) ;
@@ -77,8 +68,7 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
77
68
if ( new [ ] { ".vhd" , ".vhdx" } . Contains ( Path . GetExtension ( application ) . ToLowerInvariant ( ) ) )
78
69
{
79
70
// Use powershell to mount vhds as this requires admin rights
80
- Win32API . MountVhdDisk ( application ) ;
81
- return ;
71
+ return Win32API . MountVhdDisk ( application ) ;
82
72
}
83
73
84
74
try
@@ -123,6 +113,7 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
123
113
process . StartInfo . WorkingDirectory = workingDirectory ;
124
114
process . Start ( ) ;
125
115
Win32API . BringToForeground ( currentWindows ) ;
116
+ return true ;
126
117
}
127
118
catch ( Win32Exception )
128
119
{
@@ -136,12 +127,13 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
136
127
{
137
128
process . Start ( ) ;
138
129
Win32API . BringToForeground ( currentWindows ) ;
130
+ return true ;
139
131
}
140
132
catch ( Win32Exception )
141
133
{
142
134
try
143
135
{
144
- await Win32API . StartSTATask ( ( ) =>
136
+ return await Win32API . StartSTATask ( ( ) =>
145
137
{
146
138
var split = application . Split ( '|' ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) . Select ( x => GetMtpPath ( x ) ) ;
147
139
if ( split . Count ( ) == 1 )
@@ -172,16 +164,19 @@ await Win32API.StartSTATask(() =>
172
164
catch ( Win32Exception )
173
165
{
174
166
// Cannot open file (e.g DLL)
167
+ return false ;
175
168
}
176
169
catch ( ArgumentException )
177
170
{
178
171
// Cannot open file (e.g DLL)
172
+ return false ;
179
173
}
180
174
}
181
175
}
182
176
catch ( InvalidOperationException )
183
177
{
184
178
// Invalid file path
179
+ return false ;
185
180
}
186
181
}
187
182
0 commit comments