Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Calling Dos Programs

Calling Dos Programs

Calling Dos Programs

Does any one know how to use SHELL or any other command in VB to call & terminate dos programs?
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.
Any help on this will greatly appreciate.

RE: Calling Dos Programs

Sure, try using the Shell Function, returns 0 if everything went OK, and an error number otherwise.

RE: Calling Dos Programs

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.

RE: Calling Dos Programs

Thanks for your replies. I will try it.

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?.
Can you explain how your freeze program works with a sample please?

RE: Calling Dos Programs

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.

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.:

'Put these in the Declarations of a Module:
Declare Function OpenProcess Lib "kernel32" (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Const SYNCHRONIZE = 1048576

'Now here's how to make the Shell call:
Dim ProcessId As Long, ProcessHandle As Long
ProcessId = Shell(MyPgmName, vbNormalFocus)
ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessId)
WaitForSingleObject ProcessHandle, INFINITE ,<----alterable
'The execution stops here and the app will not accept Window Messages of any type until shelled pgm closes.
CloseHandle ProcessHandle

RE: Calling Dos Programs

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.
Thank you once again.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close