mirror-ac/test/cli/main.cpp

57 lines
1.3 KiB
C++
Raw Normal View History

2023-10-31 18:10:18 +01:00
#include <iostream>
#include <string>
#include <vector>
#include <string_view>
#include <Windows.h>
#include <tlhelp32.h>
2023-11-01 10:48:14 +01:00
std::wstring cstr_to_wstr(std::string cstr)
{
return std::wstring(cstr.begin(), cstr.end());
}
2023-11-18 11:40:22 +01:00
DWORD get_proc_id_by_name(const std::string& process_name)
2023-10-31 18:10:18 +01:00
{
PROCESSENTRY32 entry = { 0 };
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
while (Process32Next(snapshot, &entry))
{
2023-11-01 10:48:14 +01:00
if (entry.szExeFile == cstr_to_wstr(process_name))
2023-10-31 18:10:18 +01:00
{
return entry.th32ProcessID;
}
}
CloseHandle(snapshot);
2023-11-01 10:48:14 +01:00
return 0;
2023-10-31 18:10:18 +01:00
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
2023-11-01 10:48:14 +01:00
std::cerr << "Please enter a valid Process Name";
2023-10-31 18:10:18 +01:00
return EXIT_FAILURE;
}
const std::vector<std::string_view> args(argv + 1, argv + argc);
2023-11-01 10:48:14 +01:00
std::string process_name = std::string(args[0].data());
DWORD proc_id = get_proc_id_by_name(process_name);
2023-10-31 18:10:18 +01:00
2023-11-01 10:48:14 +01:00
if (!proc_id)
2023-10-31 18:10:18 +01:00
{
2023-11-01 10:48:14 +01:00
std::cerr << "Process does not exist, please enter a valid running process name." << std::endl;
2023-10-31 18:10:18 +01:00
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}