Skip to content

Commit 549e91c

Browse files
committed
Refactor and enhance disk setup and image writing
Reorganized main menu logic and split disk setup and image writing into dedicated functions. Added support for automated disk partitioning using diskpart scripts for both MBR and GPT, with improved user prompts and error handling. Updated project settings to require administrator privileges and use multi-threaded runtime.
1 parent b27d0f5 commit 549e91c

2 files changed

Lines changed: 194 additions & 90 deletions

File tree

AutoWinInstaller.cpp

Lines changed: 192 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,137 @@
88
#include <sstream>
99
#include <string>
1010
#include <fstream>
11+
#include <vector>
1112

1213
using namespace std;
1314

14-
void WriteImg();
15+
void WriteImageFile();
16+
void DiskPartionSetup();
17+
string writeDiskScript(string content);
1518
void commandRun(string cmd);
16-
void createFile(string file, string text);
1719

1820
int main() {
19-
int option;
2021
SetConsoleTitle(L"Automated Windows Installer");
21-
22-
cout << "Welcome to the Automated Windows Installer!\n";
23-
cout << "Created by SSP6904 (https://shaunhoffer.cc)\n";
24-
cout << "Select one of the options below to get started!\n";
25-
cout << "1 - Setup the disk\n";
26-
cout << "2 - Write the image file\n";
27-
cout << "3 - Quit the application\n";
22+
cout << "Menu selection: \n";
23+
cout << "[1] - Setup the disk\n";
24+
cout << "[2] - Write the image file\n";
25+
cout << "[3] - Quit the application\n";
2826
cout << "Enter the number: ";
2927
while (true) {
30-
option = _getch();
31-
if (option == '1') {
32-
system("cls");
33-
cout << "Check the README for how you can setup your disk!\n";
34-
system("diskpart");
35-
cout << "\n";
36-
cout << "Please be sure that you know what your drive letters that you set for your disk setup! \n";
37-
cout << "If you know what they are, you may exit this step!";
38-
cin.ignore();
39-
system("cls");
40-
main();
41-
}
42-
else if (option == '2') {
43-
system("cls");
44-
WriteImg();
45-
}
46-
else if (option == '3') {
47-
exit(-1);
48-
}
49-
else {
50-
cout << "Invalid option! Please try again!";
51-
cin.ignore();
52-
system("cls");
53-
main();
28+
int option = _getch();
29+
switch (option) {
30+
case '1':
31+
system("cls");
32+
DiskPartionSetup();
33+
break;
34+
case '2':
35+
system("cls");
36+
WriteImageFile();
37+
break;
38+
case '3':
39+
exit(-1);
40+
break;
41+
default:
42+
cout << "Invalid option! Please try again!";
43+
cin.ignore();
44+
system("cls");
45+
main();
46+
break;
5447
}
5548
}
5649
}
5750

58-
void commandRun(string cmd) {
59-
int runCmd = system(cmd.c_str());
60-
if (runCmd != 0) {
61-
cout << "Something went wrong while trying to run this command! Sorry about that! If this is an issue with the program, please submit it to this program's GitHub repository!";
62-
cout << "Press any key to exit the program";
63-
cin.ignore();
64-
exit(-1);
51+
void DiskPartionSetup() {
52+
// Variables
53+
string disk_number;
54+
55+
// Warning
56+
cout << "Make sure that you have backed up any important data from your hard disk before you continue forward!\n";
57+
cout << "If you are ready to begin, press the enter key!\n";
58+
if (cin.ignore()) {
59+
// Step 1
60+
commandRun("diskpart /s ./" + writeDiskScript(
61+
"list disk\n"
62+
"exit"
63+
));
64+
cout << "Enter the disk number from above: ";
65+
cin >> disk_number;
66+
67+
// Step 2
68+
cout << "Will you be using GPT or MBR for your partition table? [m/g]";
69+
while (true) {
70+
int disk_type = _getch();
71+
switch (disk_type) {
72+
case 'm':
73+
case 'M':
74+
commandRun("diskpart /s " + writeDiskScript(
75+
"select disk " + disk_number + "\n"
76+
"clean\n"
77+
"convert mbr\n"
78+
"exit"
79+
));
80+
cout << "\n";
81+
cout << "Creating the required partitons! Please wait a moment!";
82+
commandRun("diskpart /s " + writeDiskScript(
83+
"select disk " + disk_number + "\n"
84+
"create part primary size=100\n"
85+
"format fs=ntfs label=SYSTEM quick\n"
86+
"assign letter w\n"
87+
"active\n"
88+
"create par primary\n"
89+
"format fs=ntfs label=Windows quick\n"
90+
"assign letter r\n"
91+
"exit"
92+
));
93+
system("cls");
94+
cout << "All done with this step! You can now write the image file! Make sure that you remember these drive letters for later!\n";
95+
cout << "Boot partition: W:\n";
96+
cout << "Windows partition: R:\n";
97+
remove("./diskpart_script.txt");
98+
system("pause>nul");
99+
system("cls");
100+
main();
101+
break;
102+
case 'g':
103+
case 'G':
104+
commandRun("diskpart /s " + writeDiskScript(
105+
"select disk " + disk_number + "\n"
106+
"clean\n"
107+
"convert gpt\n"
108+
"exit"
109+
));
110+
cout << "\n";
111+
cout << "Creating the required partitons! Please wait a moment!";
112+
commandRun("diskpart /s " + writeDiskScript(
113+
"select disk " + disk_number + "\n"
114+
"create part EFI size=500\n"
115+
"format fs=fat32 label=EFI quick\n"
116+
"assign letter w\n"
117+
"create par primary\n"
118+
"format fs=ntfs label=Windows quick\n"
119+
"assign letter r\n"
120+
"exit"
121+
));
122+
system("cls");
123+
cout << "All done with this step! You can now write the image file! Make sure that you remember these drive letters for later!\n";
124+
cout << "Boot partition: W:\n";
125+
cout << "Windows partition: R:\n";
126+
remove("./diskpart_script.txt");
127+
system("pause>nul");
128+
system("cls");
129+
main();
130+
break;
131+
default:
132+
system("cls");
133+
main();
134+
break;
135+
}
136+
}
65137
}
66138
}
67139

68-
void WriteImg() {
140+
void WriteImageFile() {
69141
int indexImg;
70-
char gptan;
71142
string imgFile;
72143

73144
cout << "Enter the WIM file path: ";
@@ -91,64 +162,95 @@ void WriteImg() {
91162
cout << "Are you using either MBR or GPT for the disk partition scheme? [m/g] ";
92163

93164
while (true) {
94-
gptan = _getch();
95-
// Choose this if the partion scheme is GPT
96-
if (gptan == 'g') {
97-
string efiPar;
98-
string winPar;
99-
cout << "\n";
100-
cout << "Input the EFI partiton letter: ";
101-
cin >> efiPar;
102-
cout << "Input the Windows partition letter: ";
103-
cin >> winPar;
104-
cout << "Starting the image writing! Please do not close this window while the writing is in progress!\n";
105-
commandRun("dism /apply-image /imagefile:" + imgFile + " /index:" + to_string(indexImg) + " /applydir:" + winPar + ":""\"");
106-
cout << "Adding the required boot files. Please wait!\n";
107-
commandRun("bcdboot " + winPar + ":""\\Windows"" /s " + efiPar + ":"" /f UEFI");
108-
system("cls");
109-
cout << "Image write was successful! Press the enter key to go back to the main menu!";
165+
int win_type = _getch();
166+
switch (win_type) {
167+
case 'm':
168+
case 'M': {
169+
string winPar;
170+
string sysresPar;
171+
cout << "\n";
172+
cout << "Input the Windows partition letter: ";
173+
cin >> winPar;
174+
cout << "Input the System Reserved partition letter: ";
175+
cin >> sysresPar;
176+
cout << "Starting the image writing! Please do not close this window while the writing is in progress!\n";
177+
commandRun("dism /apply-image /imagefile:" + imgFile + " /index:" + to_string(indexImg) + " /applydir:" + winPar + ":""\"");
178+
cout << "Adding the required boot files. Please wait!\n";
179+
commandRun("bcdboot " + winPar + ":""\\Windows"" /s " + sysresPar + ":"" /f BIOS");
180+
system("cls");
181+
cout << "Image write was successful! Press the enter key to go back to the main menu!";
110182

111-
cin.ignore();
112-
system("cls");
113-
main();
114-
}
115-
// Choose this if the partion scheme is MBR
116-
else if (gptan == 'm') {
117-
string winPar;
118-
string sysresPar;
119-
cout << "\n";
120-
cout << "Input the Windows partition letter: ";
121-
cin >> winPar;
122-
cout << "Input the System Reserved partition letter: ";
123-
cin >> sysresPar;
124-
cout << "Starting the image writing! Please do not close this window while the writing is in progress!\n";
125-
commandRun("dism /apply-image /imagefile:" + imgFile + " /index:" + to_string(indexImg) + " /applydir:" + winPar + ":""\"");
126-
cout << "Adding the required boot files. Please wait!\n";
127-
commandRun("bcdboot " + winPar + ":""\\Windows"" /s " + sysresPar + ":"" /f BIOS");
128-
system("cls");
129-
cout << "Image write was successful! Press the enter key to go back to the main menu!";
183+
cin.ignore();
184+
system("cls");
185+
main();
186+
break;
187+
}
188+
case 'g':
189+
case 'G': {
190+
string efiPar;
191+
string winPar;
192+
cout << "\n";
193+
cout << "Input the EFI partiton letter: ";
194+
cin >> efiPar;
195+
cout << "Input the Windows partition letter: ";
196+
cin >> winPar;
197+
cout << "Starting the image writing! Please do not close this window while the writing is in progress!\n";
198+
commandRun("dism /apply-image /imagefile:" + imgFile + " /index:" + to_string(indexImg) + " /applydir:" + winPar + ":""\"");
199+
cout << "Adding the required boot files. Please wait!\n";
200+
commandRun("bcdboot " + winPar + ":""\\Windows"" /s " + efiPar + ":"" /f UEFI");
201+
system("cls");
202+
cout << "Image write was successful! Press the enter key to go back to the main menu!";
130203

131-
cin.ignore();
132-
system("cls");
133-
main();
204+
cin.ignore();
205+
system("cls");
206+
main();
207+
break;
208+
}
209+
default:
210+
system("cls");
211+
main();
212+
break;
134213
}
135214
}
136215
}
137216
else {
138217
cout << "The image file does not exist! Please try again!";
139218
cin.ignore();
140219
system("cls");
141-
WriteImg();
220+
WriteImageFile();
221+
}
222+
}
223+
224+
// Helper functions
225+
void commandRun(string cmd) {
226+
int runCmd = system(cmd.c_str());
227+
if (runCmd != 0) {
228+
cout << "\n";
229+
cout << "Something went wrong while trying to run this command!\n";
230+
cout << "Press any key to exit the program";
231+
cin.ignore();
232+
exit(-1);
142233
}
143234
}
144235

145-
void createFile(string file, string text) {
146-
ofstream fileSel(file);
147-
fileSel << text;
148-
fileSel.close();
236+
string writeDiskScript(string content) {
237+
string file_name = "./diskpart_script.txt";
238+
ofstream scriptFile(file_name);
239+
if (scriptFile.is_open()) {
240+
scriptFile << content.c_str();
241+
scriptFile.close();
242+
return file_name;
243+
}
244+
else {
245+
perror("Something went wrong while trying to create the diskpart script! Exiting now!");
246+
cin.ignore();
247+
exit(-1);
248+
}
149249
}
150250

151-
//ofstream seldisk("seldisk.txt");
152-
//seldisk << "sel disk ", x;
153-
//seldisk.close();
154-
//std::cout << "Would you like to use MBR or GPT for your disk? (M/G)" << std::endl;
251+
//char tempPath[MAX_PATH];
252+
//char tempFileName[MAX_PATH];
253+
//UINT uniqueNum;
254+
//GetTempPathA(MAX_PATH, tempPath);
255+
//uniqueNum = GetTempFileNameA(tempPath, "tempScript", 0, tempFileName);
256+
//DeleteFileA(tempFileName);

AutoWinInstaller.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@
9090
<SDLCheck>true</SDLCheck>
9191
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9292
<ConformanceMode>true</ConformanceMode>
93+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
9394
</ClCompile>
9495
<Link>
9596
<SubSystem>Console</SubSystem>
9697
<EnableCOMDATFolding>true</EnableCOMDATFolding>
9798
<OptimizeReferences>true</OptimizeReferences>
9899
<GenerateDebugInformation>true</GenerateDebugInformation>
100+
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
99101
</Link>
100102
</ItemDefinitionGroup>
101103
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

0 commit comments

Comments
 (0)