#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
BOOL ListProcessThreads( DWORD dwOwnerPID )
{
HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
THREADENTRY32 te32;
// Take a snapshot of all running threads
hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
if( hThreadSnap == INVALID_HANDLE_VALUE )
return( FALSE );
// Fill in the size of the structure before using it.
te32.dwSize = sizeof(THREADENTRY32 );
// Retrieve information about the first thread,
// and exit if unsuccessful
if( !Thread32First( hThreadSnap, &te32 ) )
{
printf( "Thread32First" ); // Show cause of failure
CloseHandle( hThreadSnap ); // Must clean up the snapshot object!
return( FALSE );
}
// Now walk the thread list of the system,
// and display information about each thread
// associated with the specified process
do
{
if( te32.th32OwnerProcessID == dwOwnerPID )
{
printf( "\n\n THREAD ID = 0x%08X", te32.th32ThreadID );
printf( "\n base priority = %d", te32.tpBasePri );
printf( "\n delta priority = %d", te32.tpDeltaPri );
}
} while( Thread32Next(hThreadSnap, &te32 ) );
// Don't forget to clean up the snapshot object.
CloseHandle( hThreadSnap );
return( TRUE );
}
BOOL GetProcessList( )
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
printf( "CreateToolhelp32Snapshot (of processes)" );
return( FALSE );
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
printf( "Process32First" ); // Show cause of failure
CloseHandle( hProcessSnap ); // Must clean up the snapshot object!
return( FALSE );
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
printf( "\n\n=====================================================" );
printf( "\nPROCESS NAME: %s", pe32.szExeFile );
printf( "\n-----------------------------------------------------" );
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
printf( "OpenProcess" );
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
printf( "GetPriorityClass" );
CloseHandle( hProcess );
}
printf( "\n process ID = 0x%08X", pe32.th32ProcessID );
printf( "\n thread count = %d", pe32.cntThreads );
printf( "\n parent process ID = 0x%08X", pe32.th32ParentProcessID );
printf( "\n Priority Base = %d", pe32.pcPriClassBase );
if( dwPriorityClass )
printf( "\n Priority Class = %d", dwPriorityClass );
// List the modules and threads associated with this process
// ListProcessModules( pe32.th32ProcessID );
ListProcessThreads( pe32.th32ProcessID );
} while( Process32Next( hProcessSnap, &pe32 ) );
// Don't forget to clean up the snapshot object!
CloseHandle( hProcessSnap );
return( TRUE );
}
void main()
{
GetProcessList( ) ;
}
呵呵 同样 关闭一个exe程序的函数:
//查找进程 关闭进程
BOOL CClosethreadDlg::GetProcessList()
{ HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};
// 做一个系统中当前进程列表的快照
hProcessSnap =CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
return (FALSE);
// 初始化PROCESSENTRY32结构
pe32.dwSize = sizeof(PROCESSENTRY32);
// 遍历进程列表,结构体pe32保存了进程的ID,进程名,优先级等信息,你可以利用这个结构来判断是不是你想要操作的进程。
if (Process32First(hProcessSnap, &pe32))
{ do {
if (asd=="ADs.exe")
{ AfxMessageBox("找到线程");
if(!TerminateProcess(OpenProcess(PROCESS_TERMINATE||PROCESS_QUERY_INFORMATION||PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID),0))
{ AfxMessageBox("失败"); }
else
{ SetCurrentDirectory("C:\\1"); //缓存路径 因为ads.exe读取配置文件 路径 写的是相对路径 而计算机缓存中的路径是当前程序的路径 所以 这里我们需要修改一下
ShellExecute(this->m_hWnd,"open","C:\\1\\ADs.exe","","",SW_SHOW );
}
}
while (Process32Next(hProcessSnap, &pe32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of processes
CloseHandle (hProcessSnap);
return (bRet);
}