mirror of
https://github.com/360NENZ/Taiga74164-Akebi-GC.git
synced 2025-09-19 20:26:20 +08:00
107 lines
3.0 KiB
C++
107 lines
3.0 KiB
C++
![]() |
#include "util.h"
|
||
|
|
||
|
#include <TlHelp32.h>
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <sstream>
|
||
|
|
||
|
//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
|
||
|
std::string GetLastErrorAsString()
|
||
|
{
|
||
|
//Get the error message ID, if any.
|
||
|
DWORD errorMessageID = ::GetLastError();
|
||
|
if (errorMessageID == 0) {
|
||
|
return std::string(); //No error message has been recorded
|
||
|
}
|
||
|
|
||
|
LPSTR messageBuffer = nullptr;
|
||
|
|
||
|
//Ask Win32 to give us the string version of that message ID.
|
||
|
//The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
|
||
|
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||
|
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
|
||
|
|
||
|
//Copy the error message into a std::string.
|
||
|
std::string message(messageBuffer, size);
|
||
|
|
||
|
//Free the Win32's string's buffer.
|
||
|
LocalFree(messageBuffer);
|
||
|
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
std::string* GetFilePathByUser() {
|
||
|
|
||
|
// common dialog box structure, setting all fields to 0 is important
|
||
|
OPENFILENAME ofn = { 0 };
|
||
|
TCHAR szFile[260] = { 0 };
|
||
|
|
||
|
// Initialize remaining fields of OPENFILENAME structure
|
||
|
ofn.lStructSize = sizeof(ofn);
|
||
|
ofn.hwndOwner = NULL;
|
||
|
ofn.lpstrFile = szFile;
|
||
|
ofn.nMaxFile = sizeof(szFile);
|
||
|
ofn.lpstrFilter = "Executable\0*.exe\0";
|
||
|
ofn.nFilterIndex = 1;
|
||
|
ofn.lpstrFileTitle = NULL;
|
||
|
ofn.nMaxFileTitle = 0;
|
||
|
ofn.lpstrInitialDir = NULL;
|
||
|
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
|
||
|
|
||
|
if (GetOpenFileName(&ofn) == TRUE)
|
||
|
{
|
||
|
return new std::string(szFile);
|
||
|
}
|
||
|
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
int FindProcessId(const std::string& processName) {
|
||
|
int pid = -1;
|
||
|
|
||
|
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||
|
PROCESSENTRY32 process;
|
||
|
ZeroMemory(&process, sizeof(process));
|
||
|
process.dwSize = sizeof(process);
|
||
|
|
||
|
if (Process32First(snapshot, &process))
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
if (std::string(process.szExeFile) == processName)
|
||
|
{
|
||
|
pid = process.th32ProcessID;
|
||
|
break;
|
||
|
}
|
||
|
} while (Process32Next(snapshot, &process));
|
||
|
}
|
||
|
|
||
|
CloseHandle(snapshot);
|
||
|
|
||
|
return pid;
|
||
|
}
|
||
|
|
||
|
void WaitForCloseProcess(const std::string& processName) {
|
||
|
int pid = FindProcessId(processName);
|
||
|
if (pid == -1)
|
||
|
return;
|
||
|
|
||
|
std::cout << "Found '" << processName << "' process. Waiting for closing..." << std::endl;
|
||
|
|
||
|
#ifdef _DEBUG
|
||
|
std::stringstream stream;
|
||
|
stream << "taskkill /F /T /IM " << processName;
|
||
|
int retval = system(stream.str().c_str());
|
||
|
|
||
|
std::cout << "Trying to kill process." << std::endl;
|
||
|
#endif
|
||
|
|
||
|
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
|
||
|
DWORD exitCode = 0;
|
||
|
while (hProc && (GetExitCodeProcess(hProc, &exitCode), exitCode == STILL_ACTIVE)) {
|
||
|
Sleep(1000);
|
||
|
}
|
||
|
|
||
|
if (hProc != NULL)
|
||
|
CloseHandle(hProc);
|
||
|
}
|