为Windiws和Linux
#ifdef WIN32
#include <winsock2.h>
#include <process.h>
#pragma comment(lib, "Ws2_32.lib")
#include <direct.h>
#include "tlhelp32.h"
#include "shellapi.h"
#else
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/procfs.h>
#include <unistd.h>
#include <stropts.h>
#include <fcntl.h>
#include <signal.h>
#endif
#ifdef WIN32
#define rmdir(x) _rmdir(x)
#define close_file(x) _close(x)
#define PID DWORD
#define mkdir(x) _mkdir(x)
#define sp_sleep(x) Sleep(x)
#define setWorkingPath(x) SetCurrentDirectory(x)
#define getErrorNo() GetLastError()
#else
#define rmdir(x) rmdir(x)
#define close_file(x) close(x)
#define PID pid_t
#define mkdir(x) mkdir(x, 0)
#define sp_sleep(x) usleep(x * 1000)
#define setWorkingPath(x) chdir(x)
#define getErrorNo() errno
#endif
/**
* @brief startUpProcess
*
* Detailed description.
* @param[in] processName
* @param[in] processWorkingPath
* @param[in] startupParam Defaults to "".
* @param[in] showWindow Defaults to true.
* @return bool
*/
inline bool startUpProcess(std::string processName, std::string processWorkingPath, std::string startupParam = "", bool showWindow = true)
{
#ifdef WIN32
HINSTANCE hInstance;
if (showWindow)
{
hInstance = ShellExecute(NULL, NULL, (processWorkingPath + processName).c_str(), startupParam.c_str(), processWorkingPath.c_str(), SW_SHOW);
}
else
{
hInstance = ShellExecute(NULL, NULL, (processWorkingPath + processName).c_str(), startupParam.c_str(), processWorkingPath.c_str(), SW_HIDE);
}
if (32 < (int)hInstance)
{
return true;
}
else
{
return false;
}
#else
system(("chmod 777 " + processWorkingPath + processName).c_str());
std::string cwp = getCurrentWorkingPath();
setWorkingPath(processWorkingPath.c_str());
int ret = system((processWorkingPath + processName + " " + startupParam + " &").c_str());
setWorkingPath(cwp.c_str());
if (-1 == ret)
{
return false;
}
#endif
return true;
}
其中getCurrentWorkingPath见