vc++中程序运行后为什么进程没有关闭?能给出正确的完整的程序吗?源代码如下
#include<iostream>#include<windows.h>usingnamespacestd;intmain(){HANDLEhProcessHandle...
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{HANDLE hProcessHandle;
ULONG nProcessID;
HWND TheWindow;
TheWindow = ::FindWindow( NULL, "Explorer.EXE" );
::GetWindowThreadProcessId( TheWindow, &nProcessID );
hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE,
nProcessID );
::TerminateProcess( hProcessHandle, 4 );
return 0;
}
为什么我提升了权限也不行?可以给一个可行的代码我看一下吗?麻烦了
} 展开
#include<windows.h>
using namespace std;
int main()
{HANDLE hProcessHandle;
ULONG nProcessID;
HWND TheWindow;
TheWindow = ::FindWindow( NULL, "Explorer.EXE" );
::GetWindowThreadProcessId( TheWindow, &nProcessID );
hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE,
nProcessID );
::TerminateProcess( hProcessHandle, 4 );
return 0;
}
为什么我提升了权限也不行?可以给一个可行的代码我看一下吗?麻烦了
} 展开
展开全部
结束进程需要提升权限,下面是一个结束指定ID进程的程序片段,希望对你又帮助。
这个是完整的代码,pslist函数列出所有进程,killps杀掉指定ID的进程。
#include <windows.h>
#include <stdio.h>
#include <Tlhelp32.h>
void pslist(void);
BOOL killps(DWORD id);
BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege);
void pslist(void)
{
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 pe32= {0};
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //创建一个进程快照
if (hProcessSnap == (HANDLE)-1)
{
printf("\nCreateToolhelp32Snapshot() failed:%d",GetLastError());
return ;
}
pe32.dwSize = sizeof(PROCESSENTRY32); //初始化PROCESSENTRY32结构体
printf("\nProcessName ProcessID");
if (Process32First(hProcessSnap, &pe32)) //第一个进程
{
char a[5];
do //开始枚举进程
{
itoa(pe32.th32ProcessID,a,10); //转换进程ID号
printf("\n%-20s%d",pe32.szExeFile,pe32.th32ProcessID); //输出进程名字和ID
}
while (Process32Next(hProcessSnap, &pe32));
}
else
{
printf("\nProcess32Firstt() failed:%d",GetLastError());
}
CloseHandle (hProcessSnap);
return;
}
BOOL killps(DWORD id)
{
HANDLE hProcess=NULL,hProcessToken=NULL;
BOOL IsKilled=FALSE,bRet=FALSE;
// try
{
//获得进程访问令牌的句柄
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ALL_ACCESS,&hProcessToken))
{
printf("\nOpen Current Process Token failed:%d",GetLastError());
// __leave;
}
//printf("\nOpen Current Process Token ok!");
if(!SetPrivilege(hProcessToken,SE_DEBUG_NAME,TRUE)) //提升权限
{
// __leave;
}
printf("\nSetPrivilege ok!");
if((hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id))==NULL) //打开进程
{
printf("\nOpen Process %d failed:%d",id,GetLastError());
// __leave;
}
//printf("\nOpen Process %d ok!",id);
if(!TerminateProcess(hProcess,1)) //关闭进程
{
printf("\nTerminateProcess failed:%d",GetLastError());
// __leave;
}
IsKilled=TRUE;
}
//__finally
{
if(hProcessToken!=NULL) CloseHandle(hProcessToken);
if(hProcess!=NULL) CloseHandle(hProcess);
}
return(IsKilled);
}
BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege) //提升权限
{
TOKEN_PRIVILEGES tp;
LUID luid;
if(!LookupPrivilegeValue(NULL,lpszPrivilege,&luid)) //查询权限GUID
{
printf("\nLookupPrivilegeValue error:%d", GetLastError() );
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges( //修改访问令牌
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL);
if (GetLastError() != ERROR_SUCCESS)
{
printf("AdjustTokenPrivileges failed: %u\n", GetLastError() );
return FALSE;
}
return TRUE;
}
void main()
{
pslist();
// killps(2544);
}
这个是完整的代码,pslist函数列出所有进程,killps杀掉指定ID的进程。
#include <windows.h>
#include <stdio.h>
#include <Tlhelp32.h>
void pslist(void);
BOOL killps(DWORD id);
BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege);
void pslist(void)
{
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 pe32= {0};
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //创建一个进程快照
if (hProcessSnap == (HANDLE)-1)
{
printf("\nCreateToolhelp32Snapshot() failed:%d",GetLastError());
return ;
}
pe32.dwSize = sizeof(PROCESSENTRY32); //初始化PROCESSENTRY32结构体
printf("\nProcessName ProcessID");
if (Process32First(hProcessSnap, &pe32)) //第一个进程
{
char a[5];
do //开始枚举进程
{
itoa(pe32.th32ProcessID,a,10); //转换进程ID号
printf("\n%-20s%d",pe32.szExeFile,pe32.th32ProcessID); //输出进程名字和ID
}
while (Process32Next(hProcessSnap, &pe32));
}
else
{
printf("\nProcess32Firstt() failed:%d",GetLastError());
}
CloseHandle (hProcessSnap);
return;
}
BOOL killps(DWORD id)
{
HANDLE hProcess=NULL,hProcessToken=NULL;
BOOL IsKilled=FALSE,bRet=FALSE;
// try
{
//获得进程访问令牌的句柄
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ALL_ACCESS,&hProcessToken))
{
printf("\nOpen Current Process Token failed:%d",GetLastError());
// __leave;
}
//printf("\nOpen Current Process Token ok!");
if(!SetPrivilege(hProcessToken,SE_DEBUG_NAME,TRUE)) //提升权限
{
// __leave;
}
printf("\nSetPrivilege ok!");
if((hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id))==NULL) //打开进程
{
printf("\nOpen Process %d failed:%d",id,GetLastError());
// __leave;
}
//printf("\nOpen Process %d ok!",id);
if(!TerminateProcess(hProcess,1)) //关闭进程
{
printf("\nTerminateProcess failed:%d",GetLastError());
// __leave;
}
IsKilled=TRUE;
}
//__finally
{
if(hProcessToken!=NULL) CloseHandle(hProcessToken);
if(hProcess!=NULL) CloseHandle(hProcess);
}
return(IsKilled);
}
BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege) //提升权限
{
TOKEN_PRIVILEGES tp;
LUID luid;
if(!LookupPrivilegeValue(NULL,lpszPrivilege,&luid)) //查询权限GUID
{
printf("\nLookupPrivilegeValue error:%d", GetLastError() );
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges( //修改访问令牌
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL);
if (GetLastError() != ERROR_SUCCESS)
{
printf("AdjustTokenPrivileges failed: %u\n", GetLastError() );
return FALSE;
}
return TRUE;
}
void main()
{
pslist();
// killps(2544);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询