How can I get a program's process id ?

How can I get a program's process id ?

Postby HunterEC » Sat Nov 29, 2008 1: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.
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Postby Antonio Linares » Sat Nov 29, 2008 8: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby HunterEC » Sat Nov 29, 2008 8: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.
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Postby Antonio Linares » Sat Nov 29, 2008 8:53 am

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

GetWindowTask() is obsolete, according to Microsoft, but keeps working fine :-)
Last edited by Antonio Linares on Sat Nov 29, 2008 8:59 am, edited 1 time in total.
regards, saludos

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

Postby Antonio Linares » Sat Nov 29, 2008 8:56 am

Gustavo,

add this code to your PRG:
Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby HunterEC » Sat Nov 29, 2008 9: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.
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Postby Antonio Linares » Sat Nov 29, 2008 9:18 am

Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby HunterEC » Sat Nov 29, 2008 9: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
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Postby Antonio Linares » Sat Nov 29, 2008 9:49 am

This line should be this way:

TerminateProcess( GetWindowTask( hTask ), 1 )
regards, saludos

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

Postby HunterEC » Sat Nov 29, 2008 9:54 am

Antonio:

Now I'm getting the following error:

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

Thank you.
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Postby Antonio Linares » Sat Nov 29, 2008 10:00 am

ops,

TerminateProcess( GetWndTask( hTask ), 1 )
regards, saludos

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

Postby HunterEC » 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 ?
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 31 guests