There are two ways to launch a separate executable program from within a Visual Basic program. One way is to use the Visual Basic "Shell" command. The other is to use the "CreateProcess" Windows application programming interface (API). Both will successfully launch the external executable program; however, "CreateProcess" gives the programmer more control over how the operating system handles the program.
- Skill level:
Other People Are Reading
Start a new Visual Basic Standard EXE project. On "Form1" that is created by default, add a command button and name it "cmdNotepad."
Code the click event of command button in the "cmdNotepad_Click()" event. Declare a variable of type long to hold the return value from the Shell command. To launch the Windows Notepad program, enter "lngReturn =Shell("C:\Windows\System32\Notepad.exe")."
Specify how you want the Notepad application to open in the Shell command. The options are: vbHide, vbMaximizedFocus, vbMinimizedFocus, vbMinimizedNoFocus, vbNormalFocus and vbNormalNoFocus. Specify one of these options as the second parameter to the Shell command as "Shell("C:\Windows\System32\Notepad.exe", vbNormalFocus)."
Get the process ID from the return value of the Shell command. As shown in Step 2, "lngReturn" will contain the Windows assigned process ID for the Notepad application that was just launched from the Shell command. With the process ID, you can perform other tasks in your program such as verifying whether the process is still running before closing the Visual Basic application.
Start a new Visual Basic Standard EXE project. On "Form1" that is created by default, add a command button and name it "cmdNotepad." Add user-defined types named "PROCESS_INFORMATION" and "STARTUPINFO" in the "Declarations" section of "Form1." These structures are used to group data and are required parameters of the "CreateProcess" function.
Add two constants that are also parameters of the "CreateProcess" function.
Const SYNCHRONISE = 1048576
Const NORMAL_PRIORITY_CLASS = &H20&
Declare functions to create the process instance ("Declare the "CreateProcess" function as "Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA""), open the process ("Private Declare Function OpenProcess Lib "kernel32.dll""), terminate the process ("Private Declare Function TerminateProcess Lib "kernel32"") and to close the handle ("Private Declare Function CloseHandle Lib "kernel32"").
Code the click event of command button in "cmdNotepad_Click()." Define a variable of type PROCESS_INFORMATION, STARTUPINFO, a string to represent null and two longs for return values. Pass the Notepad.exe to the "CreateProcess" function:
CreateProcess(sNull, "Notepad.exe", ByVal 0&, ByVal 0&, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, sNull, sInfo, pInfo).
Terminate the process with "TerminateProcess(pInfo.hProcess, 0&)." Close the handle to the thread with "CloseHandle(pInfo.hThread)" and to the process with "CloseHandle(pInfo.hProcess)."
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for