473,387 Members | 3,821 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,387 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 10789
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.