CreateProcess

S

Stefano Camaiani

Hello, please someone have the working code on how to call the CreateProcess
API in Vb.Net?
I need to call the CreateProcess API directly and i should not use the
Vb.Net Process functions like dim MyProcess as new Process - MyProcess.Start

This is my code wich i transalated from Vb6 to Vb.Net:

<StructLayout(LayoutKind.Explicit, _ charset:=charset.auto)> _
Public Structure STARTUPINFO
Public cb As Short
Public lpReserved As String
Public lpDesktop As String
Public lpTitle As String
Public dwX As Short
Public dwY As Short
Public dwXSize As Short
Public dwYSize As Short
Public dwXCountChars As Short
Public dwYCountChars As Short
Public dwFillAttribute As Short
Public dwFlags As Short
Public wShowWindow As Short
Public cbReserved2 As Short
Public lpReserved2 As Short
Public hStdInput As Short
Public hStdOutput As Short
Public hStdError As Short
End Structure

<StructLayout(LayoutKind.Explicit, _ charset:=charset.auto)> _
Public Structure PROCESS_INFORMATION
Public hProcess As Short
Public hThread As Short
Public dwProcessId As Short
Public dwThreadId As Short
End Structure

<StructLayout(LayoutKind.Explicit, _ charset:=charset.auto)> _
Public Structure SECURITY_ATTRIBUTES
Public nLength As Short
Public lpSecurityDescriptor As Short
Public bInheritHandle As Short
End Structure


Private Declare Auto Function CreateProcess Lib "kernel32" _
(ByVal lpApplicationName As String, ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As SECURITY_ATTRIBUTES, _
ByVal lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles
As Integer, _
ByVal dwCreationFlags As Integer, ByVal lpEnvironment As IntPtr, _
ByVal lpCurrentDriectory As String, ByVal lpStartupInfo As STARTUPINFO,
_
ByVal lpProcessInformation As PROCESS_INFORMATION) As Integer


But i really don't know hot to call the process..

Any help will be greatly appreciated.
Stefano from Italy
 
H

Herfried K. Wagner [MVP]

* "Stefano Camaiani said:
Hello, please someone have the working code on how to call the CreateProcess
API in Vb.Net?
I need to call the CreateProcess API directly and i should not use the
Vb.Net Process functions like dim MyProcess as new Process - MyProcess.Start

This is my code wich i transalated from Vb6 to Vb.Net:

<StructLayout(LayoutKind.Explicit, _ charset:=charset.auto)> _
Public Structure STARTUPINFO
Public cb As Short
Public lpReserved As String
Public lpDesktop As String
Public lpTitle As String
Public dwX As Short
Public dwY As Short
Public dwXSize As Short
Public dwYSize As Short
Public dwXCountChars As Short
Public dwYCountChars As Short
Public dwFillAttribute As Short
Public dwFlags As Short
Public wShowWindow As Short
Public cbReserved2 As Short
Public lpReserved2 As Short
Public hStdInput As Short
Public hStdOutput As Short
Public hStdError As Short
End Structure

<StructLayout(LayoutKind.Explicit, _ charset:=charset.auto)> _
Public Structure PROCESS_INFORMATION
Public hProcess As Short
Public hThread As Short
Public dwProcessId As Short
Public dwThreadId As Short
End Structure

<StructLayout(LayoutKind.Explicit, _ charset:=charset.auto)> _
Public Structure SECURITY_ATTRIBUTES
Public nLength As Short
Public lpSecurityDescriptor As Short
Public bInheritHandle As Short
End Structure

Replace all 'As Short' with 'As Int32, 'bInheritHandle' can be declared
as 'Boolean', the handles (variables beginning with "h") should be
declared as 'IntPtr'.
Private Declare Auto Function CreateProcess Lib "kernel32" _
(ByVal lpApplicationName As String, ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As SECURITY_ATTRIBUTES, _
ByVal lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles
As Integer, _
ByVal dwCreationFlags As Integer, ByVal lpEnvironment As IntPtr, _
ByVal lpCurrentDriectory As String, ByVal lpStartupInfo As STARTUPINFO,
_
ByVal lpProcessInformation As PROCESS_INFORMATION) As Integer

