Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 42,073 times

Related Categories

Running a DOS command

Occasionally you might find that you want to run a dos command from your VB program. The easiest way to do this is create a batch file (*.bat) containing the command (ie registering a number of components). Your program can then call this file, and wait until it has finished using the code below

Private Sub cmdDos_Click()
    Dim lngTask As Long, lRet As Long, pHandle As Long
    '// start the dos program
    lngTask = Shell(App.Path & "pointless.bat", vbMinimizedNoFocus)
    '// get a handle on the window
    pHandle = OpenProcess(SYNCHRONIZE, False, lngTask)
    lblStatus.Caption = "Running DOS program..."
    lblStatus.Refresh
    cmdDOS.Enabled = False
    Do
        '// you can pass INFINITE instead of 0 if you wish, and your
        '// program will 'hang' here until the program is finished
        lRet = WaitForSingleObject(pHandle, 0)
        DoEvents
    Loop While lRet <> 0
    lblStatus.Caption = "Finished"
    
    lRet = CloseHandle(pHandle)  'Close the task
    cmdDOS.Enabled = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If cmdNotepad.Caption = "Close Notepad" Then Call cmdNotepad_Click
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments