CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 1999
    Posts
    17

    That old devil called shell again!

    This question has occured many a time concerning shelling a DOS program from VB and waiting for it to end before VB continues. I've tried to follow the correct way of using CreateProcess and WaitForSingleObject but am having trouble getting the CreateProcess to lauch the DOS job. As the firm I work for are too tight to fork out for the WINAPI book I'm programming blind so it may be said. Can someone please supply some sample code to for example launch the DOS editor.

    Many thanks in anticipation


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

    Re: That old devil called shell again!

    this sample starts edit.com in an NT command shell.

    option Explicit

    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





    private Sub Command1_Click()


    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, "edit.com", 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



    MsgBox "Done"

    End Sub





  3. #3
    Join Date
    Oct 1999
    Posts
    17

    Re: That old devil called shell again!

    That works beautifully - the beers in the post!


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