TerminateProcessThe TerminateProcess function terminates the specified process and all of its threads. BOOL TerminateProcess( HANDLE hProcess, UINT uExitCode );Parameters hProcess [in] Handle to the process to terminate. Windows NT/2000/XP: The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.uExitCode [in] Exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);Parameters
hProcess
[in] Handle to the process to terminate.
Windows NT/2000/XP: The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.uExitCode
[in] Exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
DWORD ProcessID[50];
CString kkk[50];
HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32* info=new PROCESSENTRY32;
PROCESSENTRY32 ProcessInfo;//声明进程信息变量
ProcessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小
//返回系统中第一个进程的信息
BOOL Status=Process32First(SnapShot,&ProcessInfo);
int m_nProcess=0;
///////////////////////////////////////////////////////
while(Status)
{
CString s,str1,str2;
s.Format("%d",ProcessInfo.cntThreads);
str1.Format("%s",ProcessInfo.szExeFile);
str1=ProcessInfo.szExeFile;
kkk[m_nProcess]=ProcessInfo.szExeFile;
ProcessID[m_nProcess]=ProcessInfo.th32ProcessID;
//////////////////////////////////////////////////////
if(str1=="****.exe")
{
HANDLE ProcessHandle;
ProcessHandle=OpenProcess (PROCESS_ALL_ACCESS,FALSE,ProcessID[m_nProcess]);
TerminateProcess(ProcessHandle,0);
}
Status=Process32Next(SnapShot,&ProcessInfo);
m_nProcess++;
}
///////////////////////////最后记得加入头文件
#include "TLHELP32.H"//一定要加如这个头文件
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
if ( ! OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
{
AfxMessageBox("openprocesstoken");
return;
}
if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) ){
CloseHandle( hToken );
{
AfxMessageBox("close handle");
return;
}
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) )
{
CloseHandle( hToken );
}
}EnableDebugPriv();
TerminateProcess(OpenProcess(PROCESS_TERMINATE ,FALSE,进程号,0);
/*
函数:取得进程列表
*/
void CSystemInfo::ListProcess()
{
CString m_proc;
HANDLE hProcessSnap;
HANDLE hModuleSnap;
PROCESSENTRY32 pe32={0};
MODULEENTRY32 me32={0};
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == (HANDLE)-1)
{
return;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32))
{
do
{
if(GetVersion()&0x80000000)
{
CString strpro;
CString pstrpro;
char MName[_MAX_PATH];
char EXT[_MAX_PATH];
::_splitpath(pe32.szExeFile,NULL,NULL,MName,EXT);
pstrpro.Format("%s%s",MName,EXT);
strpro.Format("%d%s%s%s%s%s",pe32.th32ProcessID,"$",pstrpro,"#",pe32.szExeFile,"@");
m_proc+=strpro;
}
else
{
if(pe32.th32ProcessID>8)
{
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pe32.th32ProcessID);
if(hModuleSnap!=(HANDLE)-1)
{
me32.dwSize=sizeof(MODULEENTRY32);
if(Module32First(hModuleSnap,&me32))
{
CString strpro;
CString pstrpro;
char MName[_MAX_PATH];
char EXT[_MAX_PATH];
::_splitpath(pe32.szExeFile,NULL,NULL,MName,EXT);
pstrpro.Format("%s%s",MName,EXT);
strpro.Format("%d%s%s%s%s%s",pe32.th32ProcessID,"$",pstrpro,"#",me32.szExePath,"@");
m_proc+=strpro;
}
CloseHandle(hModuleSnap);
}
}
}
}while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle (hProcessSnap);
}