C语言中怎样判断某个特定的程序是否正在运行(Windows)
1个回答
展开全部
#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 hModule;
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)
{
EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
GetModuleFileNameEx(hProcess, hModule, 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(hModule);
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) );
}
#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 hModule;
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)
{
EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
GetModuleFileNameEx(hProcess, hModule, 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(hModule);
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) );
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询