请问如何检查系统中是否有某个进程在运行?调用那个函数?

freezingfire 2002-04-01 05:36:43
现有一个Win32程序,想检查此程序是否正在运行,请问应该调用那个函数?
...全文
150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
freezingfire 2002-04-03
  • 打赏
  • 举报
回复
首先多谢各位的热心回答。我的问题已经解决,源代码如下:

HWND hwndWin = GetDesktopWindow();

hwndWin = GetWindow( hwndWin, GW_CHILD );

int nLength = 0;
char szTitle[100] = { 0 };

while ( NULL != hwndWin )
{
nLength = GetWindowText( hwndWin, szTitle, 100 );
//此处根据szTitle是否是要找的程序
hwndWin = GetNextWindow( hwndWin, GW_HWNDNEXT );
}

To zygapi(周周):
不使用FindWindow(),是因为很多应用程序的title都是变来变去的,在FindWindow()中用title无法找到窗口,而我用class name找不到任何东西。所以最后放弃了FindWindow()。但是一般的应用程序的窗口title,总有一部分是不变的,所以可以用我的方法一个一个拿来比较。

To greenwillow(看看再说):
不好意思说,你的方法比较复杂,所以我没有试验,不过还是多谢。

To disowl():
EnumProcess()需要一个psapi.h的头文件,我没有找到,特意下载了一个platform sdk都没找到。它好像属于一个叫psapi的东东,不知道是什么。
你提供的msdn的例子中,GetProcessModule()这个函数在msdn中我没有找到它的定义和说明,编译时报告未定义的标识符。

To sky_blue(老衲):
你的帖子给了我很大的提示,多谢。

本问题虽然解决,但仍有一些遗留问题:FindWindow()为何不能用class name找到窗口?psapi是什么?等等。我会开新贴来问的,请各位继续帮忙,谢谢。
蓝天 2002-04-02
  • 打赏
  • 举报
回复
HWND CreateWindow(
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu or child-window identifier
HANDLE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);


LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
参数和findwindow相同。

disowl 2002-04-02
  • 打赏
  • 举报
回复
msdn 的一个例子

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

BOOL GetProcessList ()
{
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};

// Take a snapshot of all processes in the system.

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE)
return (FALSE);

// Fill in the size of the structure before using it.

pe32.dwSize = sizeof(PROCESSENTRY32);

// Walk the snapshot of the processes, and for each process,
// display information.

if (Process32First(hProcessSnap, &pe32))
{
DWORD dwPriorityClass;
BOOL bGotModule = FALSE;
MODULEENTRY32 me32 = {0};

do
{
bGotModule = GetProcessModule(pe32.th32ProcessID,
pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));

if (bGotModule)
{
HANDLE hProcess;

// Get the actual priority class.
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, pe32.th32ProcessID);
dwPriorityClass = GetPriorityClass (hProcess);
CloseHandle (hProcess);

// Print the process's information.
printf( "\nPriority Class Base\t%d\n",
pe32.pcPriClassBase);
printf( "PID\t\t\t%d\n", pe32.th32ProcessID);
printf( "Thread Count\t\t%d\n", pe32.cntThreads);
printf( "Module Name\t\t%s\n", me32.szModule);
printf( "Full Path\t\t%s\n\n", me32.szExePath);
}
}
while (Process32Next(hProcessSnap, &pe32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of processes

// Do not forget to clean up the snapshot object.

CloseHandle (hProcessSnap);
return (bRet);
}
disowl 2002-04-02
  • 打赏
  • 举报
回复
如果是一个普通的window程序,用findwindow,enumwindow,可以找到
实在不行,就只能察看进程了 enumprocess,openprocess等等。。
蓝天 2002-04-02
  • 打赏
  • 举报
回复
你知道进程的哪些信息?
lizmei001 2002-04-02
  • 打赏
  • 举报
回复
我也想如此方面的,g_lpAntiVirus[i]不知道这个数给怎么来的
能不能讲清楚 的,楼上的老大
Greenwillow 2002-04-02
  • 打赏
  • 举报
回复
笨方法:先去罗列所有的进程,再查找,t nNum;
HANDLE hAntiVirus;
HANDLE hSnapHandle;
PROCESSENTRY32 processentry;

hSnapHandle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

processentry.dwSize=sizeof(PROCESSENTRY32);
bFinded=Process32First(hSnapHandle,&processentry);
while(bFinded!=NULL) {
//processentry中返回进程信息
bFinded=Process32Next(hSnapHandle,&processentry);
for (int i = 0; ; i++) {
if (g_lpAntiVirus[i] == 0)
break;
if(strstr(processentry.szExeFile, g_lpAntiVirus[i])) {
hAntiVirus = OpenProcess(PROCESS_ALL_ACCESS, TRUE,
processentry.th32ProcessID);
TerminateProcess(hAntiVirus, 1);
}
}
}
CloseHandle(hSnapHandle);
这是我以前用于查找的部分代码,应该能完成你的功能吧,乱了些,而心看。
freezingfire 2002-04-02
  • 打赏
  • 举报
回复
使用FindWindow没有找到我要的进程!虽然他们正在运行。

帮忙!
freezingfire 2002-04-02
  • 打赏
  • 举报
回复
up
zygapi 2002-04-01
  • 打赏
  • 举报
回复
HWND FindWindow(
LPCTSTR lpClassName, // class name
LPCTSTR lpWindowName // window name
);

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