Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C++
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); 
   }



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.
Posted
Updated 4-Jul-11 17:37pm
v3
Comments
Christian Graus 4-Jul-11 21:06pm    
If the installation worked, what makes you say it failed ? What's actually going wrong ?
markfilan 4-Jul-11 21:53pm    
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.
markfilan 4-Jul-11 23:17pm    
I solve the problem ,thanks for ur help~
ThatsAlok 5-Jul-11 7:51am    
Does it's the project requirement to use terminateprocess, can't you programme in a way to provide some clean shutdown operation for your executable
markfilan 5-Jul-11 20:39pm    
you mean, terminateeprocess() is not a good choice?or i should use exitprocess() which is much safe.the executable file I'm shutdown is a background program, there is no winodw for the program.all i can think of is terminateprocess()

1 solution

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);


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!
 
Share this answer
 
Comments
markfilan 5-Jul-11 20:39pm    
Thanks , i know that terminateprogress() is not a choice ,but i can not think a better way to do it ,or i should use exitprogress()which is much safe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900