Page 1 of 1
Kill Process at end of program ?
Posted: Sun Jan 21, 2007 5:38 pm
by Richard Chidiak
Antonio,
In many cases especially when using Ftp, the program remains in memory after quitting the app , all connexions are properly closed by the app.
Is there a way to "kill all process issued by the program" ? and make sure it is ended in anyway ?
I often have to reset completely my Hp Ipaq to run my app again.
Thanks for your help,
Richard
Posted: Sun Jan 21, 2007 7:40 pm
by pawelu
Richard,
I use this code for testing "sleep" program process.
Regards
Pawel
Code: Select all
#define MAX_TASKS 256
#define PROCESS_SIZE 128
#define TITLE_SIZE 64
typedef struct _TASK_LIST
{
DWORD cntUsage;
DWORD dwProcessId;
DWORD dwInheritedFromProcessId;
DWORD cntThreads;
DWORD th32ModuleID;
DWORD th32DefaultHeapID;
LONG pcPriClassBase;
BOOL flags;
HWND hwnd;
TCHAR ProcessName[PROCESS_SIZE];
TCHAR WindowTitle[TITLE_SIZE];
}
TASK_LIST, *PTASK_LIST;
DWORD GetTaskListCE (PTASK_LIST pTask, DWORD dwNumTasks);
BOOL KillProcess (PTASK_LIST tlist, BOOL fForce = TRUE);
HB_FUNC (APPTASK)
{
DWORD i = 0, j = 0, k = 0;
TASK_LIST tlist[MAX_TASKS];
DWORD numTasks;
BOOL bCurrent = ISNIL (1) ? TRUE : hb_parl (1);
BOOL bRet = FALSE;
k = GetCurrentProcessId ();
memset (&tlist, 0, sizeof (TASK_LIST) * MAX_TASKS);
numTasks = GetTaskListCE (tlist, MAX_TASKS);
for (i = 0; i < numTasks; i ++)
{
if (bCurrent || k != tlist[i].dwProcessId && _tcscmp (_tcsupr (tlist[i].ProcessName), TEXT ("APPNAME.EXE")) == 0)
{
KillProcess (&tlist[i], TRUE);
bRet = TRUE;
}
}
hb_retl (bRet);
}
DWORD GetTaskListCE (PTASK_LIST pTask, DWORD dwNumTasks)
{
HINSTANCE hProcessSnap = NULL;
PROCESSENTRY32 pe32 = {0};
DWORD dwTaskCount = 0;
if (dwNumTasks == 0) return 0;
hProcessSnap = (HINSTANCE) CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == (HANDLE) -1) return 0;
dwTaskCount = 0;
pe32.dwSize = sizeof (PROCESSENTRY32);
if (Process32First (hProcessSnap, &pe32))
{
do
{
LPTSTR pCurChar;
if (_tcsstr (pe32.szExeFile, L"\\"))
pCurChar = _tcsrchr (pe32.szExeFile, '\\');
else
pCurChar = pe32.szExeFile;
lstrcpy (pTask->ProcessName, pCurChar);
pTask->flags = 0;
pTask->dwProcessId = pe32.th32ProcessID;
pTask->cntThreads = pe32.cntThreads;
pTask->cntUsage = pe32.cntUsage;
pTask->dwInheritedFromProcessId = pe32.th32ParentProcessID;
pTask->th32ModuleID = pe32.th32ModuleID;
++ dwTaskCount;
++ pTask;
}
while (dwTaskCount < dwNumTasks && Process32Next (hProcessSnap, &pe32));
}
else dwTaskCount = 0;
CloseHandle (hProcessSnap);
CloseToolhelp32Snapshot (hProcessSnap);
return dwTaskCount;
}
BOOL KillProcess (PTASK_LIST tlist, BOOL fForce)
{
HANDLE hProcess;
if (fForce || !tlist->hwnd)
{
hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, tlist->dwProcessId);
if (hProcess)
{
hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, tlist->dwProcessId);
if (hProcess == NULL)
{
return FALSE;
}
if (!TerminateProcess (hProcess, 1))
{
CloseHandle (hProcess);
return FALSE;
}
CloseHandle (hProcess);
return TRUE;
}
}
PostMessage (tlist->hwnd, WM_CLOSE, 0, 0);
return TRUE;
}
Posted: Sun Jan 21, 2007 7:43 pm
by Richard Chidiak
Thank you
Best regards
Richard
Posted: Sun Jan 21, 2007 7:43 pm
by Antonio Linares
Richard,
Have you tried TerminateProcess( GetModuleHandle( 0 ), 0 ) ?
Posted: Sun Jan 21, 2007 8:00 pm
by Richard Chidiak
Antonio
I will try this solution first and report if it is OK
Thanks
Richard
Posted: Mon Jan 22, 2007 11:00 am
by Richard Chidiak
Antonio,
Your solution is fine to "end the program" , it works OK.
I like Pawel's idea of killing all sleeping process. I think about a gprs connexion that did not close properly etc.. .This can be a great add on to fwppc at the end of the app if it could be executed auomatically.
I am having problems compiling pawel's code.
Do i need any particular header files different than the standard ones windows.h and hbapi.h ?
Thanks for the help
Richard
Posted: Tue Jan 23, 2007 8:47 am
by pawelu
Richard,
Add your source tlhelp32.h and link app with toolhelp.lib.
Regards
Pawel