Hi Devron,
Thanks for your quickly reply!
Here I write an interop code to call CreateProcess in VB.NET.
<StructLayout(LayoutKind.Sequential)> Public Structure
SECURITY_ATTRIBUTES
Public nLength As Integer
Public lpSecurityDescriptor As Integer
Public bInheritHandle As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure STARTUPINFO
Public cb As Integer
Public lpReserved As String
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 Integer
Public dwFlags As Integer
Public wShowWindow As Integer
Public cbReserved2 As Integer
Public lpReserved2 As Integer
Public hStdInput As Integer
Public hStdOutput As Integer
Public hStdError As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
PROCESS_INFORMATION
Public hProcess As Integer
Public hThread As Integer
Public dwProcessId As Integer
Public dwThreadId As Integer
End Structure
Public Declare Function CreateProcess Lib "kernel32" Alias
"CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As
String, <MarshalAs(UnmanagedType.Struct)> ByRef lpProcessAttributes As
SECURITY_ATTRIBUTES, <MarshalAs(UnmanagedType.Struct)> ByRef
lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As
Integer, ByVal dwCreationFlags As Integer, ByVal lpEnvironment As Integer,
ByVal lpCurrentDriectory As String, <MarshalAs(UnmanagedType.Struct)> ByRef
lpStartupInfo As STARTUPINFO, <MarshalAs(UnmanagedType.Struct)> ByRef
lpProcessInformation As PROCESS_INFORMATION) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim saProcess As SECURITY_ATTRIBUTES
Dim saThread As SECURITY_ATTRIBUTES
saProcess.bInheritHandle = True
saProcess.lpSecurityDescriptor = 0
saProcess.nLength = Marshal.SizeOf(saProcess)
saThread.bInheritHandle = True
saThread.lpSecurityDescriptor = 0
saThread.nLength = Marshal.SizeOf(saThread)
Dim pi As PROCESS_INFORMATION
Dim si As STARTUPINFO
CreateProcess("C:\WINDOWS\system32\notepad.exe", " C:\test.txt",
saProcess, saThread, True, 0, Nothing, vbNullString, si, pi)
End Sub
I agree with Tom's suggestion.
From you code snipper, it seems that when you create a new child process,
the parent process will wait. That is to say the programming modal is
synchronized, so did you have any concern why you do need to create a new
process but not a thread or just do the thing in the same process.
As Tom said, in .NET you can redirect an process's stdin and stdout to do
the IPC stuff which will help you avoid the P/Invoke stuff.
In ASP.NET, as a webserver, the server application will get the request and
write the request to a named pipe and the spawned child process will read
the request from the named pipe and write the response to the parent
process by the named pipe too. But this will cause many IPC operation. So I
strongly recomment you deal with the request in the current thread, since
it seems that you do not need multiple thread function. If you wants to
separate the code that handles the request by create a new classlibrary.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.