终止过程的问题 [英] problem of terminating a process

查看:109
本文介绍了终止过程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中添加了终止代码,但是有一个有线问题,我的程序用于安装驱动程序,问题是代码终止进程时.我的安装应该成功,但是仍然失败.当我添加时我使用MessageBox()进行调试的所有调试信息,在终止该过程之后,安装工作均正常进行.任何人都知道如何解决此问题或如何找到问题所在.非常感谢.希望我清楚地说明了问题.
这是代码:

I added a terminate code in my program,but there is a wired problem,my program is used for installing drivers,The problem is when the code terminate the process.my installation should successed,but it still failed .when i add all my debug information which i used MessageBox() for Debugging,the installation worked perfectly after terminate the process .anyone know how to solve this or how to find where is the problem .thanks very much .hope i explained the problem clearly .
here are the codes:

CString strProcess = "install.exe";
	HANDLE hProcessSnap;
	DWORD dwID = 0;
	PROCESSENTRY32 pe32={0};
 
	// Take a snapshot of all processes in the system
	hProcessSnap=CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS,0);
	if(hProcessSnap == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox(_T("create snapshoot failed"));
	}
	BOOL Res = Process32First(hProcessSnap,&pe32);
	//add new function here
	while(Res) 
   { 
	if(strProcess.CompareNoCase(pe32.szExeFile)==0) 
    { 
		DWORD dwExitCode = pe32.th32ProcessID;
		HANDLE   hProcess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID); 
		::GetExitCodeProcess(hProcess,&dwExitCode);
		Res = ::TerminateProcess(hProcess,dwExitCode);
    } 
    Res=Process32Next(hProcessSnap,&pe32); 
    ::CloseHandle(hProcessSnap); 
   }




我刚刚发现,在终止正在使用的install.exe程序后,安装过程必须将新的install.exe int复制到Windows目录,因为此副本出错了.无法将install.exe复制到我想要的目录.




i just found out that,after i terminate the install.exe program which is in use.the install process have to copy a new install.exe int to windows directory asn this copy went wrong .it failed to copy the install.exe to the directory i wanted it to be.

推荐答案

我刚刚浏览了您的代码

I just went through your code

while(Res) 
   { 
	if(strProcess.CompareNoCase(pe32.szExeFile)==0) 
    { 
		DWORD dwExitCode = pe32.th32ProcessID;
		HANDLE   hProcess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID); 
		::GetExitCodeProcess(hProcess,&dwExitCode);
		Res = ::TerminateProcess(hProcess,dwExitCode);
    } 
    Res=Process32Next(hProcessSnap,&pe32); 
    ::CloseHandle(hProcessSnap); 
   }DWORD dwExitCode = pe32.th32ProcessID;
		HANDLE   hProcess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID); 
		::GetExitCodeProcess(hProcess,&dwExitCode);
		Res = ::TerminateProcess(hProcess,dwExitCode);



我有一些评论!!

-终止进程时,至少检查是否处于活动状态,提示:使用waitforsingleobject
-为什么在实际调用终止进程api之前得到GetExitCodeProcess? -您不能在install.exe中提供简单的关闭介质,而是利用危险的terminateprocess api.提示:窗口消息WM_QUIT!

其余的将再次进行并进行更新!



i have some review comments!

- when terminating the process, atleast check is it active or not, hint: use waitforsingleobject
- why are you getting GetExitCodeProcess before actually calling terminateprocess api
- can''t you provide simple close down medium in your install.exe instead utilizing dangerous terminateprocess api. hint: window message WM_QUIT!

rest will go through again and will update it!


这篇关于终止过程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