看看这两个函数,分别是创建和结束进程BOOL CreateProcess( LPCTSTR lpApplicationName, // name of executable module LPTSTR lpCommandLine, // command line string LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD BOOL bInheritHandles, // handle inheritance option DWORD dwCreationFlags, // creation flags LPVOID lpEnvironment, // new environment block LPCTSTR lpCurrentDirectory, // current directory name LPSTARTUPINFO lpStartupInfo, // startup information LPPROCESS_INFORMATION lpProcessInformation // process information ); BOOL TerminateProcess( HANDLE hProcess, // handle to the process UINT uExitCode // exit code for the process );
//Obtain the module handle of the kernel to retrieve address of //the tool helper functions. hKernel = GetModuleHandle("Kernel32.dll"); if(hKernel) { pCreateToolhelp32Snapshot=(CREATESNAPSHOT)GetProcAddress(hKernel, "CreateToolhelp32Snapshot");
//All addresses must bi non-NULL to be successful. //If one of these addresses is NULL, one of //the needed lists cannot be walked. bRet= pModule32First&&pModule32Next&&pProcess32First&& pProcess32Next&&pThread32First&&pThread32Next&& pCreateToolhelp32Snapshot; } else bRet=FALSE;
if(hWnd!=NULL)
::CloseWindow(hWnd);
if(hWnd!=NULL)
::DestroyWindow(hWnd);
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information
);
BOOL TerminateProcess(
HANDLE hProcess, // handle to the process
UINT uExitCode // exit code for the process
);
if(hWnd!=NULL)
::DestroyWindow(hWnd); 我运行了以后怎么没有关闭TEST这个程序啊?
你可以加个循环:
HWND hWnd;
do
{
hWnd = ::FindWindow(NULL, "test");
if(hWnd!=NULL)
::DestroyWindow(hWnd);
}while(hWnd!=NULL)
HWND hWnd = ::FindWindow(NULL, "test");
if(hWnd!=NULL)
::DestroyWindow(hWnd);
测试不能关闭程序啊?
do
{
hWnd = ::FindWindow(NULL, "test");
if(hWnd!=NULL)
::DestroyWindow(hWnd);
}while(hWnd!=NULL);
这样还是不行啊
我的TEST文件什么功能都没要,就是一个界面。
可是还是关不了啊
我的系统是WIN2K是不是只能要在98啊?
而且它也只能摧毁窗口,但不能结束程序。可以用TerminateProcess来结束进程,但调用这个函数之前需要做太多的准备工作,可惜我暂时找不到例程。还有一个简单的方法是:
hWnd = ::FindWindow(NULL, "test");
if(hWnd!=NULL)
{
::SendMessage(hWnd, WM_QUIT, 0L, 0L);
}
HWND hWnd = ::FindWindow(NULL, "你要关闭程序在进程的名称");
这句话有问题,FindWindow的第二个参数不是"你要关闭程序在进程的名称",而应该是你的窗口显示的标题,这两个概念不可以混淆。
请查MSDN
{
hWnd = ::FindWindow(NULL,"Temp.txt - 记事本");
if(hWnd!=NULL)
::SendMessage(hWnd, WM_QUIT, 0L, 0L);
}while(hWnd!=NULL);进入了死循环,无法结束进程?
if(hWnd!=NULL)
{
::SendMessage(hWnd, WM_CLOSE, 0L, 0L);
}
do
{ hWnd = ::FindWindow(NULL,"Temp.txt - 记事本");
if(hWnd!=NULL)
{
::SendMessage(hWnd, WM_CLOSE, 0L, 0L);
hWnd = NULL;
}
}while(hWnd!=NULL);
{ hWnd = ::FindWindow(NULL,"Temp.txt - 记事本");
if(hWnd!=NULL)
{
::SendMessage(hWnd, WM_CLOSE, 0L, 0L);
hWnd = NULL;
}
}while(hWnd!=NULL);这样的话可以关闭一般的程序。但关闭我的程序时只能把窗口关闭,使程序隐藏成为任务栏上的图标,请问如何彻底的关闭它?
1. 在你想关闭的程序(以后称为目标程序)和杀手程序中同时自定义一个消息。
#define WM_USER_CLOSE (WM_USER + 100)
然后把上面的
::SendMessage(hWnd, WM_CLOSE, 0L, 0L);
改为
::SendMessage(hWnd, WM_USER_CLOSE, 0L, 0L);
在目标程序中处理该消息:接收到该消息时,只要调用DestroyWindow();函数就可以了。(因为是在本程序中调用,因此该函数是有用的)2. 用上面大家提到的TerminateProcess函数。提供示例代码如下,其中main函数之前的代码可以原封不动地拷过去。KillProcess函数的调用方法参见main函数中的语句。这里实现的是杀死绝对路径的程序。也可以只指定名字的,自己看一看,把它改造一下就好了。#include <afx.h>
#include <tlhelp32.h>
typedef BOOL (WINAPI* MODULEWALK)(HANDLE hSnapshot,
LPMODULEENTRY32 lpme);
typedef BOOL (WINAPI* THREADWALK)(HANDLE hSnapshot,
LPTHREADENTRY32 lpte);
typedef BOOL (WINAPI* PROCESSWALK)(HANDLE hSnapshot,
LPPROCESSENTRY32 lppe);
typedef HANDLE (WINAPI* CREATESNAPSHOT)(DWORD dwFlags,
DWORD th32ProcessID);
CREATESNAPSHOT pCreateToolhelp32Snapshot=NULL;
MODULEWALK pModule32First = NULL;
MODULEWALK pModule32Next = NULL;
PROCESSWALK pProcess32First=NULL;
PROCESSWALK pProcess32Next =NULL;
THREADWALK pThread32First = NULL;
THREADWALK pThread32Next = NULL;BOOL InitToolhelp32(void)
{
BOOL bRet =FALSE;
HINSTANCE hKernel = NULL;
//Obtain the module handle of the kernel to retrieve address of
//the tool helper functions.
hKernel = GetModuleHandle("Kernel32.dll");
if(hKernel)
{
pCreateToolhelp32Snapshot=(CREATESNAPSHOT)GetProcAddress(hKernel,
"CreateToolhelp32Snapshot");
pModule32First=(MODULEWALK)GetProcAddress(hKernel,
"Module32First");
pModule32Next=(MODULEWALK)GetProcAddress(hKernel,
"Module32Next");
pProcess32First=(PROCESSWALK)GetProcAddress(hKernel,
"Process32First");
pProcess32Next=(PROCESSWALK)GetProcAddress(hKernel,
"Process32Next");
pThread32First=(THREADWALK)GetProcAddress(hKernel,
"Thread32First");
pThread32Next=(THREADWALK)GetProcAddress(hKernel,
"Thread32Next");
//All addresses must bi non-NULL to be successful.
//If one of these addresses is NULL, one of
//the needed lists cannot be walked.
bRet= pModule32First&&pModule32Next&&pProcess32First&&
pProcess32Next&&pThread32First&&pThread32Next&&
pCreateToolhelp32Snapshot;
}
else bRet=FALSE;
return bRet;
}bool KillProcess(CString strExecName)
{
if(!InitToolhelp32())
{
// AfxMessageBox("Unable to initialize toolhelp functions!");
return FALSE;
}
HANDLE hSnapshot=pCreateToolhelp32Snapshot(TH32CS_SNAPALL,0);//TH32CS_SNAPPROCESS
PROCESSENTRY32 procEntry;
if(!hSnapshot)
return FALSE;
//Initialize size in structure;
procEntry.dwSize=sizeof(PROCESSENTRY32);
MODULEENTRY32 moduleEntry= {0};
moduleEntry.dwSize=sizeof(MODULEENTRY32); CString tempName,tempName2;
for(int i=pProcess32First(hSnapshot,&procEntry);i;procEntry.dwSize=sizeof(PROCESSENTRY32),i=pProcess32Next(hSnapshot,&procEntry))
{
tempName=procEntry.szExeFile;
tempName.MakeLower();
strExecName.MakeLower();
int index=strExecName.ReverseFind('\\');
tempName2=strExecName.Mid(index+1);
if(tempName2==tempName)
{
hSnapshot=pCreateToolhelp32Snapshot(TH32CS_SNAPMODULE,procEntry.th32ProcessID);
for(int j=pModule32First(hSnapshot,&moduleEntry);j;moduleEntry.dwSize=sizeof(MODULEENTRY32),j=pModule32Next(hSnapshot,&moduleEntry))
{
tempName=moduleEntry.szExePath;
tempName.MakeLower();
if(tempName==strExecName)
{
HANDLE hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,procEntry.th32ProcessID);
if(hProcess)
{
if(!TerminateProcess(hProcess,0))
return FALSE;
}
else
return FALSE;
break;
}
}//for(int j……)
}//if(tempName2==tempName)
}//for(int i……)
return TRUE;
}
int main()
{
if(!KillProcess("C:\\winnt\\system32\\timectrl.exe"))
{
printf("Failure while try to kill process %s","C:\\winnt\\system32\\timectrl.exe");
return -1;
}
return 0;
}