注意:下面类中用到的std::string path路径,一律假设为编码ASCI路径,因为是调用Windows API。
头文件
#pragma once
#include <WinBase.h>
#include <string>
class ProcessHelper
{
public:
static bool IsRunningAlready(const std::string& processName);
static void StartProcessByName(const std::string& processName);
static void StartProcessByFileFullPath(const std::string& exeFileFullPath);
static DWORD FindProcessIDByName(const std::string& processName);//0 not found ; other found; processName "processName.exe"
};
源文件
#include "StdAfx.h"
#include "ProcessHelper.h"
#include <WinBase.h>
#include "tlhelp32.h"//Kernel32.dll Kernel32.lib
#include "PathHelper.h"
#include <ShellAPI.h>
#include "StringHelper.h"
bool ProcessHelper::IsRunningAlready(const std::string& processName)
{
HANDLE tempMutex = CreateMutexA(NULL, TRUE, processName.c_str());
DWORD dwRet = GetLastError();
if (tempMutex)
{
if (ERROR_ALREADY_EXISTS == dwRet)
{
//MessageBoxA(nullptr, "程序已经在运行中了!", "警告", MB_OK);
CloseHandle(tempMutex);
return true;
}
}
return false;
}
void ProcessHelper::StartProcessByName(const std::string& processName)
{
if(FindProcessIDByName(processName)==0)
{
//wfout << "ready to start exe:"<<fullPathFile<<std::endl;
SHELLEXECUTEINFOA lpExecInfo = {0};
lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.lpVerb = "Open";
lpExecInfo.hwnd = NULL;
auto fullPathFile = PathHelper::GetCurrentDir() + processName;
lpExecInfo.lpFile = (fullPathFile).c_str();
lpExecInfo.lpDirectory = nullptr;
lpExecInfo.nShow = SW_SHOWDEFAULT;
lpExecInfo.lpParameters = NULL;
lpExecInfo.hInstApp = NULL;
auto ret = ShellExecuteExA(&lpExecInfo);
if (!ret)
{
//wfout<<L"can not found file:" << fullPathFile << std::endl;
}
}
}
void ProcessHelper::StartProcessByFileFullPath(const std::string& exeFileFullPath)
{
if(FindProcessIDByName(exeFileFullPath)==0)
{
//wfout << "ready to start exe:"<<fullPathFile<<std::endl;
SHELLEXECUTEINFOA lpExecInfo = {0};
lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.lpVerb = "Open";
lpExecInfo.hwnd = NULL;
auto fullPathFile = exeFileFullPath;
lpExecInfo.lpFile = (fullPathFile).c_str();
lpExecInfo.lpDirectory = nullptr;
lpExecInfo.nShow = SW_SHOWDEFAULT;
lpExecInfo.lpParameters = NULL;
lpExecInfo.hInstApp = NULL;
auto ret = ShellExecuteExA(&lpExecInfo);
if (!ret)
{
//wfout<<L"can not found file:" << fullPathFile << std::endl;
}
}
}
DWORD ProcessHelper::FindProcessIDByName(const std::string& processName)//0 not found ; other found; processName "processName.exe"
{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
return( 0 );
}
pe32.dwSize = sizeof( PROCESSENTRY32 );
if( !Process32First( hProcessSnap, &pe32 ) )
{
CloseHandle( hProcessSnap ); // clean the snapshot object
return( 0 );
}
DWORD processId = 0;
do
{
#ifdef _UNICODE
if(std::wstring(pe32.szExeFile) == StringHelper::ANSIToUnicode(processName))//进程名称
#else
if(std::string(pe32.szExeFile) == processName)//进程名称
#endif
{
processId = pe32.th32ProcessID;//进程ID
break;
}
} while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return( processId );
}