473,416 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Createprocess in VB.NET

Jai
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
Jul 4 '06 #1
1 10798
"Jai" <Ja*@discussions.microsoft.comwrote in message
news:71**********************************@microsof t.com...
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.
This is the Visual C++.NET newsgroup, you'll have to look elsewhere for VB
help. Furthermore, that looks like VB6, not VB.NET

For help calling Win32 API functions from .NET, try http://pinvoke.net

For example, here is the CreateProcess page with sample code:
http://pinvoke.net/default.aspx/kern...teProcess.html

>
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


Jul 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Achim Domma | last post by:
Hi, I try to start a process on windows using win32process.CreateProcess and want to to redirect the output. I set the STARTF_USESTDHANDLES flag in the STARTUPINFO structure. But I can not...
6
by: david.humpherys | last post by:
os:winnt python2.3.2 I have a exe that dumps info to the command line. I want to run this process and capture the stdout into a file. I think i'm close... any help appreciated. dh...
0
by: ByteSize | last post by:
Would be grateful of advice please. I have code in VB6 working as follows: lngReply = CreateProcess(sNull, txtStart, ByVal 0&, ByVal 0&, 1&, _ NORMAL_PRIORITY_CLASS, ByVal 0&, sNull, sInfo,...
1
by: DOT NET JIM | last post by:
in VB6 the api declares the createprocess lbEnv var as any. When converted to ..NET is change the type any to Object. The problem is, what kind of object. I can not get the env variables set when...
3
by: kal | last post by:
Hi, I am trying to write an application that will launch a second application using CreateProcess... SECURITY_ATTRIBUTES sa; STARTUPINFO si; PROCESS_INFORMATION pi; ::ZeroMemory( &sa,...
5
by: Stefano Camaiani | last post by:
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...
2
by: Paul Schenk | last post by:
Hi All :) Would be grateful of advice please. I have code in VB6 working as follows: lngReply = CreateProcess(sNull, txtStart, ByVal 0&, ByVal 0&, 1&, _ NORMAL_PRIORITY_CLASS, ByVal 0&,...
1
by: itmanager | last post by:
I am trying to create a process at a particular set of coordinates (for example 1,1) but the CreateProcess api seemingly ignores the settings in the dwX and dwY properties of the STARTUPINFO...
6
by: ss.teo | last post by:
I want to fork a new process and my executable binary is stored inside somewhere. Is there any possible way to fork a process using CreateProcess API without writing to the disk first? Like let's...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.