FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How can I get a program's process id ?
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 01:22 AM

Is there a way to close a program that was left on purpose open ? I need to close it after the user has finished the current transaction.

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 08:09 AM

Gustavo,

Do you know the caption of its main window ?

TerminateProcess( GetWindowTask( FindWindow( "window caption", 0 ) ) )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 08:24 AM
Antonio:

I do know the caption and can set it up as a unique one. Where I can find the description for these two functions ?

TerminateProcess( GetWindowTask(

Working with the TerminateApp the linker gives me this error:

Error: Unresolved external '_HB_FUN_TERMINATEAPP' referenced from ...


I know there's a LIB or include missing. Please shine some light on these 2 issues. Thank you very much.
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 08:53 AM

http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx

GetWindowTask() is obsolete, according to Microsoft, but keeps working fine :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 08:56 AM
Gustavo,

add this code to your PRG:
#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

HB_FUNC( TERMINATEPROCESS )
{
   hb_retl( TerminateProcess( ( HANDLE ) hb_parnl( 1 ),
                        IF( PCOUNT() > 1, hb_parni( 2 ), 0 ) ) );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 09:11 AM
Antonio:

Thank you. Now I got a couple of warnings and errors:

Warning W8065 PKILL.prg 18: Call to function 'PCOUNT' with no prototype in function HB_FUN_TERMINATEPROCESS
Warning W8065 PKILL.prg 18: Call to function 'IF' with no prototype in functio
n HB_FUN_TERMINATEPROCESS
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external '_PCOUNT' referenced from F:\PKILL.OBJ
Error: Unresolved external '_IF' referenced from F:\PKILL.OBJ



Thank you for your help.
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 09:18 AM
#pragma BEGINDUMP 

#include <hbapi.h> 
#include <windows.h> 

#define IF(x,y,z) ((x)?(y):(z))

HB_FUNC( TERMINATEPROCESS ) 
{ 
   hb_retl( TerminateProcess( ( HANDLE ) hb_parnl( 1 ), 
                        IF( hb_pcount() > 1, hb_parni( 2 ), 0 ) ) ); 
} 

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 09:32 AM
Antonio:

Everything compiled and linked OK, but I can't get to terminate the program.


PROCEDURE Main
LOCAL hTask

hTask := FindWindow("6.0" )
ALERT("hTask: " + STR(hTask,5), {" Ok "})
TerminateProcess(hTask, 1 )
RETURN



Program that needs to be closed:

...
DEFINE DIALOG oDlg TITLE "6.0"
oDlg:nTop := GetSysMetrics(1) - 250 - 65
oDlg:nLeft := GetSysMetrics(0) - 377 - 10
oDlg:nBottom := GetSysMetrics(1) - 65
oDlg:nRight := GetSysMetrics(0) - 10

...
ACTIVATE DIALOG oDlg
RETURN
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 09:49 AM

This line should be this way:

TerminateProcess( GetWindowTask( hTask ), 1 )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 09:54 AM
Antonio:

Now I'm getting the following error:

Error: Unresolved external '_HB_FUN_GETWINDOWTASK' referenced from F:\PKILL.OBJ

Thank you.
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 10:00 AM

ops,

TerminateProcess( GetWndTask( hTask ), 1 )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
How can I get a program's process id ?
Posted: Sat Nov 29, 2008 10:17 AM
Antonio:

Everything compiled & linked ok. The FindWindow function gets the correct hWnd value but the TerminateProcess( GetWndTask( hTask ), 1 ) does not close the application. Any clues ?

Continue the discussion