-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm6.cs
155 lines (141 loc) · 6.05 KB
/
Form6.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowkillSeedFinderGUI {
public partial class LookingForSeed : Form {
private Process? runningProcess; // Declare as nullable
private static TaskCompletionSource<bool> waitForStringTask = new TaskCompletionSource<bool>();
private string lastOutputLine = "";
private List<string> outputLines = new List<string>();
public static MiscWindow? Instance { get; private set; }
private MainWindow? mainWindow;
public SeedFound? seedFound;
public LookingForSeed() {
InitializeComponent();
this.FormClosing += MainForm_FormClosing;
Application.ApplicationExit += Application_ApplicationExit;
}
public async void StartLookingForSeed(string arguments) {
string exeName = "WindowkillSeedFinderGUI.main.exe";
string dllName = "WindowkillSeedFinderGUI.main.dll";
string tempPath = Path.Combine(Path.GetTempPath(), "main.exe");
string tempDLLPath = Path.Combine(Path.GetTempPath(), "main.dll");
Debug.WriteLine((Assembly.GetExecutingAssembly().GetManifestResourceStream(exeName)));
try {
// Read the embedded .exe from resources
using (Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(exeName)) {
if (stream == null) {
MessageBox.Show("Error: Embedded exe not found!");
return;
}
// Write to a temp file
using (FileStream fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fileStream);
}
}
using (Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(dllName)) {
if (stream == null) {
MessageBox.Show("Error: Embedded dll not found!");
return;
}
using (FileStream fileStream = new FileStream(tempDLLPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fileStream);
}
}
LabelStatus.Text = "Starting program...";
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = tempPath,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
runningProcess = new Process
{
StartInfo = psi
};
outputLines.Clear();
runningProcess.OutputDataReceived += (sender, e) =>
{
if (e.Data != null) {
lastOutputLine = e.Data; // Store last received line
outputLines.Add(e.Data); // Store full output
// Update LabelStatus on the UI thread
this.Invoke((System.Windows.Forms.MethodInvoker)delegate {
LabelStatus.Text = lastOutputLine;
});
}
};
runningProcess.Start();
runningProcess.BeginOutputReadLine();
runningProcess.BeginErrorReadLine();
await Task.Run(() => runningProcess.WaitForExit()); // Wait for process to finish
if (!hasKilledProcess) {
seedFound.Show();
seedFound.Populate(outputLines);
this.Hide();
}
hasKilledProcess = false;
}
catch (Exception ex) {
MessageBox.Show("Error: " + ex.Message);
}
finally {
if (File.Exists(tempPath)) {
try { File.Delete(tempPath); } catch { } // Silent fail
}
if (File.Exists(tempDLLPath)) {
try { File.Delete(tempDLLPath); } catch { } // Silent fail
}
}
}
private void LookingForSeed_Load(object sender, EventArgs e) {
mainWindow = Application.OpenForms["Form1"] as MainWindow;
}
private void MainForm_FormClosing(object? sender, FormClosingEventArgs e) {
e.Cancel = true; // Cancel the close event
this.Hide(); // Hide the form instead
if (runningProcess != null && !runningProcess.HasExited) {
runningProcess.Kill();
runningProcess.WaitForExit();
hasKilledProcess = true;
}
}
private void Application_ApplicationExit(object? sender, EventArgs e) {
if (runningProcess != null && !runningProcess.HasExited) {
runningProcess.Kill();
runningProcess.WaitForExit();
}
}
private void LookingForSeed_SizeChanged(object sender, EventArgs e) {
}
private async void LookingForSeed_FormClosing(object sender, FormClosingEventArgs e) {
if (!isClosing) {
isClosing = true;
List<Form> openForms = Application.OpenForms.Cast<Form>()
.Reverse()
.ToList();
foreach (Form form in openForms) {
await Task.Delay(100);
form.BringToFront();
if (!form.IsDisposed) {
form.Close();
form.Hide();
}
}
}
}
bool isClosing = false;
private bool hasKilledProcess = false;
}
}