2015-11-06 11:55:21 +00:00
|
|
|
#include "Util.h"
|
2021-04-12 09:41:28 +00:00
|
|
|
#include "StdString.h"
|
|
|
|
#include "path.h"
|
2016-01-17 18:48:19 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 10:32:18 +00:00
|
|
|
#include <windows.h>
|
2015-11-06 11:55:21 +00:00
|
|
|
#include <Tlhelp32.h>
|
2016-01-17 18:48:19 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2016-04-28 07:36:39 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
2016-01-17 18:48:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pjutil::DynLibHandle pjutil::DynLibOpen(const char *pccLibraryPath, bool ShowErrors)
|
|
|
|
{
|
|
|
|
if (pccLibraryPath == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-28 07:36:39 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-17 18:48:19 +00:00
|
|
|
UINT LastErrorMode = SetErrorMode(ShowErrors ? 0 : SEM_FAILCRITICALERRORS);
|
2020-06-09 13:04:38 +00:00
|
|
|
pjutil::DynLibHandle lib = (pjutil::DynLibHandle)LoadLibraryA(pccLibraryPath);
|
2016-01-17 18:48:19 +00:00
|
|
|
SetErrorMode(LastErrorMode);
|
2016-04-28 07:36:39 +00:00
|
|
|
#else
|
|
|
|
pjutil::DynLibHandle lib = (pjutil::DynLibHandle)dlopen(pccLibraryPath, RTLD_NOW);
|
|
|
|
#endif
|
2016-01-17 18:48:19 +00:00
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * pjutil::DynLibGetProc(pjutil::DynLibHandle LibHandle, const char * ProcedureName)
|
|
|
|
{
|
|
|
|
if (ProcedureName == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2016-04-28 07:36:39 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-17 18:48:19 +00:00
|
|
|
return GetProcAddress((HMODULE)LibHandle, ProcedureName);
|
2016-04-28 07:36:39 +00:00
|
|
|
#else
|
|
|
|
return dlsym(LibHandle, ProcedureName);
|
|
|
|
#endif
|
2016-01-17 18:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pjutil::DynLibClose(pjutil::DynLibHandle LibHandle)
|
|
|
|
{
|
2016-04-28 07:36:39 +00:00
|
|
|
if (LibHandle != NULL)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
FreeLibrary((HMODULE)LibHandle);
|
|
|
|
#else
|
|
|
|
dlclose(LibHandle);
|
|
|
|
#endif
|
|
|
|
}
|
2016-01-17 18:48:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 11:55:21 +00:00
|
|
|
void pjutil::Sleep(uint32_t timeout)
|
|
|
|
{
|
2016-01-17 18:48:19 +00:00
|
|
|
#ifdef _WIN32
|
2015-11-06 11:55:21 +00:00
|
|
|
::Sleep(timeout);
|
2016-01-17 18:48:19 +00:00
|
|
|
#else
|
2016-04-28 07:36:39 +00:00
|
|
|
int was_error;
|
|
|
|
struct timespec elapsed, tv;
|
|
|
|
elapsed.tv_sec = timeout / 1000;
|
|
|
|
elapsed.tv_nsec = (timeout % 1000) * 1000000;
|
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
tv.tv_sec = elapsed.tv_sec;
|
|
|
|
tv.tv_nsec = elapsed.tv_nsec;
|
|
|
|
was_error = nanosleep(&tv, &elapsed);
|
|
|
|
} while (was_error && (errno == EINTR));
|
2016-01-17 18:48:19 +00:00
|
|
|
#endif
|
2015-11-06 11:55:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:48:19 +00:00
|
|
|
#ifdef _WIN32
|
2015-11-06 11:55:21 +00:00
|
|
|
bool pjutil::TerminatedExistingExe()
|
|
|
|
{
|
|
|
|
bool bTerminated = false;
|
|
|
|
bool AskedUser = false;
|
|
|
|
DWORD pid = GetCurrentProcessId();
|
|
|
|
|
|
|
|
HANDLE nSearch = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (nSearch != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
PROCESSENTRY32 lppe;
|
|
|
|
|
|
|
|
memset(&lppe, 0, sizeof(PROCESSENTRY32));
|
|
|
|
lppe.dwSize = sizeof(PROCESSENTRY32);
|
|
|
|
stdstr ModuleName = CPath(CPath::MODULE_FILE).GetNameExtension();
|
|
|
|
|
|
|
|
if (Process32First(nSearch, &lppe))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
if (_wcsicmp(lppe.szExeFile, ModuleName.ToUTF16().c_str()) != 0 ||
|
2015-11-06 11:55:21 +00:00
|
|
|
lppe.th32ProcessID == pid)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!AskedUser)
|
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
stdstr_f Message("%s currently running\n\nTerminate pid %d now?", ModuleName.c_str(), lppe.th32ProcessID);
|
|
|
|
stdstr_f Caption("Terminate %s", ModuleName.c_str());
|
|
|
|
|
2015-11-06 11:55:21 +00:00
|
|
|
AskedUser = true;
|
2020-06-09 13:04:38 +00:00
|
|
|
int res = MessageBox(NULL, Message.ToUTF16().c_str(), Caption.ToUTF16().c_str(), MB_YESNO | MB_ICONEXCLAMATION);
|
2015-11-06 11:55:21 +00:00
|
|
|
if (res != IDYES)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lppe.th32ProcessID);
|
|
|
|
if (hHandle != NULL)
|
|
|
|
{
|
|
|
|
if (TerminateProcess(hHandle, 0))
|
|
|
|
{
|
|
|
|
bTerminated = true;
|
2017-07-10 04:16:12 +00:00
|
|
|
WaitForSingleObject(hHandle, 30 * 1000);
|
2015-11-06 11:55:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
stdstr_f Message("Failed to terminate pid %d", lppe.th32ProcessID);
|
|
|
|
stdstr_f Caption("Terminate %s failed!", ModuleName.c_str());
|
|
|
|
MessageBox(NULL, Message.ToUTF16().c_str(), Caption.ToUTF16().c_str(), MB_YESNO | MB_ICONEXCLAMATION);
|
2015-11-06 11:55:21 +00:00
|
|
|
}
|
|
|
|
CloseHandle(hHandle);
|
|
|
|
}
|
|
|
|
} while (Process32Next(nSearch, &lppe));
|
|
|
|
}
|
|
|
|
CloseHandle(nSearch);
|
|
|
|
}
|
|
|
|
return bTerminated;
|
2016-01-17 18:48:19 +00:00
|
|
|
}
|
|
|
|
#endif
|