我试图从以下答案导入代码:Get full running process list ( Visual C++ )
bool FindRunningProcess(AnsiString process) {
/*
Function takes in a string value for the process it is looking for like ST3Monitor.exe
then loops through all of the processes that are currently running on windows.
If the process is found it is running, therefore the function returns true.
*/
AnsiString compare;
bool procRunning = false;
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
procRunning = false;
} else {
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32)) { // Gets first running process
if (pe32.szExeFile == process) {
procRunning = true;
} else {
// loop through all running processes looking for process
while (Process32Next(hProcessSnap, &pe32)) {
// Set to an AnsiString instead of Char[] to make compare easier
compare = pe32.szExeFile;
if (compare == process) {
// if found process is running, set to true and break from loop
procRunning = true;
break;
}
}
}
// clean the snapshot object
CloseHandle(hProcessSnap);
}
}在菲尔的回答中,他使用的是System::AnsiString类,我不确定如何将它包含在我的项目中,即它是安装的软件包的一部分,还是我需要下载并包含它?
这个问题的一个扩展:我还可以用另一个替代品来实现与AnsiString相同的功能吗?
我对这段代码的最终目标是修改它,这样我就可以得到正在运行的进程的当前列表,如果它正在运行,我将寻找一个特定的进程来终止它。我尝试使用ce::string,但由于pe32.szExeFile是TCHAR [260]类型,所以无法将其传递给以下ce::string process_name;的ce::string声明(这可能是他使用System::AnsiString的原因)。
我假设pe32.szExeFile将返回进程名,所以我想将它与另一个声明的字符串和特定的进程名进行比较。
发布于 2015-06-02 19:21:08
好的,所以,根本不清楚AnsiString是什么;,你已经假设它是 class ,坦白地说,这看起来是一个合理的假设。
不过,我不想尝试去获得它。我将把重点放在编写标准代码上,切换到std::string**/**std::wstring (适当的情况下)。要使作者的代码变得可移植,应该是非常简单的。您将不得不浏览并阅读该代码中使用的函数的文档,以查看哪些功能可以工作,哪些不能工作。看起来System::AnsiString几乎或者完全是std::string-compatible,但是只有尝试一下才会知道。
我再怎么强调也不为过,不要走在步入你的时间机器并在1950年打开它的道路上,它的午餐盒里装满了指针和可怕的过时的C-字符串比较功能。我真的不明白为什么有人会建议这么做。
https://stackoverflow.com/questions/30604485
复制相似问题