Solved

how to know closing of one window within another

Posted on 2003-10-23
18
1,445 Views
Last Modified: 2013-12-26
Hi

i am calling a batch file (.bat) file from one of my powerbuilder window. it will take bit seconds to fully execute the batch statements and it will close automatically.

now what i want is, i need to know closing of that dos mode window from my called powerbuilder window to show some message. how will i acheive it ?. if u can, pls give example codes..

is it possible to find with findwindowA API ?.  but what will be the title of the batch file ?

any comment will be appreciated

kidu 11
0
Comment
Question by:kidu11
  • 9
  • 5
  • 3
  • +1
18 Comments
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9607777
If you use SheelExecuteA api instad of RUN function. You can accomplish this.

Function long ShellExecuteA( long hWindow, string Operation, string File, string Parameters, string Directory, long Visible) Library 'shell32'

See example below:

Use shellExecute API method :
// Add this to local/global external functions
//FUNCTION long ShellExecuteA(long hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, long nShowCmd) LIBRARY "SHELL32.DLL"

// API call script
long hwnd
string filname
hwnd=handle(parent)
filname='app2.exe'
shellexecuteA(hwnd,'open',filname,'','',0)


HTH

Namasi Navaretnam
0
 

Author Comment

by:kidu11
ID: 9613078
hi

thanx 4 ur comment

shellexecuteA is not working 4 the program i am trying to run, that has parameters also. here is my code,

is_commandline =  "command.com /c " + is_batfile + " " + is_workingfolder + " " + is_dbuid + " " + is_dbpwd + " " + is_zippwd + " " + is_span + " " + is_zipfile


//running the backup batchfile

if run( is_commandline , normal!) = 1 then
     return 1
end if

so, what is the problem...i think shellexecuteA will not work files that has paremeters, am i correct ?.

then how will achieve this

kidu 11
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9613294
long hwnd
string filname
hwnd=handle(parent)

is_commandline =  "command.com /c " + is_batfile + " " + is_workingfolder + " " + is_dbuid + " " + is_dbpwd + " " + is_zippwd + " " + is_span + " " + is_zipfile


Try This
ShellExecuteA(0,'open',is_commandline ,'','',0)

Or Try This

shellexecuteA(hwnd,'open',is_commandline ,'','',0)

ShellExcuteA work well for me with parameters. I run a command to bcp with parameters.

HTH

Namasi Navaretnam
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9613306
I am not sure of command.com is 16 bit or 32 bit. SO may want to try with cmd as well.

long hwnd
string filname
hwnd=handle(parent)

is_commandline =  "cmd /c " + is_batfile + " " + is_workingfolder + " " + is_dbuid + " " + is_dbpwd + " " + is_zippwd + " " + is_span + " " + is_zipfile


Try This
ShellExecuteA(0,'open',is_commandline ,'','',0)

Or Try This

shellexecuteA(hwnd,'open',is_commandline ,'','',0)

ShellExcuteA work well for me with parameters. I run a command to bcp with parameters.

0
 
LVL 5

Expert Comment

by:gafoor78
ID: 9619642
Hi

it is not working if any argument is there. it return a value 2 ( no path found ) if any space is there ( that is arguments)

for ex: u check this one in ur system

ShellExecuteA(0,'open','notepad test.txt','','',1)

if it will be like       ShellExecuteA(0,'open','notepad','','',1), then no pblm it is working fine, return a value >34

my OS is win98

pls check and let me know if any other method is there ?.

kidu11

0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9624597
You are right!!
I went and checked my previous  code!! This is what I did

ls_commandfilename = "C:\test.bat"
ls_command = "commmand.... etc with your parameters"


Ll_FileNum = FileOpen ( Ls_CommandFileName, LineMode!, Write!, LockWrite!, Replace!)
FileWrite(ll_FileNum, Ls_Command)
FileClose(ll_FileNum)

Now execute,

Ll_Return = ShellExecuteA(0,'open',ls_CommandFileName,'','',0)
//make sure to use 0 as the last parameter.

HTH

Namasi Navaretnam





0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9633301
At the end ,

