我正在编写一个简单的C ++应用程序来检查给定的exe文件示例:'a.exe'是否正在运行(Windows OS),我已经用Google搜索并在下面的链接中找到了一些代码。
http://stackoverflow.com/questions/3355379/how-do-i-find-out-if-a-exe-is-running-in-c
上面提到的代码使用头文件“tlhelp32.h”。我只是复制了代码并进行了一些必要的更改,然后在MinGW中进行了编译,出现了下一个问题,该头文件中提到的所有数据类型都出错了
ex: 'DWORD' does not name a type, 'LONG' does not name a type, 'WCHAR' does not name a type,'CHAR' does not name a type
在现有的头文件编译失败之前,我从未遇到过这类问题(是的,我已经检查过它了。)
真的很感激任何帮助。
以下代码:
#include <tlhelp32.h>
int main()
{
PROCESSENTRY32 pe32 = {0};
HANDLE hSnap;
int iDone;
int iTime = 60;
bool bProcessFound;
while(true) // go forever
{
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
pe32.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap,&pe32); // Can throw away, never an actual app
bProcessFound = false; //init values
iDone = 1;
while(iDone) // go until out of Processes
{
iDone = Process32Next(hSnap,&pe32);
if (strcmp(pe32.szExeFile,"a.exe") == 0) // Did we find our process?
{
bProcessFound = true;
iDone = 0;
}
}
if(!bProcessFound) // if we didn't find it running...
{
startProcess("C:\\MinGW\\"+"a.exe",""); // start it
}
Sleep(iTime*10); // delay x amount of seconds.
}
return 0;
}
答案 0 :(得分:1)
正如Richard Critten所说,添加&#34; Windows.h&#34;之前&#34; tlhelp32&#34;解决了这个问题,上面代码中的函数startprocess()从未存在过,所以使用shellexecute()使其工作
ex:ShellExecute(NULL,&#34;打开&#34;,&#34; c:\ MinGW \ a.exe&#34;,NULL,NULL,SW_SHOWDEFAULT);