On 2003-10-11, Stefano Camaiani <st*****@subsonic.it> wrote:
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.GetLa stWin32Error())
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
--
Tom Shelton
MVP [Visual Basic]