Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Dos Programs 1

Status
Not open for further replies.

NW

Programmer
Feb 3, 2000
61
0
0
GB
Does any one know how to use SHELL or any other command in VB to call & terminate dos programs?<br>
I need to run a dos exe, with this command and when user exist this prog. (doc) it should return back to main VB program.<br>
Any help on this will greatly appreciate.<br>
Thanks<br>

 
Sure, try using the Shell Function, returns 0 if everything went OK, and an error number otherwise. <p> <br><a href=mailto: > </a><br><a href= > </a><br>
 
nw,<br>
Do you want to freeze the VB app while the shelled pgm is running, ie, make it syncronous, then upon exit the shell'd pgm unfreeze the vb app? If so, I've got the method for that.<br>
--Jim
 
DannyB,<br>
Thanks for your replies. I will try it.<br>
<br>
Jim,<br>
Upon exit shelled program, I need to close dos window & return back to main VB program. Is your freeze/unfreeze method able to do this?. Also can I use SenKeys function to carry out few tasks in shelled program?. <br>
Can you explain how your freeze program works with a sample please?<br>
Thanks <br>
Niranjan<br>

 
NW,<br>
If you right-click on the MSdos prompt icon, then select the Program tab, and check the 'close on exit' box, this will close shelled DOS boxes on exit. I'm not sure about SendKeys, I've never used it to try to control a DOS program. I think you'd need to get the Window Handle of the dos box or shelled program, since if the user did anything with the vb app to give it back the focus, the sendkeys would go to the vb app, not the intended program. <br>
<br>
The 'freeze' thing isn't a program it's just some API's that will guarantee the calling program (the VB app) can't recieve the focus until the shelled program has finished (or until a specified amount of time has passed). So, you need to know that you will be able to manually or programatically close the shelled program, since in the example below, the INFINITE constant is used as the timeout, so if the shelled program can't close, you'll never regain the VB app. For this reason, you need to have control over the shelled program, ie. you must be able to end it.:<br>
<br>
'Put these in the Declarations of a Module:<br>
Declare Function OpenProcess Lib &quot;kernel32&quot; (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long<br>
Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long<br>
Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br>
Const SYNCHRONIZE = 1048576<br>
Const NORMAL_PRIORITY_CLASS = &H20&<br>
Const INFINITE = &HFFFF<br>
<br>
'Now here's how to make the Shell call:<br>
Dim ProcessId As Long, ProcessHandle As Long<br>
ProcessId = Shell(MyPgmName, vbNormalFocus)<br>
ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessId)<br>
WaitForSingleObject ProcessHandle, INFINITE ,&lt;----alterable<br>
'The execution stops here and the app will not accept Window Messages of any type until shelled pgm closes.<br>
CloseHandle ProcessHandle<br>
--Jim
 
Jim,<br>
Thanks for your reply. It works fine. I tried SendKeys to close dos window without a success. But it’s not a problem. Your routine does exactly what I needed to do.<br>
Thank you once again.<br>
Niranjan<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top