-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cpp
More file actions
203 lines (183 loc) · 4.64 KB
/
Copy pathMainForm.cpp
File metadata and controls
203 lines (183 loc) · 4.64 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#pragma once
#include "MainForm.h"
#include "sstream"
using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace DrvLoader;
inline Void MainForm::DisplayException(RSTATUS^ ex)
{
MessageBox::Show(
ex,
TEXT("错误"),
MessageBoxButtons::OK,
MessageBoxIcon::Error);
LOG(ex);
}
MainForm::MainForm(array<String^>^ args)
{
InitializeComponent();
try
{
ThrowIfFailed(SrvUtils::OpenSCM());
} CatchDisplay(
delete ret; \
ret = nullptr; \
// 防止内存泄露
System::Environment::Exit(0););
ActiveControl = txtSrvName;
LOG("欢迎使用DrvLoader");
if (args->Length > 0)
{
txtDrvPath->Text = args[0];
LOG("。\r\n");
}
else
{
LOG(",请选择驱动程序文件/输入服务名。\r\n");
}
}
MainForm::~MainForm()
{
if (components)
{
delete components;
}
}
System::Void MainForm::MainForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)
{
LOG("正在结束程序……");
if (chkAutoUnload->Checked)
{
SrvUtils::Clear();
}
}
System::Void MainForm::MainForm_DragEnter(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
if (e->Data->GetDataPresent(DataFormats::FileDrop))
{
e->Effect = DragDropEffects::Link;
}
else
{
e->Effect = DragDropEffects::None;
}
}
System::Void MainForm::MainForm_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
txtDrvPath->Text = cli::safe_cast<Array^>(e->Data->GetData(DataFormats::FileDrop))->GetValue(0)->ToString();
}
System::Void MainForm::btnBrow_Click(System::Object^ sender, System::EventArgs^ e)
{
openFileDialog->InitialDirectory = "C:\\";
openFileDialog->Filter = "所有文件|*.*";
if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
txtDrvPath->Text = openFileDialog->FileName;
}
}
System::Void MainForm::btnInst_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
LOG("正在安装驱动……\r\n");
ThrowIfFailed(SrvUtils::Create(drvPath, srvName));
LOG("安装成功。服务名:{0}\r\n", srvName);
} CatchDisplay();
}
System::Void MainForm::btnStart_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
LOG("正在启动服务 {0} ……\r\n", srvName);
ThrowIfFailed(SrvUtils::Start(srvName));
LOG("服务 {0} 已启动。\r\n", srvName);
} CatchDisplay();
}
System::Void MainForm::btnStop_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
LOG("正在停止服务 {0} ……\r\n", srvName);
ThrowIfFailed(SrvUtils::Stop(srvName));
LOG("服务 {0} 已停止。\r\n", srvName);
} CatchDisplay();
}
System::Void MainForm::btnDel_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
LOG("正在删除服务 {0} ……\r\n", srvName);
auto ret = (gcnew RSTATUS(SrvUtils::Delete(srvName)));
switch (ret->ExitCode)
{
case ERROR_SUCCESS:
LOG("服务 {0} 已删除。\r\n", srvName);
break;
case ERROR_NEED_CONFIRM:
{
const System::Windows::Forms::DialogResult dialogResult =
MessageBox::Show(
String::Format("指定的服务({0})似乎不是由本程序创建的。确定要删除吗?", srvName),
"提示",
MessageBoxButtons::OKCancel,
MessageBoxIcon::Question);
if (dialogResult == System::Windows::Forms::DialogResult::OK)
{
try
{
ThrowIfFailed(SrvUtils::Delete(srvName, true));
LOG("服务 {0} 已删除。\r\n", srvName);
break;
} CatchDisplay();
}
else
{
LOG("删除取消。\r\n");
break;
}
}
default:
throw ret;
break;
}
delete ret;
ret = nullptr;
} CatchDisplay();
}
System::Void MainForm::txtSrvName_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
srvName = txtSrvName->Text;
btnInst->Enabled = drvPath.Length != 0 && srvName.Length != 0;
// 我也不知道为什么不可以连续赋值
// 可能是因为编译器把属性的set当做函数调用
// 而set没有返回值
btnLookup->Enabled = srvName.Length != 0;
btnStart->Enabled = srvName.Length != 0;
btnStop->Enabled = srvName.Length != 0;
btnDel->Enabled = srvName.Length != 0;
}
System::Void MainForm::txtDrvPath_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
drvPath = txtDrvPath->Text;
txtSrvName->Text = Path::GetFileNameWithoutExtension(drvPath);
}
System::Void MainForm::btnLookup_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
LOG("正在查询……\r\n");
auto ret = (gcnew RSTATUS(SrvUtils::Lookup(srvName)));
if (!ret->Success)
{
throw ret;
}
LOG(ret);
delete ret;
ret = nullptr;
} CatchDisplay();
}