CreateProcess function doesn't work properly. 
Author Message
 CreateProcess function doesn't work properly.

Hi,

I'm making a program that runs application 'A' and have to wait for its
termination to start running the application 'B'.

I try use the exemple code   (WIN98, VB6)

Public Sub ExecuteAndWait()
    Dim NameOfProc As PROCESS_INFORMATION
    Dim NameStart As STARTUPINFO
    Dim X As Long
    Dim S As SECURITY_ATTRIBUTES
    dim cmdline As String

    cmdline = "C:\windows\notepad.exe"
    NameStart.cb = Len(NameStart)
    X = CreateProcess(0&, cmdline, S, S, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&,
NameStart, NameOfProc)
    X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
    X = CloseHandle(NameOfProc.hProcess)
End Sub

All api functions and Consts are defined.

But the example doesn't work. The function CreateProcess returns the zero
value when the correct value value should be nonzero. The
NameOfProc.hProcess is also zero.

Am I forgeting something? Is anything wrong with the cmdline?

Thanks in advance.

Cumprimentos,
Nuno Oliveira
(Programador / Programmer)

PROFITUS www.profitus.pt
Tel.: +351 282 490 270 Fax: +351 282 490 279



Fri, 13 Dec 2002 03:00:00 GMT  
 CreateProcess function doesn't work properly.

Quote:

>Hi,

>I'm making a program that runs application 'A' and have to wait for its
>termination to start running the application 'B'.

>I try use the exemple code   (WIN98, VB6)

>Public Sub ExecuteAndWait()
>    Dim NameOfProc As PROCESS_INFORMATION
>    Dim NameStart As STARTUPINFO
>    Dim X As Long
>    Dim S As SECURITY_ATTRIBUTES
>    dim cmdline As String

>    cmdline = "C:\windows\notepad.exe"
>    NameStart.cb = Len(NameStart)
>    X = CreateProcess(0&, cmdline, S, S, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&,
>NameStart, NameOfProc)
>    X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
>    X = CloseHandle(NameOfProc.hProcess)
>End Sub

>All api functions and Consts are defined.

>But the example doesn't work. The function CreateProcess returns the zero
>value when the correct value value should be nonzero. The
>NameOfProc.hProcess is also zero.

>Am I forgeting something? Is anything wrong with the cmdline?

>Thanks in advance.

>Cumprimentos,
>Nuno Oliveira
>(Programador / Programmer)

>PROFITUS www.profitus.pt
>Tel.: +351 282 490 270 Fax: +351 282 490 279

Firstly, attach a module to your project.
Paste in the following declarations:

===============================================================

Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long,
_
    ByVal binheritHandle As Long, ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

===============================================================

In your Main form or module paste the code:

===============================================================

Private Sub ShellW(ByVal strProg As String, ByVal lStyle As VbAppWinStyle)
Dim PId As Long
Dim PHandle As Long
Const Access As Long = &H100000

ProcessId = Shell(strProg, lStyle)
    Do
        PHandle = OpenProcess(Access, False, PId)
        If PHandle <> 0 Then
            CloseHandle PHandle
        End If
        DoEvents
    Loop Until PHandle = 0
End Sub

Private Sub cmdRun_Click()

    ShellW "myprog.bat", vbMaximizedFocus

End Sub

===============================================================

The shellw command now replaces the shell command, when it spawns the job
it get the process ident, it then loops until this process ident no longer
exists,
i.e. the end of the job.

hope this helps,

  Mike.



Fri, 13 Dec 2002 03:00:00 GMT  
 CreateProcess function doesn't work properly.

Quote:
> The shellw command now replaces the shell command, when it spawns the job
> it get the process ident, it then loops until this process ident no longer
> exists,
> i.e. the end of the job.

While this might be a functional approach, I wouldn't recommend it.  This
means that the process that calls ShellW is going to be contantly polling
and wasting valuable CPU time waiting for the other application to exit.

WaitForSingleObject is a cleaner solution as it will essentially put your
execution thread to sleep while waiting for the other process to complete.



Fri, 13 Dec 2002 03:00:00 GMT  
 CreateProcess function doesn't work properly.

Quote:
> I'm making a program that runs application 'A' and have to wait for its
> termination to start running the application 'B'.

