Click to See Complete Forum and Search --> : That old devil called shell again!


Ste
November 9th, 1999, 09:18 AM
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

Lothar Haensler
November 9th, 1999, 09:25 AM
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

Ste
November 9th, 1999, 09:47 AM
That works beautifully - the beers in the post!