CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 21

Threaded View

  1. #4
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: how can we run a dos command from VB

    This works well for me. It allows you to wait until the process finishes.
    Put this code in a Module (Module1.bas):

    Code:
    option Explicit
        
    '//public Constants
    public Const NORMAL_PRIORITY_CLASS = &H20&
    public Const INFINITE = -1&
        
    '//public Types
    public Type STARTUPINFO
       cb as Long
       lpReserved as string
       lpDesktop as string
       lpTitle as string
       dwX as Long
       dwY as Long
       dwXSize as Long
       dwYSize as Long
       dwXCountChars as Long
       dwYCountChars as Long
       dwFillAttribute as Long
       dwFlags as Long
       wShowWindow as Integer
       cbReserved2 as Integer
       lpReserved2 as Long
       hStdInput as Long
       hStdOutput as Long
       hStdError as Long
    End Type
        
    public Type PROCESS_INFORMATION
       hProcess as Long
       hThread as Long
       dwProcessID as Long
       dwThreadID as Long
    End Type
        
    '//API Declarations
    public Declare Function WaitForSingleObject Lib "kernel32" (byval _
       hHandle as Long, byval dwMilliseconds as Long) as Long
        
    public Declare Function CreateProcessA Lib "kernel32" (byval _
       lpApplicationName as Long, byval lpCommandLine as string, byval _
       lpProcessAttributes as Long, byval lpThreadAttributes as Long, _
       byval bInheritHandles as Long, byval dwCreationFlags as Long, _
       byval lpEnvironment as Long, byval lpCurrentDirectory as Long, _
       lpStartupInfo as STARTUPINFO, lpProcessInformation as _
       PROCESS_INFORMATION) as Long
        
    public Declare Function CloseHandle Lib "kernel32" (byval _
       hObject as Long) as Long
    
    
    
        
    Put This Code In A Form (Form1):
    
    private Sub Command1_Click()
        Dim AppToRun as string
        Dim ParamForApp as string
        Dim CmdLine as string
           
        '//set Application to Run
        AppToRun = "C:\WINNT\System32\CMD.exe"
           
        '//set Command Line Parameters
        '//The "/C" Tells Windows to Run The Command then Terminate
        '//The Command Line (CMD.exe)
        ParamForApp = " /C MD New_Folder" '//Make new Directory Called 'New_Folder'
           
        '//Build Command string
        CmdLine = AppToRun & ParamForApp
           
        '//Shell App And Wait for It to Finish
        ExecCmd CmdLine
               
    End Sub
        
    public Sub ExecCmd(CmdLine as string)
        Dim Proc as PROCESS_INFORMATION
        Dim start as STARTUPINFO
        Dim ReturnValue as Integer
           
        '//Initialize The STARTUPINFO Structure
        start.cb = len(start)
           
        '//Start The Shelled Application
        ReturnValue = CreateProcessA(0&, CmdLine, 0&, 0&, 1&, _
           NORMAL_PRIORITY_CLASS, 0&, 0&, start, Proc)
           
        '//Wait for The Shelled Application to Finish
        Do
           ReturnValue = WaitForSingleObject(Proc.hProcess, 0)
           DoEvents
        Loop Until ReturnValue <> 258
           
        '//Close Handle to Shelled Application
        ReturnValue = CloseHandle(Proc.hProcess)
           
    End Sub


    Kris
    Software Engineer
    Phoenix,AZ
    Last edited by Cimperiali; October 31st, 2004 at 05:41 PM. Reason: substitute html chars for "<" and ">"
    Kris
    Software Engineer
    Phoenix, AZ USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured