杀死进程
注意openprocess权限
PROCESS_QUERY_INFORMATION|PROCESS_VM_READ |PROCESS_TERMINATE
示例:
HWND hWnd;
hWnd=FindWindow(NULL,_T("名字"));
if(hWnd==NULL)
return;
方法1 ::PostMessage(hWnd,WM_CLOSE,0,0);
方法2 DWORD dwAID;
GetWindowThreadProcessID(hWnd,&dwAID);
HANDLE handle=OpenProcess(0,FALSE,deAID);
TerminateProcess(handle,0);
转 http://www.vckbase.com/index.php/wv/1200
示例:
*************************************************************************
Module:ps.c
说明:调用PSAPI函数枚举系统进程名和ID,Only for NT/2000
*************************************************************************/
#include "psapi.h"
#pragma comment(lib,"psapi.lib")
void PrintProcessNameAndID( DWORD processID )
{
char szProcessName[MAX_PATH] = "unknown";
//取得进程的句柄
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
//取得进程名称
if ( hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
//回显进程名称和ID
printf( "\n%-20s%-20d", szProcessName, processID );
CloseHandle( hProcess );
}
void main( )
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
//枚举系统进程ID列表
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
//计算进程数量
cProcesses = cbNeeded / sizeof(DWORD);
// 输出每个进程的名称和ID
for ( i = 0; i < cProcesses; i++ )
PrintProcessNameAndID( aProcesses[i] );
return;
}
///////////////////////////////////////////////////////////////
示例2:
/**********************************************************************
Module:ps.c
说明:调用ToolHelp函数枚举本地系统进程名和ID,Only for 9x/2000
**********************************************************************/
int main()
{
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 pe32 = {0};
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == (HANDLE)-1)
{
printf("\nCreateToolhelp32Snapshot() failed:%d",GetLastError());
return 1;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
printf("\nProcessName ProcessID");
if (Process32First(hProcessSnap, &pe32))
{
do
{
printf("\n%-20s%d",pe32.szExeFile,pe32.th32ProcessID);
}while (Process32Next(hProcessSnap, &pe32));
}
else
{
printf("\nProcess32Firstt() failed:%d",GetLastError());
}
CloseHandle (hProcessSnap);
return 0;
}
////////////////////