Paradox Community
Search:

 Welcome |  What is Paradox |  Paradox Folk |  Paradox Solutions |
 Interactive Paradox |  Paradox Programming |  Internet/Intranet Development |
 Support Options |  Classified Ads |  Wish List |  Submissions 


Paradox Programming Articles  |  Beyond Help Articles  |  Tips & Tricks Articles  


Tracking External Programs from ObjectPAL
© 2002 Vladimir Menkin


Consider the following situation. You need to start via ObjectPAL an external program, allow the user to continue to work in Paradox while this program is running, and to close this program (or to check, if this program is still alive) when the user exits the Paradox application. Real life example: the database contains records with links to Word documents, and the user can invoke Word (via ObjectPAL code) to edit the document. When the user closes the document, the program must update the database. And, of course, the user isn't allowed to exit Paradox without exiting Word first. Using execute() or WinAPI ShellExecute doesn't allow us to do this job. The technique described below shows a way how it can be done.
Uses KERNEL32
   GlobalAlloc(wFlags CLONG,dwBytes CLONG) CLONG
   RtlMoveMemoryInVar(hpvDest CPTR,
                      hpvSource CLONG,
                      cbCopy CLONG) CLONG ["RtlMoveMemory"]
   RtlMoveMemoryFromVar(hpvDest CLONG,
                        hpvSource CPTR,
                        cbCopy CLONG) CLONG ["RtlMoveMemory"]
   GlobalFree(hMem CLONG) CLONG
   GetExitCodeProcess(pid CLONG, lpExitCode CPTR) CLONG
   CreateProcessA
   (lpApplicationName CLONG,
    lpCommandLine CPTR,
    lpProcessAttributes CLONG,
    lpThreadAttributes CLONG,
    bInheritHandles CLONG,
    dwCreationFlags CLONG,
    lpEnvironment CLONG,
    lpCurrentDriectory CLONG,
    lpStartupInfo CLONG,
    lpProcessInformation CLONG
   ) CLONG
endUses

method ExecProg(ExeName string, Params string) longint
;Starts prgram ExeName with command line parameters Params
;Returns 0 if error, otherwise - Process ID (pid)
const
  SW_SHOWNORMAL = 1
  SW_SHOWMINIMIZED = 2
  SW_SHOWMAXIMIZED = 3
endconst
var
  SInfo,Pinfo,adr,pid longint
endvar
if exename="" then return 0 endif
if params>"" then params=" \""+params+" \"" endif
SInfo=GlobalAlloc(fromhex("0x40"),68)
adr=SInfo
RtlMoveMemoryFromVar(adr,longint(68),4)
adr=adr+44
RtlMoveMemoryFromVar(adr,longint(1),4);dwFlags=STARTF_USESHOWWINDOW
adr=adr+4
RtlMoveMemoryFromVar(adr,smallint(SW_SHOWNORMAL),2);wShowWindow
Pinfo=GlobalAlloc(fromhex("0x40"),16)
if CreateProcessA(0,exename+params,0,0,0,0,0,0,sInfo,pInfo)=0 then
  return 0
endif
RtlMoveMemoryInVar(pid,Pinfo,4)
GlobalFree(SInfo)
GlobalFree(PInfo)
return pid
endMethod

method IsProgramAlive(pid longint) logical
;Returns true, if the program with the given PID is still running,
;otherwise - false
var
  res longint
endvar
if GetExitCodeProcess(pid,res)<>0 then
  if res=259 then
    return true
  endif
endif
return false
endMethod


Discussion of this article


 Feedback |  Paradox Day |  Who Uses Paradox |  I Use Paradox |  Downloads 


 The information provided on this Web site is not in any way sponsored or endorsed by Corel Corporation.
 Paradox is a registered trademark of Corel Corporation.


 Modified: 15 May 2003
 Terms of Use / Legal Disclaimer


 Copyright © 2001- 2003 Paradox Community. All rights reserved. 
 Company and product names are trademarks or registered trademarks of their respective companies. 
 Authors hold the copyrights to their own works. Please contact the author of any article for details.