-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoWinInstaller.cpp
More file actions
268 lines (248 loc) · 9.45 KB
/
AutoWinInstaller.cpp
File metadata and controls
268 lines (248 loc) · 9.45 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <conio.h>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Global variables
void WriteImageFile();
void DiskPartionSetup();
string writeDiskScript(string content);
void commandRun(string cmd);
string bcdboot = "%systemroot%\\System32\\bcdboot.exe";
string diskpart = "%systemroot%\\System32\\diskpart.exe";
string dism = "%systemroot%\\System32\\dism.exe";
string app_title = "Automated Windows Installer";
int main() {
SetConsoleTitleA(app_title.c_str());
cout << "Menu selection: \n";
cout << "[1] - Setup the disk\n";
cout << "[2] - Write the image file\n";
cout << "[3] - Quit the application\n";
cout << "Enter the number: ";
while (true) {
int option = _getch();
switch (option) {
case '1':
system("cls");
DiskPartionSetup();
break;
case '2':
system("cls");
WriteImageFile();
break;
case '3':
exit(-1);
break;
default:
cout << "Invalid option! Please try again!\n";
system("pause>nul");
system("cls");
main();
break;
}
}
}
void DiskPartionSetup() {
// Variables
string disk_number;
// Warning
cout << "Make sure that you have backed up any important data from your hard disk before you continue forward!\n";
cout << "If you are ready to begin, press the enter key!\n";
if (cin.ignore()) {
// Step 1
commandRun(diskpart + " /s ./" + writeDiskScript(
"list disk\n"
"exit"
));
cout << "Enter the disk number from above: ";
cin >> disk_number;
// Step 2
cout << "Will you be using GPT or MBR for your partition table? [m/g]";
while (true) {
int disk_type = _getch();
switch (disk_type) {
case 'm':
case 'M':
commandRun(diskpart + " /s " + writeDiskScript(
"select disk " + disk_number + "\n"
"clean\n"
"convert mbr\n"
"exit"
));
cout << "\n";
cout << "Creating the required partitons! Please wait a moment!";
commandRun(diskpart + " /s " + writeDiskScript(
"select disk " + disk_number + "\n"
"create part primary size=100\n"
"format fs=ntfs label=SYSTEM quick\n"
"assign letter w\n"
"active\n"
"create par primary\n"
"format fs=ntfs label=Windows quick\n"
"assign letter r\n"
"exit"
));
system("cls");
cout << "All done with this step! You can now write the image file! Make sure that you remember these drive letters for later!\n";
cout << "Boot partition: W:\n";
cout << "Windows partition: R:\n";
remove("./diskpart_script.txt");
system("pause>nul");
system("cls");
main();
break;
case 'g':
case 'G':
commandRun(diskpart + " /s " + writeDiskScript(
"select disk " + disk_number + "\n"
"clean\n"
"convert gpt\n"
"exit"
));
cout << "\n";
cout << "Creating the required partitons! Please wait a moment!";
commandRun(diskpart + " /s " + writeDiskScript(
"select disk " + disk_number + "\n"
"create part EFI size=500\n"
"format fs=fat32 label=EFI quick\n"
"assign letter w\n"
"create par primary\n"
"format fs=ntfs label=Windows quick\n"
"assign letter r\n"
"exit"
));
system("cls");
cout << "All done with this step! You can now write the image file! Make sure that you remember these drive letters for later!\n";
cout << "Boot partition: W:\n";
cout << "Windows partition: R:\n";
remove("./diskpart_script.txt");
system("pause>nul");
system("cls");
main();
break;
default:
system("cls");
main();
break;
}
}
}
}
void WriteImageFile() {
int indexImg;
string imgFile;
cout << "Enter the WIM file path: ";
cin >> imgFile;
ifstream file(imgFile);
// Checks if the image file exists
if (file.is_open()) {
// Grabs the data from the image file and asks for the index number
commandRun(dism + " /Get-WimInfo /WimFile:" + imgFile + "");
cout << "Input the index number from above: ";
cin >> indexImg;
if (indexImg == NULL) {
cout << "The number you typed in is either invaild or is not a number! Please try again! Exiting the program!";
system("pause>nul");
exit(-1);
}
// Asks if the disk partition scheme is MBR or GPT
cout << "Are you using either MBR or GPT for the disk partition scheme? [m/g] ";
while (true) {
int win_type = _getch();
switch (win_type) {
case 'm':
case 'M': {
string winPar;
string sysresPar;
cout << "\n";
cout << "Input the Windows partition letter: ";
cin >> winPar;
cout << "Input the System Reserved partition letter: ";
cin >> sysresPar;
system("cls");
cout << "Starting the image writing! Please do not close this window while the writing is in progress!\n";
commandRun(dism + " /apply-image /imagefile:" + imgFile + " /index:" + to_string(indexImg) + " /applydir:" + winPar + ":""\"");
cout << "\n";
cout << "Adding the required boot files. Please wait!\n";
commandRun(bcdboot + " " + winPar + ":""\\Windows"" /s " + sysresPar + ":"" /f BIOS");
system("cls");
cout << "Image write was successful! Press the enter key to go back to the main menu!\n";
system("pause>nul");
system("cls");
main();
break;
}
case 'g':
case 'G': {
string efiPar;
string winPar;
cout << "\n";
cout << "Input the EFI partiton letter: ";
cin >> efiPar;
cout << "Input the Windows partition letter: ";
cin >> winPar;
system("cls");
cout << "Starting the image writing! Please do not close this window while the writing is in progress!\n";
commandRun(dism + " /apply-image /imagefile:" + imgFile + " /index:" + to_string(indexImg) + " /applydir:" + winPar + ":""\"");
cout << "\n";
cout << "Adding the required boot files. Please wait!\n";
commandRun(bcdboot + " " + winPar + ":""\\Windows"" /s " + efiPar + ":"" /f UEFI");
system("cls");
cout << "Image write was successful! Press the enter key to go back to the main menu!\n";
system("pause>nul");
system("cls");
main();
break;
}
default:
system("cls");
main();
break;
}
}
}
else {
cout << "The image file does not exist! Please try again!";
system("pause>nul");
system("cls");
WriteImageFile();
}
}
// Helper functions
void commandRun(string cmd) {
int runCmd = system(cmd.c_str());
if (runCmd != 0) {
cout << "\n";
cout << "Something went wrong while trying to run this command!\n";
cout << "Press any key to exit the program\n";
system("pause>nul");
exit(-1);
}
}
string writeDiskScript(string content) {
string file_name = "./diskpart_script.txt";
ofstream scriptFile(file_name);
if (scriptFile.is_open()) {
scriptFile << content.c_str();
scriptFile.close();
return file_name;
}
else {
perror("Something went wrong while trying to create the diskpart script! Exiting now!");
system("pause>nul");
exit(-1);
}
}
//char tempPath[MAX_PATH];
//char tempFileName[MAX_PATH];
//UINT uniqueNum;
//GetTempPathA(MAX_PATH, tempPath);
//uniqueNum = GetTempFileNameA(tempPath, "tempScript", 0, tempFileName);
//DeleteFileA(tempFileName);