This repository was archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGDXD.cpp
More file actions
104 lines (86 loc) · 2.73 KB
/
GDXD.cpp
File metadata and controls
104 lines (86 loc) · 2.73 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
#include <iostream>
#include <string>
#include <filesystem>
#include <cstdlib>
#include <windows.h>
#include <stdexcept>
#include <thread>
#include <chrono>
namespace Utility {
std::string getName(const std::string& filePath) {
try {
std::string name = std::filesystem::path(filePath).filename().string();
size_t ext = name.find('.');
if (ext == std::string::npos) {
throw std::runtime_error("Unable to extract base name from the file name.");
}
return name.substr(0, ext);
} catch (const std::exception& e) {
throw std::runtime_error("Error get name: " + std::string(e.what()));
}
}
bool isRunning(const std::string& process) {
std::string command = "tasklist /FI \"IMAGENAME eq " + process + ".exe\"";
FILE* pipe = _popen(command.c_str(), "r");
if (!pipe) {
throw std::runtime_error("Failed to open pipe for tasklist command.");
}
std::string output;
char buffer[128];
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
output += buffer;
}
_pclose(pipe);
return output.find(process + ".exe") != std::string::npos;
}
int execute(const std::string& command) {
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION process;
if (!CreateProcess(
nullptr,
const_cast<char*>(command.c_str()),
nullptr,
nullptr,
FALSE,
0,
nullptr,
nullptr,
&info,
&process
)) {
throw std::runtime_error("Failed to create process. Error code: " + std::to_string(GetLastError()));
}
WaitForSingleObject(process.hProcess, INFINITE);
DWORD exitCode;
if (!GetExitCodeProcess(process.hProcess, &exitCode)) {
CloseHandle(process.hProcess);
CloseHandle(process.hThread);
throw std::runtime_error("Failed to retrieve process exit code.");
}
CloseHandle(process.hProcess);
CloseHandle(process.hThread);
return static_cast<int>(exitCode);
}
} // Utility
int main(int argc, char* argv[]) {
try {
if (argc < 1) {
throw std::invalid_argument("No executable path provided.");
}
std::string path = argv[0];
std::string name = Utility::getName(path);
if (Utility::isRunning(name)) {
std::cerr << "Error: The program is already running." << std::endl;
return 1;
}
std::string exe = name + ".exe";
if (!std::filesystem::exists(exe)) {
throw std::runtime_error( exe + " not found.");
}
std::string command = exe + " -v -d";
return Utility::execute(command);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}