How to close the Opened process.
-
I am using following code to create the new process. Now I want to Close the process that I have created. I am using
CloseHandle()
to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
{
TCHAR str[100] ={};
_tcscpy_s( str, 100, CmdLine );STARTUPINFO si; PROCESS\_INFORMATION pi; BOOL ProcCreated; ZeroMemory(&si,sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF\_USESHOWWINDOW; si.wShowWindow = SW\_SHOWMAXIMIZED; ZeroMemory(&pi,sizeof(pi)); //si.lpDesktop = \_T("NewDesktop"); si.lpDesktop = NULL; ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi ); return pi;
}
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
PROCESS_INFORMATION pi;
DWORD dError;.
do
{
scanf("%c",&ch);
if(ch == 'c')
pi=OpenProc(L"MSPAINT");
if(ch == 't')
{
CloseHandle(pi.hProcess);
dError = GetLastError(); //Error = 0
CloseHandle(pi.hThread);
dError = GetLastError(); //dError =0
}} while(ch != 'E');
}
[ Screen Capture ][ Tool Tip ]
-
I am using following code to create the new process. Now I want to Close the process that I have created. I am using
CloseHandle()
to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
{
TCHAR str[100] ={};
_tcscpy_s( str, 100, CmdLine );STARTUPINFO si; PROCESS\_INFORMATION pi; BOOL ProcCreated; ZeroMemory(&si,sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF\_USESHOWWINDOW; si.wShowWindow = SW\_SHOWMAXIMIZED; ZeroMemory(&pi,sizeof(pi)); //si.lpDesktop = \_T("NewDesktop"); si.lpDesktop = NULL; ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi ); return pi;
}
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
PROCESS_INFORMATION pi;
DWORD dError;.
do
{
scanf("%c",&ch);
if(ch == 'c')
pi=OpenProc(L"MSPAINT");
if(ch == 't')
{
CloseHandle(pi.hProcess);
dError = GetLastError(); //Error = 0
CloseHandle(pi.hThread);
dError = GetLastError(); //dError =0
}} while(ch != 'E');
}
[ Screen Capture ][ Tool Tip ]
Why you dont use of
TerminateProcess
? -
I am using following code to create the new process. Now I want to Close the process that I have created. I am using
CloseHandle()
to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
{
TCHAR str[100] ={};
_tcscpy_s( str, 100, CmdLine );STARTUPINFO si; PROCESS\_INFORMATION pi; BOOL ProcCreated; ZeroMemory(&si,sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF\_USESHOWWINDOW; si.wShowWindow = SW\_SHOWMAXIMIZED; ZeroMemory(&pi,sizeof(pi)); //si.lpDesktop = \_T("NewDesktop"); si.lpDesktop = NULL; ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi ); return pi;
}
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
PROCESS_INFORMATION pi;
DWORD dError;.
do
{
scanf("%c",&ch);
if(ch == 'c')
pi=OpenProc(L"MSPAINT");
if(ch == 't')
{
CloseHandle(pi.hProcess);
dError = GetLastError(); //Error = 0
CloseHandle(pi.hThread);
dError = GetLastError(); //dError =0
}} while(ch != 'E');
}
[ Screen Capture ][ Tool Tip ]
-
Why you dont use of
TerminateProcess
?I used the Code Like this to terminate the process. The code works fine when the Created Process is in the same desktop. But It doesnn't success when the Process created is in **another Desktop.**It doesn't show any Error but it doesn't close the opened process.
//dwExitCode = 259 Always. if process is in another Desktop
GetExitCodeProcess(pi.hProcess,&dwExitCode);
TerminateProcess(pi.hProcess,dwExitCode);[ Screen Capture ][ Tool Tip ]
-
I am using following code to create the new process. Now I want to Close the process that I have created. I am using
CloseHandle()
to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
{
TCHAR str[100] ={};
_tcscpy_s( str, 100, CmdLine );STARTUPINFO si; PROCESS\_INFORMATION pi; BOOL ProcCreated; ZeroMemory(&si,sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF\_USESHOWWINDOW; si.wShowWindow = SW\_SHOWMAXIMIZED; ZeroMemory(&pi,sizeof(pi)); //si.lpDesktop = \_T("NewDesktop"); si.lpDesktop = NULL; ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi ); return pi;
}
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
PROCESS_INFORMATION pi;
DWORD dError;.
do
{
scanf("%c",&ch);
if(ch == 'c')
pi=OpenProc(L"MSPAINT");
if(ch == 't')
{
CloseHandle(pi.hProcess);
dError = GetLastError(); //Error = 0
CloseHandle(pi.hThread);
dError = GetLastError(); //dError =0
}} while(ch != 'E');
}
[ Screen Capture ][ Tool Tip ]
GauranG33 wrote:
TCHAR str[100] ={}; ... DWORD dError;.
These two statements are in error. That has no bearing on your problem, however, but it does make it somewhat difficult to debug something with syntax errors.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I am using following code to create the new process. Now I want to Close the process that I have created. I am using
CloseHandle()
to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
{
TCHAR str[100] ={};
_tcscpy_s( str, 100, CmdLine );STARTUPINFO si; PROCESS\_INFORMATION pi; BOOL ProcCreated; ZeroMemory(&si,sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF\_USESHOWWINDOW; si.wShowWindow = SW\_SHOWMAXIMIZED; ZeroMemory(&pi,sizeof(pi)); //si.lpDesktop = \_T("NewDesktop"); si.lpDesktop = NULL; ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi ); return pi;
}
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
PROCESS_INFORMATION pi;
DWORD dError;.
do
{
scanf("%c",&ch);
if(ch == 'c')
pi=OpenProc(L"MSPAINT");
if(ch == 't')
{
CloseHandle(pi.hProcess);
dError = GetLastError(); //Error = 0
CloseHandle(pi.hThread);
dError = GetLastError(); //dError =0
}} while(ch != 'E');
}
[ Screen Capture ][ Tool Tip ]
Send the opened process a WM_CLOSE message. Just closing your handle to it does not close the process. You've received some suggestions to use TerminateProcess - that is the last way you should try if you can't close a process nicely. Using TP can causes memory leaks and issues if the process is using any kind of inter-process communication. It is a function-of-last-resort. Judy
-
I used the Code Like this to terminate the process. The code works fine when the Created Process is in the same desktop. But It doesnn't success when the Process created is in **another Desktop.**It doesn't show any Error but it doesn't close the opened process.
//dwExitCode = 259 Always. if process is in another Desktop
GetExitCodeProcess(pi.hProcess,&dwExitCode);
TerminateProcess(pi.hProcess,dwExitCode);[ Screen Capture ][ Tool Tip ]
Can you get handle to this window and then send WM_CLOSE to it?