Solved

how to know closing of one window within another

Posted on 2003-10-23
18
1,413 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

by:namasi_navaretnam
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
At the end ,

FileDelete("c:\test.bat)
0
 

Author Comment

by:kidu11
Comment Utility
hi namasi

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

kidu11
0
 

Author Comment

by:kidu11
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Assisted Solution

by:namasi_navaretnam
namasi_navaretnam earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Kidu11
Did that solution work?
0
 
LVL 8

Accepted Solution

by:
Vikas_Dixit earned 40 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes. I agree ShellExecuteEx would be a better approch.
0
 

Author Comment

by:kidu11
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now