Silverfrost Forums

Welcome to our forums

Process Handle

17 Jan 2018 9:32 #21152

How can I get the handle of a process that I started with START_PROCESS_AND_WAIT@ in a new thread?

!-----------------------------------------------------------------------------------

    INTEGER FUNCTION ModWait (ProgramName, ParamString, iSilent) 
    
    ! Start program in separate thread which waits for completion

    USE      ProcNameS
    IMPLICIT NONE
    CHARACTER*(*), INTENT(IN) :: ProgramName, ParamString
    INTEGER,       INTENT(IN) :: iSilent

    INTEGER  :: iThID, NULL4b
    INTEGER  :: iThread
    INTEGER, EXTERNAL :: WaitForBingo

!   STDCALL CreateThread 'CreateThread' (REF,VAL,REF,REF,VAL,REF) : INTEGER*4 
!   Original modified to call with NULL pointer:

    STDCALL CreateThread 'CreateThread' (VAL,VAL,REF,REF,VAL,REF) : INTEGER*4
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NULL4b = 0
    iThID  = 0
       :
       :
    iThread = CreateThread (NULL4b, NULL4b, WaitForBingo, iSilent, NULL4b, iThID)
       :
       :
    RETURN
    END FUNCTION ModWait
!-----------------------------------------------------------------------------------

    INTEGER FUNCTION WaitForBingo (iSilent) 

    ! Start program and wait for completion

    USE      ProcNameS
    IMPLICIT NONE
    INCLUDE  <clearwin.ins>
    INTEGER, INTENT(IN) :: iSilent

	CHARACTER   CTX*256, cProgName*128, cRunString*128 
    INTEGER     iStat
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       :
       :
    iStat = START_PROCESS_AND_WAIT@ (cProgName, cRunString, -1)
       :
       :
    WaitForBingo = 1
    RETURN
    END FUNCTION WaitForBingo
!-----------------------------------------------------------------------------------
17 Jan 2018 10:02 #21153

If your spawned program cProgName was written by you, you can give it a class name through Clearwin+, and send messages to it. Inside cProgName you would need something like:

   IA=WINIO@('%nc[cProgClassName]%rm&amp;',MESSAGE_REPLY_FN)

in which MESSAGE_REPLY_FN is an integer function that responds to Windows messages within program cProgName. Messages could be sent from the parent program, or indeed, sent from the spawned program back to the parent.

You will also need to use CALL REPLY_TO_TEXT_MESSAGE@ and SEND_TEXT_MESSAGE@ (described in FTN95.CHM) and if communication back to the parent is required, it will also need its own class name and message handler.

If you didn't write cProgName, then you will need a Windows utility to discover its class name.

A way of doing this and finding the program handle is given here:

https://stackoverflow.com/questions/4727135/how-to-find-out-class-name-tiled-of-program-in-c

17 Jan 2018 10:10 #21154

That is already in use, but not thread-save.

17 Jan 2018 10:30 #21155

Interesting. Clearly, the software side of things lags behind the hardware, as the latter is very capable of handling threads, but Windows may not be. It's only to be expected therefore that other software lags even further behind.

A bit like 64-bit ...

17 Jan 2018 11:58 #21156

The Silverfrost libraries do not provide a way to get this handle. At the moment I can't think of an alternative approach other than to write your own call to CreatProcess.

17 Jan 2018 12:40 #21157

Paul,

my point is to get a return code within 'WaitForBingo' (one integer or string) from the program started with START_PROCESS_AND_WAIT@. That program is written in Intel Fortran.

17 Jan 2018 3:22 #21159

Erwin

I am not sure that I follow. How would you use this returned handle?

17 Jan 2018 3:48 #21160

Paul,

I have a cycle to start two programs in sequence several times, creating a new thread for every start. This cause problems in 64 bit version. Therefore I want to do this cycle within one thread. To decide what's to do next, I need the return code from the first program. The first program can provide the return code via VOID WINAPI ExitProcess( __in UINT uExitCode );

The cycle in the tread could get the return code using BOOL WINAPI GetExitCodeProcess( __in HANDLE hProcess, __out LPDWORD lpExitCode );

This needs the process handle.

17 Jan 2018 5:03 #21161

Erwin

I could provide a routine that takes an address from you and plants the process handle into that address.

I notice that you are using INTEGER4 for the return from CreateThread and that should be INTEGER8 for 64 bits.

17 Jan 2018 5:08 #21162

perfect

24 Jan 2018 10:37 #21208

Erwin

It seems like my personal messages are not getting through to you. Are you reading your new messages?

24 Jan 2018 12:00 #21209

Thanks Paul,

now I received 3 emails and found your message in my inbox. I will start testing immediately.

Erwin

Please login to reply.