How to Make VB6 Open in EXE

Written by lora covrett
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Make VB6 Open in EXE
Visual Basic programs often interact with other programs. (Stockbyte/Stockbyte/Getty Images)

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:
Moderate

Other People Are Reading

Instructions

    Shell Command

  1. 1

    Start a new Visual Basic Standard EXE project. On "Form1" that is created by default, add a command button and name it "cmdNotepad."

  2. 2

    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")."

  3. 3

    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)."

  4. 4

    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.

    CreateProcess

  1. 1

    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.

  2. 2

    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"").

  3. 3

    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).

  4. 4

    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)."

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.