The structures should be passed 'ByRef', 'bInheritHandles' can be
declared as 'Boolean'.

All untested!
 
T

Tom Shelton

Hello, please someone have the working code on how to call the CreateProcess
API in Vb.Net?
I need to call the CreateProcess API directly and i should not use the
Vb.Net Process functions like dim MyProcess as new Process - MyProcess.Start

This is my code wich i transalated from Vb6 to Vb.Net:

<snip>

Stephano!

I'm sorry I didn't get back to you sooner... Here is a quick example of
using CreateProcess that may help you (though, I still think you should
experiment more with System.Diagnostics.Process :)... It is pretty
rough, I through it together this morning, and I was sort of in a
hurry - so don't be to rough on me :)

Option Explicit On
Option Strict On

Imports System.Runtime.InteropServices

Public Class MainForm
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Go As System.Windows.Forms.Button
Friend WithEvents Display As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Go = New System.Windows.Forms.Button
Me.Display = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'Go
'
Me.Go.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Go.Location = New System.Drawing.Point(296, 232)
Me.Go.Name = "Go"
Me.Go.TabIndex = 0
Me.Go.Text = "&Go"
'
'Display
'
Me.Display.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Display.IntegralHeight = False
Me.Display.Location = New System.Drawing.Point(0, 0)
Me.Display.Name = "Display"
Me.Display.Size = New System.Drawing.Size(372, 228)
Me.Display.TabIndex = 1
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(372, 257)
Me.Controls.Add(Me.Display)
Me.Controls.Add(Me.Go)
Me.Name = "MainForm"
Me.Text = "ReadConsoleOutput"
Me.ResumeLayout(False)

End Sub

#End Region

<Flags()> _
Private Enum HANDLE_TYPES
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
End Enum

<Flags()> _
Private Enum FILL_ATTRIBUTES
FOREGROUND_BLUE = &H1 ' text color contains blue.
FOREGROUND_GREEN = &H2 ' text color contains green.
FOREGROUND_RED = &H4 ' text color contains red.
FOREGROUND_INTENSITY = &H8 ' text color is intensified.
BACKGROUND_BLUE = &H10 ' background color contains blue.
BACKGROUND_GREEN = &H20 ' background color contains green.
BACKGROUND_RED = &H40 ' background color contains red.
BACKGROUND_INTENSITY = &H80 ' background color is intensified.
End Enum

<Flags()> _
Private Enum START_UP_INFO_FLAGS
STARTF_USESHOWWINDOW = &H1
STARTF_USESIZE = &H2
STARTF_USEPOSITION = &H4
STARTF_USECOUNTCHARS = &H8
STARTF_USEFILLATTRIBUTE = &H10
STARTF_RUNFULLSCREEN = &H20
STARTF_FORCEONFEEDBACK = &H40
STARTF_FORCEOFFFEEDBACK = &H80
STARTF_USESTDHANDLES = &H100
End Enum

<Flags()> _
Private Enum SHOW_WINDOW As Short
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure STARTUPINFO
Public cb As Integer
Public lpReserved As IntPtr
Public lpDesktop As String
Public lpTitle As String
Public dwX As Integer
Public dwY As Integer
Public dwXSize As Integer
Public dwYSize As Integer
Public dwXCountChars As Integer
Public dwYCountChars As Integer
Public dwFillAttribute As FILL_ATTRIBUTES
Public dwFlags As START_UP_INFO_FLAGS
Public wShowWindow As SHOW_WINDOW
Public cbReserved2 As Short
Public lpReserved2 As IntPtr
Public hStdInput As IntPtr
Public hStdOutput As IntPtr
Public hStdError As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure PROCESS_INFORMATION
Public hProcess As IntPtr
Public hThread As IntPtr
Public dwProcessId As Integer
Public dwThreadId As Integer
End Structure

