|
This is the example for VB, may be it will help.
Andrew Bushmakin
******************************
HOWTO: Launch a Win32 Application from Visual Basic
Last reviewed: November 24, 1997
Article ID: Q129797
The information in this article applies to:
Standard, Professional, and Enterprise Editions of Microsoft Visual
Basic, 32-bit only, for Windows, version 4.0
SUMMARY
A Windows application can consist of more than one process, and a
process can consist of more than one thread. The Microsoft Win32
application program interface (API) supports multitasking, which
creates the effect of simultaneous execution of multiple processes and
threads. This article describes processes and threads, and explains how
to create and use them from Microsoft Visual Basic version 4.0, with a
step-by-step example.
MORE INFORMATION
A process is a program that is loaded into memory and prepared for
execution. Each process has a private virtual address space. A process
consists of the code, data, and other system resources such as files,
pipes, and synchronization objects that are accessible to the threads
of the process. Each process is started with a single thread, but
additional independently executing threads can be created.
A thread can execute any part of the program's code, including a part
executed by another thread. Threads are the basic entity to which the
operating system allocates CPU time. Each thread maintains a set of
structures for saving its context while waiting to be scheduled for
processing time. The context includes the thread's set of machine
registers, the kernel stack, a thread environment block, and a user
stack in the address space of the thread's process. All threads of a
process share the virtual address space and can access the global
variables and system resources of the process.
A multitasking operating system divides the available CPU time among
the threads that need it. In Windows, the Win32 API is designed for
preemptive multitasking; this means that the system allocates small
slices of CPU time among the competing threads. The currently executing
thread is suspended when its time slice elapses, allowing another
thread to run. When the system switches from one thread to another, it
saves the context of the suspended thread and restores the saved
context of the next thread in the queue.
Because each time slice is small (approximately 20 milliseconds), it
appears that multiple threads are executing at the same time. This is
actually the case on multiprocessor systems where the executable
threads are distributed among the available processors. On a single
processor system, however, using multiple threads does not result in
more instructions being executed. In fact, the system can slow down if
it is forced to keep track of too many threads.
How to Launch Win32 Applications from Visual Basic
There are two ways to launch another Win32 application from a Microsoft
Visual Basic version 4.0 application:
Use the Visual Basic shell command. This spawns a new process and
returns its process ID. However, to be able to do anything useful, a
process handle is required, which can be obtained by a subsequent call
to the OpenProcess Win32 API function.
Use the CreateProcess Win32 API function that creates both a process
object and a main thread object. Both the process and the initial
thread are assigned a 32-bit identifier that remains valid until the
respective object (process or thread) terminates. The 32-bit identifier
can be used to uniquely identify the object within the system. The new
process and new thread handles are also created with full access
rights. All these four values are returned in the PROCESS_INFORMATION
structure that is passed by reference to CreateProcess.
The process handle returned by either of the two methods can then be
used with other Win32 APIs such as TerminateProcess to manipulate the
newly launched application.
It is important to understand that at creation time, the system gives
each object an initial usage count of one. Then, just before
CreateProcess returns, the function opens both the process and the
thread object and places the process-relative handles for each in the
hProcess and hThread members of the PROCESS_INFORMATION structure.
When CreateProcess opens these objects, the usage count for each
increments to two. This means that before the Windows NT Executive can
free the process object, the process must terminate (decrementing the
usage count to one) and the parent process must call CloseHandle
(decrementing the usage count to zero). To free the thread object, the
thread must terminate and the parent process must close the handle to
the thread object.
CAUTION: It is very important to close these handles. Failure to do so
can result in a system memory leak because some Windows NT Executive
objects are never destroyed.
Similar considerations are required when obtaining a process handle
with OpenProcess. In this case too, the usage count is incremented by
one, and unless the handle is closed, the process object will remain in
memory even when the process itself has terminated.
Step-by-Step Example
Start a new project in Visual Basic. Form1 is created by default.
Add the following code to the General Declarations section of Form1:
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
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 Declare Function CreateProcess Lib "kernel32" Alias _
"CreateProcessA" (ByVal lpApplicationName As String, ByVal _
lpCommandLine As String, lpProcessAttributes As Any, _
lpThreadAttributes As Any, 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 OpenProcess Lib "kernel32.dll" (ByVal _
dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal _
hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Const SYNCHRONIZE = 1048576
Const NORMAL_PRIORITY_CLASS = &H20&
Add the following code to the Form1_Click event:
Dim pInfo As PROCESS_INFORMATION
Dim sInfo As STARTUPINFO
Dim sNull As String
sInfo.cb = Len(sInfo)
success& = CreateProcess(sNull, "Calc.exe", ByVal 0&, ByVal 0&, 1&,
NORMAL_PRIORITY_CLASS, ByVal 0&, sNull, sInfo, pInfo)
MsgBox "Calculator has been launched!"
ret& = TerminateProcess(pInfo.hProcess, 0&)
ret& = CloseHandle(pInfo.hThread)
ret& = CloseHandle(pInfo.hProcess)
MsgBox "Calculator has terminated!"
Press the F5 key to run the program. Click Form1. The Windows
Calculator program launches, and a message tells you so. When you click
the OK button on the MsgBox, the Calculator program terminates, and
another message tells you so.
To see how this method works using the Shell and OpenProcess method,
change the commented code into executed code, change the rest of the
code into comments except for the two MsgBox statements.
Keywords : APrgOther vb432 VB4WIN
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type : kbhowto
*********************************
In article <8jeve4$qe4$1@nnrp1.deja.com>,
autretx@my-deja.com wrote:
> I'm looking for an API function equivalent to the 'start' MS-DOS
> command:
>
> start c:\image.bmp
>
> WinExec is not appropriate,
> CreateProcess is hard but appropiate???
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
Sent via Deja.com http://www.deja.com/
Before you buy.
|