I'm guessing that you may have had some problems with some of the flags you
were passing to CreateProcess.  It's got a lot of power and flexibility but
sometimes that means a lot of work in understanding all of the params and
how they fit together.  Instead of debugging your project, I'm going to take
the lazy way out and just paste in some code that I use and know works.  I
use VB's Shell function to start the process, then I use API calls to get a
process handle for that new process.  Once I have the handle, my code is
more or less the same as yours...just wait until it completes and close the
handle.

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, Shell(cmdline,
iShowMode))
    WaitForSingleObject hProcess, INFINITE
    CloseHandle hProcess

Jason



Fri, 13 Dec 2002 03:00:00 GMT  
 CreateProcess function doesn't work properly.
I tried your code, but WaitForSingleObject always returns -1 immediately. I
can't figure out what's wrong.

I declared the funcitons as the following:

Declare Function OpenProcess Lib "KERNEL32" (ByVal dwDesiredAccess As Long, _
    ByVal binheritHandle As Long, ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Declare Function WaitForSingleObject Lib "KERNEL32" (ByVal hHandle As Long, _
  ByVal dwMilliseconds As Long) As Long

    Dim hRet, hInstance, lTime as long

    hRet = Shell(sCmd)
    SendKeys Chr(13), True
    AppActivate hRet
    hInstance = OpenProcess(1024&, False, hRet)
    lTime = 1000000
    hRet = WaitForSingleObject(hInstance, lTime)
    CloseHandle hInstance

where the sCmd is a commandline that launches the microsoft access to convert a
97 database into a 2000 database.

If the waitforsingleobject works properly, I will reduce the wait time and put
the call into a while loop. By this way I hope I can display  something to show
the database conversion is in progress. By the way, do you know is there any
such control I can use to show user something is in progress?

Quote:

> > I'm making a program that runs application 'A' and have to wait for its
> > termination to start running the application 'B'.

> I'm guessing that you may have had some problems with some of the flags you
> were passing to CreateProcess.  It's got a lot of power and flexibility but
> sometimes that means a lot of work in understanding all of the params and
> how they fit together.  Instead of debugging your project, I'm going to take
> the lazy way out and just paste in some code that I use and know works.  I
> use VB's Shell function to start the process, then I use API calls to get a
> process handle for that new process.  Once I have the handle, my code is
> more or less the same as yours...just wait until it completes and close the
> handle.

>     hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, Shell(cmdline,
> iShowMode))
>     WaitForSingleObject hProcess, INFINITE
>     CloseHandle hProcess

> Jason



Fri, 13 Dec 2002 03:00:00 GMT  
 CreateProcess function doesn't work properly.
Hello!