Private Declare Auto Function CreateProcess Lib "kernel32" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Integer, _
ByVal lpThreadAttributes As Integer, _
ByVal bInheritHandles As Boolean, _
ByVal dwCreationFlags As Integer, _
ByVal lpEnvironment As String, _
ByVal lpCurrentDirectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As IntPtr) As Boolean

Private Declare Function AllocConsole Lib "kernel32" () As Boolean
Private Declare Function FreeConsole Lib "kernel32" () As Boolean

Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As HANDLE_TYPES) As IntPtr

Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
Dim StartInfo As STARTUPINFO
Dim ProcessInfo As PROCESS_INFORMATION

With StartInfo
.cb = Marshal.SizeOf(StartInfo)
.lpReserved = IntPtr.Zero
.lpDesktop = Nothing
.lpTitle = Nothing
.dwFlags = START_UP_INFO_FLAGS.STARTF_USESTDHANDLES
.hStdInput = GetStdHandle(HANDLE_TYPES.STD_INPUT_HANDLE)
.hStdOutput = GetStdHandle(HANDLE_TYPES.STD_OUTPUT_HANDLE)
.hStdError = GetStdHandle(HANDLE_TYPES.STD_ERROR_HANDLE)
End With

If CreateProcess("c:\windows\system32\cmd.exe", "/c dir c:\", 0, 0, True, 0, Nothing, Nothing, StartInfo, ProcessInfo) Then
CloseHandle(ProcessInfo.hProcess)
CloseHandle(ProcessInfo.hThread)
Else
Dim win As New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show(win.Message)
End If
End Sub

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
AllocConsole()
End Sub

Private Sub MainForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
FreeConsole()
End Sub
End Class

HTH
 
S

Stefano Camaiani

Hi Tom,
you are not only a HERO, but also a GENIUS!!!

I must pay to you thousands beers! ;)
I will try this immediately....

Really THANKS!!! I really hope to meet more people like you!

Hope to hear from you soon!

Stefano.
 
S

Stefano Camaiani

Hi Tom,
this is my code to call the CreateProcess API:

Public Function StartProcess() As Boolean
If AllocMyConsole() = True Then
Dim MyStartupInfo As STARTUPINFO
Dim MyProcessInformation As PROCESS_INFORMATION
If CreateProcess("", "Mem.exe", 0, 0, True, 0, "", "",
MyStartupInfo, MyProcessInformation) = True Then
MsgBox("Yahoo!")
Else
MsgBox("Oh-Oh...")
End If
Else
MsgBox("Error: I should not Allocate the Console!")
End If
End Function


Public Function AllocMyConsole() As Boolean
If AllocConsole Then
Return True
Else
Return False
End If
End Function

Public Function FreeMyConsole() As Boolean
If FreeConsole Then
Return True
Else
Return False
End If


Eh-eh: i receive ever the Oh-Oh message.... where i'm wrong?
THKsssssss
Stefano
 
S

Stefano Camaiani

Hello TOM, sorry for my last request, i seen your message on DevelopersDex
and your code was cutted.....now i opened it my the News and i get it
complete....
Thanks!!!!

Stefano



Tom Shelton said:
<snip>

Stephano!

I'm sorry I didn't get back to you sooner... Here is a quick example of
using CreateProcess that may help you (though, I still think you should
experiment more with System.Diagnostics.Process :)... It is pretty
rough, I through it together this morning, and I was sort of in a
hurry - so don't be to rough on me :)

Option Explicit On
Option Strict On

Imports System.Runtime.InteropServices

Public Class MainForm
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Go As System.Windows.Forms.Button
Friend WithEvents Display As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Go = New System.Windows.Forms.Button
Me.Display = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'Go
'
Me.Go.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Go.Location = New System.Drawing.Point(296, 232)
Me.Go.Name = "Go"
Me.Go.TabIndex = 0
Me.Go.Text = "&Go"
'
'Display
'
Me.Display.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Display.IntegralHeight = False
Me.Display.Location = New System.Drawing.Point(0, 0)
Me.Display.Name = "Display"
Me.Display.Size = New System.Drawing.Size(372, 228)
Me.Display.TabIndex = 1
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(372, 257)
Me.Controls.Add(Me.Display)
Me.Controls.Add(Me.Go)
Me.Name = "MainForm"
Me.Text = "ReadConsoleOutput"
Me.ResumeLayout(False)

