__inline static
void KillallProcess(LPCWSTR lpcwProcessName)
{
DWORD e = 0;
WCHAR ss[MAX_PATH] = { 0 };
LPVOID lpMsgBuf = NULL;
long re = 0;
BOOL bResult = FALSE;
HANDLE hSnapShot = NULL;
PROCESSENTRY32W PE32W = { 0 };
hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
e = ::GetLastError();
if (hSnapShot == NULL)
{
::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL);
if (lpMsgBuf != NULL)
{
::_snwprintf_s(ss, sizeof(ss) / sizeof(*ss), L"%s", (LPWSTR)lpMsgBuf);
::LocalFree(lpMsgBuf);
}
if (e != 0)::MessageBoxW(NULL, ss, (L"GWPID -> CreateToolhelp32Snapshot"), MB_OK);
}
else
{
PE32W.dwSize = sizeof(PE32W);
bResult = ::Process32FirstW(hSnapShot, &PE32W);
e = ::GetLastError();
if (bResult == FALSE)
{
::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL);
if (lpMsgBuf != NULL)
{
::_snwprintf_s(ss, sizeof(ss) / sizeof(*ss), L"%s", (LPWSTR)lpMsgBuf);
::LocalFree(lpMsgBuf);
}
if (e != 0)::MessageBoxW(NULL, ss, (L"GWPID -> Process32First"), MB_OK);
}
while (bResult != FALSE)
{
if (::_wcsicmp(PE32W.szExeFile, lpcwProcessName) == 0 && PE32W.th32ProcessID != ::GetCurrentProcessId())
{
HANDLE hP = NULL;
hP = ::OpenProcess(PROCESS_ALL_ACCESS, false, PE32W.th32ProcessID);
e = ::GetLastError();
if (hP == NULL)
{
::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL);
if (lpMsgBuf != NULL)
{
::_snwprintf_s(ss, sizeof(ss) / sizeof(*ss), L"%s", (LPWSTR)lpMsgBuf);
::LocalFree(lpMsgBuf);
}
if (e != 0)::MessageBoxW(NULL, ss, (L"SW -> OpenProcess"), MB_OK);
}
if (hP != NULL)
{
::TerminateProcess(hP, (0));
::CloseHandle(hP);
}
}
bResult = ::Process32NextW(hSnapShot, &PE32W);
e = ::GetLastError();
if (bResult == FALSE && e != ERROR_NO_MORE_FILES)
{
::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL);
if (lpMsgBuf != NULL)
{
::_snwprintf_s(ss, sizeof(ss) / sizeof(*ss), L"%s", (LPWSTR)lpMsgBuf);
::LocalFree(lpMsgBuf);
}
if (e != 0)::MessageBoxW(NULL, ss, (L"GWPID -> Process32Next"), MB_OK);
}
}
::CloseHandle(hSnapShot);
}
return;
}
__inline static
INT RunCmd(LPCWSTR lpApplicationName, LPWSTR lpCommandLine = NULL,
INT(__stdcall* cbEnter)(STARTUPINFOW&, PROCESS_INFORMATION&, DWORD&, LPVOID) = NULL,
INT(__stdcall* cbStart)(STARTUPINFOW&, PROCESS_INFORMATION&, DWORD&, LPVOID) = NULL,
INT(__stdcall* cbLeave)(STARTUPINFOW&, PROCESS_INFORMATION&, DWORD&, LPVOID) = NULL,
LPVOID lp = NULL)
{
BOOL bRet = FALSE;
STARTUPINFO si = { 0 };
DWORD cf = CREATE_NO_WINDOW;
PROCESS_INFORMATION pi = { 0 };
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
if (cbEnter != NULL)
{
if (cbEnter(si, pi, cf, lp) != 0)
{
return(-1);
}
}
if (::CreateProcessW(lpApplicationName, lpCommandLine, NULL, NULL, FALSE, cf, NULL, NULL, &si, &pi) == TRUE)
{
if (cbStart != NULL)
{
if (cbStart(si, pi, cf, lp) != 0)
{
return(-1);
}
}
::WaitForSingleObject(pi.hProcess, INFINITE);
::TerminateProcess(pi.hProcess, (0U));
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
}
if (cbLeave!= NULL)
{
if (cbLeave(si, pi, bi, cf, cd, lp) != 0)
{
return(-1);
}
}
return(0);
}
//关闭清理进程
__inline static VOID CleanupProgram(LPSTARTUPINFO lpsi, LPPROCESS_INFORMATION lppi)
{
if (lpsi)
{
if (lpsi->hStdError && lpsi != INVALID_HANDLE_VALUE)
{
CloseHandle(lpsi->hStdError);
lpsi->hStdError = NULL;
}
if (lpsi->hStdInput && lpsi != INVALID_HANDLE_VALUE)
{
CloseHandle(lpsi->hStdInput);
lpsi->hStdInput = NULL;
}
if (lpsi->hStdOutput && lpsi != INVALID_HANDLE_VALUE)
{
CloseHandle(lpsi->hStdOutput);
lpsi->hStdOutput = NULL;
}
}
if (lppi)
{
if (lppi->hProcess)
{
TerminateProcess(lppi->hProcess, (0L));
CloseHandle(lppi->hProcess);
lppi->hProcess = NULL;
}
if (lppi->hThread)
{
TerminateThread(lppi->hThread, (0L));
CloseHandle(lppi->hThread);
lppi->hThread = NULL;
}
}
}
//启动进程(应用程序文件名称、参数、启动类型及等待时间)
__inline static BOOL StartupProgram(LPCTSTR lpApplicationName, LPTSTR lpCommandLine = NULL, WORD wShowWindow = SW_SHOW, DWORD dwMilliseconds = WAIT_TIMEOUT, LPPROCESS_INFORMATION lpProcessInformation = NULL, LPSTARTUPINFO lpStartupInfo = NULL)
{
BOOL bResult = FALSE;
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
LPSTARTUPINFO lpsi = &si;
LPPROCESS_INFORMATION lppi = π
if (lpStartupInfo)
{
lpsi = lpStartupInfo;
}
if (lpProcessInformation)
{
lppi = lpProcessInformation;
}
ZeroMemory(lpsi, sizeof(STARTUPINFO));
lpsi->cb = sizeof(STARTUPINFO);
lpsi->dwFlags = STARTF_USESHOWWINDOW;
lpsi->wShowWindow = wShowWindow;
ZeroMemory(lppi, sizeof(PROCESS_INFORMATION));
// Start the child process.
bResult = CreateProcess(lpApplicationName, // No module name (use command line)
lpCommandLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0L, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
lpsi, // Pointer to STARTUPINFO structure
lppi); // Pointer to PROCESS_INFORMATION structure
if (bResult)
{
// Wait until child process exits.
WaitForSingleObject(lppi->hProcess, dwMilliseconds);
if (!lpProcessInformation)
{
// Close process and thread handles.
CleanupProgram(lpsi, lppi);
}
}
return bResult;
}
__inline static DWORD GetProgramPath(_TCHAR tFilePath[MAX_PATH] = _T(""))
{
DWORD dwSize = 0;
_TCHAR * pT = NULL;
dwSize = GetModuleFileName(GetModuleHandle(NULL), tFilePath, MAX_PATH);
if (dwSize)
{
pT = _tcsrchr(tFilePath, _T('\\'));
if (pT)
{
*(pT + sizeof(BYTE)) = _T('\0');
dwSize = (DWORD)(pT - tFilePath);
}
}
return dwSize;
}
__inline static UINT GetSystemPath(_TCHAR tFilePath[MAX_PATH] = _T(""))
{
UINT uSize = 0;
_TCHAR * pT = NULL;
uSize = GetSystemDirectory(tFilePath, MAX_PATH);
if (uSize)
{
pT = _tcsrchr(tFilePath, _T('\\'));
if (pT)
{
*(pT + sizeof(BYTE)) = _T('\0');
uSize = (DWORD)(pT - tFilePath);
}
}
return uSize;
}
// 判断是否非数字(10进制)
__inline static bool IsDigitsString(const _TCHAR * s)
{
bool result = false;
if (s)
{
do
{
if (*s != _T('\0'))
{
if ((*s < _T('0')) || (*s > _T('9')))
{
result = false;
break;
}
}
else
{
result = true;
break;
}
} while (s++);
}
return result;
}
// 过滤尾部空格回车换行符( \r\n)
__inline static void RemoveTailerFilter(tstring & s)
{
tstring::size_type n = s.length();
tstring::size_type i = n;
while (i-- && (s.at(i) == _T('\r') || s.at(i) == _T('\n') || s.at(i) == _T(' ')))
{
s.at(i) = _T('\0');
}
}