Page 1 of 1

How can I get a program's process id ?

Posted: Sat Nov 29, 2008 1:22 am
by HunterEC
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.

Posted: Sat Nov 29, 2008 8:09 am
by Antonio Linares
Gustavo,

Do you know the caption of its main window ?

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

Posted: Sat Nov 29, 2008 8:24 am
by HunterEC
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.

Posted: Sat Nov 29, 2008 8:53 am
by Antonio Linares
http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx

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

Posted: Sat Nov 29, 2008 8:56 am
by Antonio Linares
Gustavo,

add this code to your PRG:

Code: Select all | Expand

#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

Posted: Sat Nov 29, 2008 9:11 am
by HunterEC
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.

Posted: Sat Nov 29, 2008 9:18 am
by Antonio Linares

Code: Select all | Expand

#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

Posted: Sat Nov 29, 2008 9:32 am
by HunterEC
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

Posted: Sat Nov 29, 2008 9:49 am
by Antonio Linares
This line should be this way:

TerminateProcess( GetWindowTask( hTask ), 1 )

Posted: Sat Nov 29, 2008 9:54 am
by HunterEC
Antonio:

Now I'm getting the following error:

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

Thank you.

Posted: Sat Nov 29, 2008 10:00 am
by Antonio Linares
ops,

TerminateProcess( GetWndTask( hTask ), 1 )

Posted: Sat Nov 29, 2008 10:17 am
by HunterEC
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 ?