End Sub

#End Region

<Flags()> _
Private Enum HANDLE_TYPES
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
End Enum

<Flags()> _
Private Enum FILL_ATTRIBUTES
FOREGROUND_BLUE = &H1 ' text color contains blue.
FOREGROUND_GREEN = &H2 ' text color contains green.
FOREGROUND_RED = &H4 ' text color contains red.
FOREGROUND_INTENSITY = &H8 ' text color is intensified.
BACKGROUND_BLUE = &H10 ' background color contains blue.
BACKGROUND_GREEN = &H20 ' background color contains green.
BACKGROUND_RED = &H40 ' background color contains red.
BACKGROUND_INTENSITY = &H80 ' background color is intensified.
End Enum

<Flags()> _
Private Enum START_UP_INFO_FLAGS
STARTF_USESHOWWINDOW = &H1
STARTF_USESIZE = &H2
STARTF_USEPOSITION = &H4
STARTF_USECOUNTCHARS = &H8
STARTF_USEFILLATTRIBUTE = &H10
STARTF_RUNFULLSCREEN = &H20
STARTF_FORCEONFEEDBACK = &H40
STARTF_FORCEOFFFEEDBACK = &H80
STARTF_USESTDHANDLES = &H100
End Enum

<Flags()> _
Private Enum SHOW_WINDOW As Short
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure STARTUPINFO
Public cb As Integer
Public lpReserved As IntPtr
Public lpDesktop As String
Public lpTitle As String
Public dwX As Integer
Public dwY As Integer
Public dwXSize As Integer
Public dwYSize As Integer
Public dwXCountChars As Integer
Public dwYCountChars As Integer
Public dwFillAttribute As FILL_ATTRIBUTES
Public dwFlags As START_UP_INFO_FLAGS
Public wShowWindow As SHOW_WINDOW
Public cbReserved2 As Short
Public lpReserved2 As IntPtr
Public hStdInput As IntPtr
Public hStdOutput As IntPtr
Public hStdError As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure PROCESS_INFORMATION
Public hProcess As IntPtr
Public hThread As IntPtr
Public dwProcessId As Integer
Public dwThreadId As Integer
End Structure

Private Declare Auto Function CreateProcess Lib "kernel32" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Integer, _
ByVal lpThreadAttributes As Integer, _
ByVal bInheritHandles As Boolean, _
ByVal dwCreationFlags As Integer, _
ByVal lpEnvironment As String, _
ByVal lpCurrentDirectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As IntPtr) As Boolean

Private Declare Function AllocConsole Lib "kernel32" () As Boolean
Private Declare Function FreeConsole Lib "kernel32" () As Boolean

Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As HANDLE_TYPES) As IntPtr

Private Sub Go_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Go.Click
Dim StartInfo As STARTUPINFO
Dim ProcessInfo As PROCESS_INFORMATION

With StartInfo
.cb = Marshal.SizeOf(StartInfo)
.lpReserved = IntPtr.Zero
.lpDesktop = Nothing
.lpTitle = Nothing
.dwFlags = START_UP_INFO_FLAGS.STARTF_USESTDHANDLES
.hStdInput = GetStdHandle(HANDLE_TYPES.STD_INPUT_HANDLE)
.hStdOutput = GetStdHandle(HANDLE_TYPES.STD_OUTPUT_HANDLE)
.hStdError = GetStdHandle(HANDLE_TYPES.STD_ERROR_HANDLE)
End With

If CreateProcess("c:\windows\system32\cmd.exe", "/c dir c:\", 0,
0, True, 0, Nothing, Nothing, StartInfo, ProcessInfo) Then
CloseHandle(ProcessInfo.hProcess)
CloseHandle(ProcessInfo.hThread)
Else
Dim win As New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show(win.Message)
End If
End Sub

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AllocConsole()
End Sub

Private Sub MainForm_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top