CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 1999
    Location
    LA, USA
    Posts
    23

    How to start a program using CreateProcess

    I need to start a program using CreateProcess and wait for the program to finish before the rest of the code of VB executes. Can you tell me how to do this?


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: How to start a program using CreateProcess

    this is a sample for starting MSPaint and wait until the user ends it.

    Dim l as Long
    Dim h as Long
    Dim hp as Long
    Dim sa as STARTUPINFO
    Dim sec as SECURITY_ATTRIBUTES
    Dim pi as PROCESS_INFORMATION
    sa.cb = LenB(sa)
    CreateProcess vbNullString, "mspaint", 0, 0, false, 0, byval 0, vbNullString, sa, pi
    hp = OpenProcess(SYNCHRONIZE, 0&, pi.dwProcessId)
    l = WaitForSingleObject(hp, 100)
    Do While l = WAIT_TIMEOUT
    DoEvents
    l = WaitForSingleObject(hp, 100)
    Loop




    tested with VB 6 and NT 4


  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: How to start a program using CreateProcess

    Oops, I forgot the Declare Statements.
    I need to make clear that I modified the Declare Statement for CreateProcess. It's not the original from the APIViewer!


    private Declare Function OpenProcess Lib "kernel32" (byval dwDesiredAccess as Long, byval bInheritHandle as Long, byval dwProcessId as Long) as Long
    private Declare Function WaitForSingleObject Lib "kernel32" (byval hHandle as Long, byval dwMilliseconds as Long) as Long
    private Declare Function CloseHandle Lib "kernel32" (byval hObject as Long) as Long
    private Const SYNCHRONIZE = &H100000
    Const WAIT_TIMEOUT = &H102

    private 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
    private Type SECURITY_ATTRIBUTES
    nLength as Long
    lpSecurityDescriptor as Long
    bInheritHandle as Long
    End Type

    private Type PROCESS_INFORMATION
    hProcess as Long
    hThread as Long
    dwProcessId as Long
    dwThreadId as Long
    End Type
    private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
    (byval lpApplicationName as string, byval lpCommandLine as string, byval x as Long, byval y as Long, _
    byval bInheritHandles as Long, byval dwCreationFlags as Long, _
    lpEnvironment as Any, byval lpCurrentDriectory as string, lpStartupInfo as STARTUPINFO, _
    lpProcessInformation as PROCESS_INFORMATION) as Long
    private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (byval dwFlags as Long, _
    lpSource as Any, byval dwMessageId as Long, byval dwLanguageId as Long, byval lpBuffer as string, _
    byval nSize as Long, Arguments as Long) as Long






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