DISCOVER
×

How to Use CreateProcess in VB6

Updated July 20, 2017

"CreateProcess" is a function available in one of the Microsoft Windows core application programming interfaces (APIs) available with the Windows operating system. "CreateProcess" opens a program as a Windows process and within its own thread. Windows APIs give more flexibility to VB programmers. For example, there are several functions available within the Windows API that allow programmers to perform specific actions with a function call to the API rather than attempting to write their own code to do the same thing.

Start a new Standard EXE Visual Basic project. Add user-defined types named "PROCESS_INFORMATION" and "STARTUPINFO" in the code for "Form1" that is automatically created with the new project. These are structures used in Visual Basic to group data and are required parameter types of the "CreateProcess" function.

Create two constants that are also parameters of the "CreateProcess" function.

Const SYNCHRONISE = 1048576

Const NORMAL_PRIORITY_CLASS = &H20&

Declare the "CreateProcess" function as "Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" ."

Declare additional functions to 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"").

Add a command button to "Form 1' and name it "cmdOpenNotepad." In the code for "cmdOpenNotepad", 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 as follows:

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

Warning

Always close the thread handle and the process handle to avoid memory leaks.

bibliography-icon icon for annotation tool Cite this Article

About the Author

Lora Covrett has been writing professionally since 2006. She has published a humor book, as well as articles for various online publications. Covrett is an IBM-certified database administrator and solution expert. She completed her Bachelor of Science in computer information systems at Devry University.