General information
Category:
Windows API functions
OK, I had expected you to use the ShellExecuteEx. This passes back a window handle which you can test for with the API IsWindow.
Here is the VFP code which uses a similar method. It is a hack of Ed's stuff, so any credits should go to him.
Just pass in the app that you want to launch, a boolean if you want to wait, and the mode of the window you would like.
Mace
*****************************************************************************
*!* PROCEDURE LaunchApp
*****************************************************************************
PROCEDURE LaunchApp
LPARAMETERS tcCommandLine, tlAndWait, tcWindowMode
LOCAL lnStatus
LOCAL lcProcessInfo, lcStartUpInfo, lnProcessHandle
*-- Make default Structures for the CreateProcess call
*-- ProcessInfo - 4 bytes, a Process handle, a Thread Handle, a (DWORD) ProcessId and a (DWORD) ThreadID
lcProcessInfo = REPL(CHR(0),16)
*-- StartUpInfo is a 68 byte long complex structure; we either have 68 bytes with a cb member (byte 1) 68
*-- or with cb of 68, dwFlag low order byte (byte 45) of 1, and low order byte wShowWindow (byte 49) set to
*-- the SW_ value appropriate for the Window Mode desired.
DO CASE
CASE EMPTY( tcWindowMode)
*-- Use default of application
lcStartUpInfo = CHR(68) + REPL(CHR(0),67)
CASE tcWindowMode = 'HID'
*-- Hide - use STARTF_USESHOWFLAG and value of 0
lcStartUpInfo = CHR(68) + ;
REPL(CHR(0),43) + ;
CHR(1) + ;
REPL(CHR(0),23)
CASE tcWindowMode = 'NOR'
*-- Normal - use STARTF_USESHOWFLAG and value of 1
lcStartUpInfo = CHR(68) + ;
REPL(CHR(0),43) + ;
CHR(1) + ;
REPL(CHR(0),3) + ;
CHR(1) + ;
REPL(CHR(0),19)
CASE tcWindowMode = 'MIN'
*-- Minimize - use STARTF_USESHOWFLAG and value of 2
lcStartUpInfo = CHR(68) + ;
REPL(CHR(0),43) + ;
CHR(1) + ;
REPL(CHR(0),3) + ;
CHR(2) + ;
REPL(CHR(0),19)
CASE tcWindowMode = 'MAX'
*-- Maximize - use STARTF_USESHOWFLAG and value of 3
lcStartUpInfo = CHR(68) + ;
REPL(CHR(0),43) + ;
CHR(1) + ;
REPL(CHR(0),3) + ;
CHR(3) + ;
REPL(CHR(0),19)
OTHERWISE
*-- Use default of application
lcStartUpInfo = CHR(68) + REPL(CHR(0),67)
ENDCASE
* This API call does the work. The parameters are as follows:
* lpszModuleName - ptr-> file name of module to execute. Since we aren't launching .CPLs, do not use
* lpszCommandLine - ptr-> command to execute, as passed in method
* lpSecurityAttributesProcess - ptr-> SECURITY_ATTRIBUTES structure for Process. Pass a null pointer
* lpSecurityAttributesThread - ptr-> SECURITY_ATTRIBUTES structure for first thread. Pass a null pointer
* bInheritHandles - whether or not chlid inherits parent handles. Since no SECURITY_ATTRIBUTES passed, default to FALSE
* dwCreateFlags - Process Creation Mode flag set. we use the default mode at normal priority, ie 0
* lpvEnvironment - ptr-> a set of environment strings as if a MULTI_SZ. We don't set, so pass a null pointer
* lpszStartupDir - ptr-> the starting directory. If none provided to method, pass a null pointer
* lpStartInfo - ptr-> a STARTUPINFO structure. We use one structure member at times.
* lpProcessInfo - ptr-> a PROCESS_INFORMATION structure, used to return PID/PHANDLE detail. We use one member
DECLARE SHORT CreateProcess IN WIN32API ;
STRING lpszModuleName, ;
STRING @lpszCommandLine, ;
STRING lpSecurityAttributesProcess, ;
STRING lpSecurityAttributesThread, ;
SHORT bInheritHandles, ;
INTEGER dwCreateFlags, ;
STRING lpvEnvironment, ;
STRING lpszStartupDir, ;
STRING @lpStartInfo, ;
STRING @lpProcessInfo
lnStatus = CreateProcess( 0, ;
tcCommandLine, ;
0, 0, 0, 0, 0, ;
NULL, ;
@lcStartUpInfo, ;
@lcProcessInfo)
CLEAR DLLS
IF lnStatus <> 1
*-- really we should have a new dll/winapi error!!!
RETURN _ValidError
ENDIF
IF tlAndWait
lnProcessHandle = ConvertWordToInt( LEFT( lcProcessInfo, 4))
*-- Stop here until the other process has gone.
*-- Declare a func.. param1 the process to wait for, param 2 idle time in milli seconds
DECLARE INTEGER WaitForSingleObject IN WIN32API INTEGER, INTEGER
DO WHILE WaitForSingleObject( lnProcessHandle, 500) = 258 && WAIT_TIMEOUT
ENDDO
CLEAR DLLS
ENDIF
RETURN 0
*****************************************************************************
FUNCTION ConvertWordToInt
*-- Supports Doubles and Single words based on the length
LPARAMETERS tcWord
LOCAL lcLowWord, lcHighWord, lnLowWord, lnHighWord
lcLowWord = SUBSTR( tcWord, 1, 2)
lcHighWord = SUBSTR( tcWord, 3, 2)
lnLowWord = ASC( LEFT( lcLowWord, 1)) + ASC( RIGHT( lcLowWord, 1)) * 256
lnHighWord = 0
IF !EMPTY( lcHighWord)
lnHighWord = ( ASC( LEFT( lcHighWord, 1)) + ASC( RIGHT( lcHighWord, 1)) * 256) * 256 * 256
ENDIF
RETURN lnLowWord + lnHighWord
>Hi Macer
>
>I tried getting hold of the window handle once the window is open.. here is the code, it's in visual basic, but you should be able to read it.....
>
>the code opens the bat file, and then calls the function to check is the window is still open.
>
>The problem occurs when I call the function loopUntilWindowIsClosed
>
>the check seems to be too fast, and hence this function thinks the window is not open, as the function is called before the window is completely open.
>
>Any ideas on this would be great
>
>
>Regards
>Steven
>
>
>
>
>Function processBr1Files(lcFileToProcess)
>
>lnReturnValue = ShellExecute(0, "open", "c:\br1loader\load_br1.bat", "data\" & lcFileToProcess & " U Y Y", "c:\br1loader\", 1)
>
>
>
> If lnReturnValue <= 32 Then
> 'There was an error
> Select Case r
> Case SE_ERR_FNF
> msg = "File not found"
> Case SE_ERR_PNF
> msg = "Path not found"
> Case SE_ERR_ACCESSDENIED
> msg = "Access denied"
> Case SE_ERR_OOM
> msg = "Out of memory"
> Case SE_ERR_DLLNOTFOUND
> msg = "DLL not found"
> Case SE_ERR_SHARE
> msg = "A sharing violation occurred"
> Case SE_ERR_ASSOCINCOMPLETE
> msg = "Incomplete or invalid file association"
> Case SE_ERR_DDETIMEOUT
> msg = "DDE Time out"
> Case SE_ERR_DDEFAIL
> msg = "DDE transaction failed"
> Case SE_ERR_DDEBUSY
> msg = "DDE busy"
> Case SE_ERR_NOASSOC
> msg = "No association for file extension"
> Case ERROR_BAD_FORMAT
> msg = "Invalid EXE file or error in EXE image"
> Case Else
> msg = "Unknown error"
> End Select
> lnAnswer = MsgBox(msg, 48, "File run error")
> End If
>
> Call loopUntilWindowIsClosed
> MsgBox ("Staring next")
>
>End Function
>
>
>Function loopUntilWindowIsClosed()
>
>
>
>Do While FindWindow("ConsoleWindowClass", 0&) <> 0
>
>
>Loop
>End Function
Previous
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only