FileDelete("c:\test.bat)
0
 

Author Comment

by:kidu11
ID: 9633429
hi namasi

thank u.. i will check and get u back soon

kidu11
0
 

Author Comment

by:kidu11
ID: 9639116
thanx again

it is working now...but still my goal is not acheived !!. how do i know when the task is finished ?. i need to do some coding just after the task is finished... where will i code this

what i already done is checking the existance of the newly created file within a timer by the PB function  fileexist() !!.  really i expect there will be some method with shellexecute..get me abooooout it.

kidu
0
 
LVL 15

Assisted Solution

by:namasi_navaretnam
namasi_navaretnam earned 25 total points
ID: 9641500
This is what i am doing in my code to see if the execution has completed

String ls_donefile
ls_donefile = "c:\done.txt"

// First delete done file.
If FileExists(Ls_DoneFile) Then
   FileDelete(Ls_DoneFile)
End If

Ls_DoneCommand = 'echo done > "' + Ls_DoneFile + '"'
ls_commandfilename = "C:\test.bat"
ls_command = "commmand.... etc with your parameters"


Ll_FileNum = FileOpen ( Ls_CommandFileName, LineMode!, Write!, LockWrite!, Replace!)
FileWrite(ll_FileNum, Ls_Command)
FileWrite(ll_FileNum, Ls_DoneCommand)
FileClose(ll_FileNum)

Ll_Return = ShellExecuteA(0,'open',ls_CommandFileName,'','',0)

//Execute the command above and wait for the done file to be created.

Do While Lb_DoneFileFound = FALSE
      If FileExists(Ls_DoneFile) then
            Lb_DoneFileFound = TRUE
      end if      
      Sleep(1) // Sleep for 1 second
Loop

FileDelete(ls_donefile)
FileDelete(ls_commandfilename )

HTH

Namasi

0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9643081
Alternative,

Open a response window (that display "Backing Up Database or what ever") so that the code underneath will not get executed.
After opening the winodw post an event (using POSTEVENT function) to check the existance of done file. whenthe file exists you can close the response winodw.

Do While Lb_DoneFileFound = FALSE
     If FileExists(Ls_DoneFile) then
          Lb_DoneFileFound = TRUE
     end if    
     Sleep(1) // Sleep for 1 second
Loop


HTH
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9657105
Kidu11
Did that solution work?
0
 
LVL 8

Accepted Solution

by:
Vikas_Dixit earned 40 total points
ID: 9659163
Hi,
 you can use the ShellExecuteEx function instead :

refer the link below for an example.
http://www.experts-exchange.com/Programming/Programming_Languages/PowerBuilder/Q_20663647.html

Declare another function

Integer WAIT_TIMEOUT =  258 //&H102

Function ulong WaitForSingleObject (ulong hHandle, ulong dwMilliseconds) Library "KERNEL32.DLL"


And call  after the call to ShellExecuteEx

Do
      retval = WaitForSingleObject(lst_shellexecuteinfo.hProcess, 0)
Loop While retval = WAIT_TIMEOUT

MessageBox("Done","")

HTH
Regards,
Vikas Dixit
0
 
LVL 8

Expert Comment

by:Vikas_Dixit
ID: 9659210
Also,

to get the processid, the flag needs to be set before calling ShellExecuteEx:
CONSTANT long SEE_MASK_CLASSNAME =64 //&H40
lst_shellexecuteinfo.fMask = SEE_MASK_NOCLOSEPROCESS

Regards,
Vikas
0
 

Author Comment

by:kidu11
ID: 9660328
Hi

Thank u Namasi.. it is working, but i will also check with Vikas's ShellExecuteEx, i think that will be a better approch

so i will get back to u soon

rgrds

kidu
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9662781
Yes. I agree ShellExecuteEx would be a better approch.
0
 

Author Comment

by:kidu11
ID: 9719140
Hello vikas

now i checked with shellexecuteex and WaitForSingleObject. but the WaitForSingleObject is not working.

here is my complete code

CONSTANT long SEE_MASK_CLASSNAME = 64
CONSTANT long SW_NORMAL = 1
Integer WAIT_TIMEOUT =  258 //&H102
CONSTANT long SH_HIDE = 0
integer retval

string ls_class
string ls_extension
long ll_ret

os_shellexecuteinfo lst_shellexecuteinfo

// Search for the classname associated with extension
RegistryGet("HKEY_CLASSES_ROOT\" + '.bat', "", ls_class)

// the file is not associated with any program.. try to install the program
IF isNull(ls_class) OR Trim(ls_class) = "" THEN
    RETURN 0
END IF

as_file = is_commandline = "command.com /c " + is_batfile + " " + is_workingfolder + " " + is_dsnname + " " + is_dbuid + " " + is_dbpwd + " " + is_zipfile

lst_shellexecuteinfo.cbsize = 60
lst_shellexecuteinfo.fMask = SEE_MASK_CLASSNAME  // Use classname
lst_shellexecuteinfo.hwnd = 0
lst_shellexecuteinfo.lpVerb = "open"
lst_shellexecuteinfo.lpfile = as_file
lst_shellexecuteinfo.lpClass = ls_class
lst_shellexecuteinfo.nShow = SH_HIDE

ll_ret = ShellExecuteEx(lst_shellexecuteinfo)

// cannot open the file...pls try reinstalling the program
IF ll_ret = 0 THEN
       RETURN 2
END IF

Do
   retval = WaitForSingleObject(lst_shellexecuteinfo.hProcess, 0)
loop while retval = WAIT_TIMEOUT

return 1

in the above code the problem is the do loop is iterating only once.  and its value at the first iteration is  -1. so what is the problem, pls check inform me what is wrong with the above code

thanx
kidu
0
 
LVL 8

Expert Comment

by:Vikas_Dixit
ID: 9726852
Hi kidu,

I tried with a .txt file, and it worked fine... the function kept returning 258 till the notepad was open, and once I closed the notepad, it returned 0..

you can try the following :

1.change the following :
lst_shellexecuteinfo.nShow = SH_SHOW  //SH_HIDE  -- SH_SHOW =1

2. check if the lst_shellexecuteinfo.hProcess has a process id.

3. instead of using "command.com /c", try opening just the batch file, and pass the parameters in the lpParameters member.

will try to test with a .bat file

regards,
Vikas D
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question