Files
GdCpp12/include/ProcessHelper.h

52 lines
2.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
GDCPP_API struct sProcessInfo {
std::wstring name;
DWORD pid;
sProcessInfo(const std::wstring& n=L"", DWORD p = 0)
: name(n), pid(p) {
}
// 复制构造函数
sProcessInfo(const sProcessInfo& other)
: name(other.name), pid(other.pid) {
}
// 赋值运算符
sProcessInfo& operator=(const sProcessInfo& other) {
if (this != &other) {
name = other.name;
pid = other.pid;
}
return *this;
}
};
// 查找正在运行的目标进程,返回数量,并将找到的进程写入 outFoundProcs。
// 输入outFoundProcs如果为空则查找结果以追加方式写入
// 如果outFoundProcs大小与targetProcs相同则将找到的进程写入对应位置。
// 示例
//std::vector<std::wstring> MonitorAppName = { L"GdCamExpert", L"GoodVision" };
//std::vector<sProcessInfo> MonitorAppStatus;
//MonitorAppStatus.resize(MonitorAppName.size());
//int runningCount = FindRunningProcesses_BeginWith(MonitorAppName, MonitorAppStatus);
// 两个版本:
// 1. FindRunningProcesses_BeginWith查找进程名以目标字符串开头的进程
// 2. FindRunningProcesses_Exact查找进程名完全匹配目标字符串的进程
// 都不区分大小写。
GDCPP_API int FindRunningProcesses_BeginWith(const std::vector<std::wstring>& targetProcs, std::vector<sProcessInfo>& outFoundProcs);
GDCPP_API int FindRunningProcesses_Exact(const std::vector<std::wstring>& targetProcs, std::vector<sProcessInfo>& outFoundProcs);
// 请求关机或重启EWX_SHUTDOWN、EWX_REBOOT
// cmd: EWX_SHUTDOWN, EWX_REBOOT, EWX_POWEROFF 等
// reason: SHTDN_REASON_MAJOR_OPERATINGSYSTEM, SHTDN_REASON_MINOR_OTHER 等
GDCPP_API void DoShutdown(uint32_t cmd = EWX_SHUTDOWN, DWORD reason = SHTDN_REASON_MAJOR_OPERATINGSYSTEM);
GDCPP_API BOOL IsProcessRunning(LPCTSTR pszProcName, LPTSTR pFoundName, DWORD dwFoundLen);
GDCPP_API bool StartProcessAndGetPid(const fs::path& exePath, DWORD* outPid);
GDCPP_API bool StartProcessIndependently(const fs::path& exePath);
GDCPP_API bool NotifyProcessToExit(DWORD pid, DWORD timeoutMs = 1000);