The declaration of CreateProcess function from Win32api.txt file is useful
only for advanced programmers to write NT-only applications.
To pass Null pointers to lpApplicationName, lpProcessAttributes,
lpThreadAttributes, lpEnvironment, lpCurrentDriectory you must change this
Declare statement:

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA"
_
    (ByVal lpApplicationName As String, 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 lpCurrentDriectory As String, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

Then call CreateProcess:

X = CreateProcess(VbNullString, cmdline, 0, 0, 0, 0, 0, _
    VbNullString, NameStart, NameOfProc)

The NORMAL_PRIORITY_CLASS in most cases is assigned to the new process by
default, and, I think, you don't need to inherit handles.
Don't forget to close the thread handle:

X = CloseHandle(NameOfProc.hThread)

Sergey Merzlikin
http://smsoft.chat.ru


Quote:
> Hi,

> I'm making a program that runs application 'A' and have to wait for its
> termination to start running the application 'B'.

> I try use the exemple code   (WIN98, VB6)

> Public Sub ExecuteAndWait()
>     Dim NameOfProc As PROCESS_INFORMATION
>     Dim NameStart As STARTUPINFO
>     Dim X As Long
>     Dim S As SECURITY_ATTRIBUTES
>     dim cmdline As String

>     cmdline = "C:\windows\notepad.exe"
>     NameStart.cb = Len(NameStart)
>     X = CreateProcess(0&, cmdline, S, S, 1&, NORMAL_PRIORITY_CLASS, 0&,
0&,
> NameStart, NameOfProc)
>     X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
>     X = CloseHandle(NameOfProc.hProcess)
> End Sub

> All api functions and Consts are defined.

> But the example doesn't work. The function CreateProcess returns the zero
> value when the correct value value should be nonzero. The
> NameOfProc.hProcess is also zero.

> Am I forgeting something? Is anything wrong with the cmdline?

> Thanks in advance.

> Cumprimentos,
> Nuno Oliveira
> (Programador / Programmer)

> PROFITUS www.profitus.pt
> Tel.: +351 282 490 270 Fax: +351 282 490 279



Fri, 13 Dec 2002 03:00:00 GMT  
 CreateProcess function doesn't work properly.

Hi all VB world.

I have a problem like a Nuno with the CreateProcess

I'm tyr to install various DOS applications from my VB5 applications, I
also use the CreateProcess to do. I pass the command line from my principal
form to the function that create the process.
I make this process that follow :
How the DOS applications install via .Bat file (all have the same name
"Instala.Bat"), I'm create one logical drive , copy the files of each
module to install, send the command line to the function and create the
process, when it close the process I delete the files from  the logical
drive and continue with the next DOS applications. Until here all Ok, but
my Boss said me that is not necessary to create the logical drive and copy
the file because this taking a lot of time and the installation is very
slow.
Then I decided send to the command line the complete path of each DOS
application and install, e.g "C:\Program Files\DosApp\DpContab\Instala.bat
D:"
( D is the target drive to install, is obligatory)

Here is the problem, when the function try to create the process it appear
like a flash and don't make the install.  

Why in the other form work find, i was be able install all applications and
the VB program
wait until the DOS program finish and continue with the next ?

Somebody can give me one ligth to my darkness ?

Thanks in Advance
PD: I sent the code.

DECLARATIONS
Private Declare Function CreatePipe Lib "kernel32" _
(phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

Private Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead
As Long, _
lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Type STARTUPINFO
    cb As Long
    lpReserved As Long
    lpDesktop As Long
    lpTitle As Long
    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 PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As String, _
lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal
bInheritHandles As Long, _
ByVal dwCreationFlags As Long, ByVal lpEnviroment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As Any,
lpProcessInformation As Any) 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
Public ExecCmd As String

Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWDEFAULT = 10
Const STARTF_USESHOWWINDOW = &H1
Const INFINITE = -1&
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
=====================================================================
INSTALL PROCESS
Public Function Instalar(ByVal CmdLine As String) As String
Dim proc As PROCESS_INFORMATION, ret As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long, hWritePipe As Long
Dim bytesread As Long, Mybuff As String
Dim i As Integer
Dim sReturnStr As String
FrmEspera.Visible = False
ChDrive (Left(Direc, 3))
Mybuff = String(25 * 1024, Chr$(65))
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
 ExecCmd = "Error: CreatePipe Failed. " & Err.LastDllError
 Exit Function
End If
start.cb = Len(start)
start.hStdOutput = hWritePipe
start.dwFlags = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW
start.wShowWindow = SW_SHOWDEFAULT
start.dwX = 250
start.dwXSize = 1000
start.dwY = 50
start.dwYSize = 1000
ret& = CreateProcessA(0&, CmdLine$, sa, sa, 1&, NORMAL_PRIORITY_CLASS, 0&,
0&, start, proc)
If ret <> 1 Then
     bSuccess = 0
Else
    bSuccess = 1
End If
ret = WaitForSingleObject(proc.hProcess, INFINITE)
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.dwThreadId)
ret = CloseHandle(hReadPipe)
ret = CloseHandle(hWritePipe)
ChDrive (ExecuteFrom)
FrmEspera.Visible = True
End Function



Sat, 08 Feb 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. CreateProcess function doesn't work properly.

2. AllocUnit() function doesn't work properly with NTFS

3. VBScript Eval function doesn't work properly

4. Replace function doesn't work properly

5. Documenter doesn't work properly in A2K

6. Select method doesn't work properly

7. Small toolbar look - toolbar control doesn't work properly

8. STMAdmin.dll to query NT Log doesn't seem to work properly

9. My CommonDialog doesn't work properly

10. Match in DataCombo doesn't work properly

11. mmioInstallIOProc() doesn't work properly when compiled to native code

12. TVM_INSERTITEM doesn't seem to work properly

 

 
Powered by phpBB® Forum Software