-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileName.cpp
39 lines (32 loc) · 1.03 KB
/
FileName.cpp
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
#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
int main() {
while (true) {
std::cout << "MyShell> ";
std::string input;
std::getline(std::cin, input);
if (input == "exit") {
break; // Exit the loop if the user types "exit"
}
// Parse the command and arguments
std::stringstream ss(input);
std::string command;
ss >> command;
// Convert narrow string to wide string
std::wstring wideCommand(command.begin(), command.end());
// Execute the command using CreateProcess
STARTUPINFOW si = {};
PROCESS_INFORMATION pi = {};
if (CreateProcessW(NULL, const_cast<LPWSTR>(wideCommand.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
std::cerr << "Error executing command." << std::endl;
}
}
return 0;
}