C语言中怎样判断某个特定的程序是否正在运行(Windows)
发布网友
发布时间:2023-06-26 15:52
我来回答
共1个回答
热心网友
时间:2023-10-27 20:47
#include<windows.h>
#include <stdio.h>
#include "PSAPI.H"
#pragma comment( lib, "PSAPI.LIB" )
BOOL EnablePrivilege(HANDLE hToken,LPCSTR szPrivName);
int main(void)
{
DWORD processid[1024],needed,processcount,i;
HANDLE hProcess;
HMODULE hMole;
char path[MAX_PATH] = "",temp[256];
HANDLE hToken;
printf("ShowProcessPath 2.0 with [Process Status API]\n\n");
if ( OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken) )
{
if (EnablePrivilege(hToken,SE_DEBUG_NAME))
{
EnumProcesses(processid, sizeof(processid), &needed);
processcount=needed/sizeof(DWORD);
for (i=0;i<processcount;i++)
{
hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false,processid[i]);
if (hProcess)
{
EnumProcessMoles(hProcess, &hMole, sizeof(hMole), &needed);
GetMoleFileNameEx(hProcess, hMole, path, sizeof(path));
>>>>在这里判断是否是你所需要的文件
GetShortPathName(path,path,256);
itoa(processid[i],temp,10);
printf("%s --- %s\n",path,temp);
}
else
printf("Failed!!!\n");
}
}
}
CloseHandle(hProcess);
CloseHandle(hMole);
itoa(processcount,temp,10);
printf("\nProcess Count:%s\n\n",temp);
return 0;
}
BOOL EnablePrivilege(HANDLE hToken,LPCSTR szPrivName)
{
TOKEN_PRIVILEGES tkp;
LookupPrivilegeValue( NULL,szPrivName,&tkp.Privileges[0].Luid );//修改进程权限
tkp.PrivilegeCount=1;
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken,FALSE,&tkp,sizeof tkp,NULL,NULL );//通知系统修改进程权限
return( (GetLastError()==ERROR_SUCCESS) );
}