G
Guest
Please provide sample code for using Createprocess in VB.NET. The code I am
using is given below. But I get "Error 91: Object referrence not set to an
instance of the object" at CreateProcess calling line. Please provide some
input.
CODE
====
Structure STARTUPINFO
Public cb As Long
Public lpReserved As String
Public lpDesktop As String
Public lpTitle As String
Public dwX As Long
Public dwY As Long
Public dwXSize As Long
Public dwYSize As Long
Public dwXCountChars As Long
Public dwYCountChars As Long
Public dwFillAttribute As Long
Public dwFlags As Long
Public wShowWindow As Long
Public cbReserved2 As Long
Public lpReserved2 As Long
Public hStdInput As Long
Public hStdOutput As Long
Public hStdError As Long
End Structure
Structure PROCESS_INFORMATION
Public hProcess As Long
Public hThread As Long
Public dwProcessId As Long
Public dwThreadID As Long
End Structure
Private Declare Function CreateProcess Lib "kernel32" _
Alias "CreateProcessA" _
(ByVal lpAppName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
ByVal lpStartupInfo As STARTUPINFO, _
ByVal lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal
hProcess As Long, ByVal lpExitCode As Long) As Long
Public Function RunProcess(ByVal cmdline As String) As Long
Dim sNull As String
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
start.cb = Len(start)
Dim lngRet As Long
lngRet = CreateProcess(0&, cmdline, 0&, 0&, 1&, _
CREATE_NO_WINDOW, 0&, 0&, _
start, proc)
'Wait for the application to finish
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
'Close the handle to the process
Call CloseHandle(proc.hProcess)
'Close the handle to the thread created
Call CloseHandle(proc.hThread)
Exit Function
End Function
using is given below. But I get "Error 91: Object referrence not set to an
instance of the object" at CreateProcess calling line. Please provide some
input.
CODE
====
Structure STARTUPINFO
Public cb As Long
Public lpReserved As String
Public lpDesktop As String
Public lpTitle As String
Public dwX As Long
Public dwY As Long
Public dwXSize As Long
Public dwYSize As Long
Public dwXCountChars As Long
Public dwYCountChars As Long
Public dwFillAttribute As Long
Public dwFlags As Long
Public wShowWindow As Long
Public cbReserved2 As Long
Public lpReserved2 As Long
Public hStdInput As Long
Public hStdOutput As Long
Public hStdError As Long
End Structure
Structure PROCESS_INFORMATION
Public hProcess As Long
Public hThread As Long
Public dwProcessId As Long
Public dwThreadID As Long
End Structure
Private Declare Function CreateProcess Lib "kernel32" _
Alias "CreateProcessA" _
(ByVal lpAppName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
ByVal lpStartupInfo As STARTUPINFO, _
ByVal lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal
hProcess As Long, ByVal lpExitCode As Long) As Long
Public Function RunProcess(ByVal cmdline As String) As Long
Dim sNull As String
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
start.cb = Len(start)
Dim lngRet As Long
lngRet = CreateProcess(0&, cmdline, 0&, 0&, 1&, _
CREATE_NO_WINDOW, 0&, 0&, _
start, proc)
'Wait for the application to finish
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
'Close the handle to the process
Call CloseHandle(proc.hProcess)
'Close the handle to the thread created
Call CloseHandle(proc.hThread)
Exit Function
End Function