Kill Process at end of program ?

Kill Process at end of program ?

Postby Richard Chidiak » Sun Jan 21, 2007 5:38 pm

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
http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
User avatar
Richard Chidiak
 
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France

Postby pawelu » Sun Jan 21, 2007 7:40 pm

Richard,

I use this code for testing "sleep" program process.

Regards
Pawel

Code: Select all  Expand view
#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;
}
pawelu
 
Posts: 126
Joined: Thu Oct 06, 2005 10:18 pm
Location: Poland

Postby Richard Chidiak » Sun Jan 21, 2007 7:43 pm

Thank you

Best regards


Richard
http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
User avatar
Richard Chidiak
 
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France

Postby Antonio Linares » Sun Jan 21, 2007 7:43 pm

Richard,

Have you tried TerminateProcess( GetModuleHandle( 0 ), 0 ) ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Richard Chidiak » Sun Jan 21, 2007 8:00 pm

Antonio

I will try this solution first and report if it is OK

Thanks

Richard
http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
User avatar
Richard Chidiak
 
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France

Postby Richard Chidiak » Mon Jan 22, 2007 11:00 am

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
http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
User avatar
Richard Chidiak
 
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France

Postby pawelu » Tue Jan 23, 2007 8:47 am

Richard,

Add your source tlhelp32.h and link app with toolhelp.lib.

Regards
Pawel
pawelu
 
Posts: 126
Joined: Thu Oct 06, 2005 10:18 pm
Location: Poland


Return to FiveWin for Pocket PC

Who is online

Users browsing this forum: No registered users and 15 guests