File tree 2 files changed +58
-0
lines changed
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ sealed class InjectedProgram : IInjectedProgram
2
+ {
3
+ static readonly TimeSpan _timeout = TimeSpan . FromMinutes ( 1 ) ;
4
+
5
+ public static async Task < int > RunAsync ( InjectedProgramContext context , ReadOnlyMemory < string > args )
6
+ {
7
+ if ( context . InjectorProcessId != null )
8
+ return 42 ;
9
+
10
+ Console . WriteLine ( "Starting conhost.exe..." ) ;
11
+
12
+ using var proc = new Process ( )
13
+ {
14
+ StartInfo = new ( )
15
+ {
16
+ FileName = "conhost.exe" ,
17
+ } ,
18
+ } ;
19
+
20
+ _ = proc . Start ( ) ;
21
+
22
+ try
23
+ {
24
+ if ( ! proc . WaitForInputIdle ( _timeout ) )
25
+ throw new TimeoutException ( ) ;
26
+
27
+ Console . WriteLine ( "Started as {0}. Injecting..." , proc . Id ) ;
28
+
29
+ using var target = TargetProcess . Open ( proc . Id ) ;
30
+ using var injector = new AssemblyInjector (
31
+ target ,
32
+ new AssemblyInjectorOptions ( typeof ( InjectedProgram ) . Assembly . Location )
33
+ . WithInjectionTimeout ( _timeout )
34
+ . WithCompletionTimeout ( _timeout ) ) ;
35
+
36
+ await injector . InjectAssemblyAsync ( ) ;
37
+
38
+ Console . WriteLine ( "Injected." ) ;
39
+
40
+ var code = await injector . WaitForCompletionAsync ( ) ;
41
+
42
+ Console . WriteLine ( "Returned code {0}." , code ) ;
43
+
44
+ return code == 42 ? 0 : 1 ;
45
+ }
46
+ finally
47
+ {
48
+ proc . Kill ( true ) ;
49
+
50
+ await proc . WaitForExitAsync ( ) ;
51
+ }
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+ <PropertyGroup >
3
+ <SelfContained >false</SelfContained >
4
+ </PropertyGroup >
5
+ </Project >
You can’t perform that action at this time.
0 commit comments