Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the result of an application

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have written an little WebServer in Vb and now I need PHP-Support. I've tried to find a solution in VB, but there seems to be no one. If it's possible to grab the result of php.exe (and I know it is) then I would write an DLL to do this job for VB.

The name of the Exe-file is php.exe and it starts in Dos-Console
 
I'm not completely sure of this (the PHP development folks would probably be the people to ask - look for their mailing list), but I'd bet that php.exe writes its output ot STDOUT, so if you can pipe that (or otherwise redirect STDOUT), you should be able to capture that (now how do you do that in VB???? I have NO idea!).
 
Well, here's how to do it in VB (out of MSDN):

Option Explicit

Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long

Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, _
lpProcessAttributes As Any, lpThreadAttributes As Any, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As Any, lpProcessInformation As Any) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&

Private Sub ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _
As Long
Dim bytesread As Long, mybuff As String
Dim i As Integer

mybuff = String(256, Chr$(65))

sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&

ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
MsgBox "CreatePipe failed. Error: " & Err.LastDllError
Exit Sub
End If

start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES
start.hStdOutput = hWritePipe

' Start the shelled application:
ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then
MsgBox &quot;CreateProcess failed. Error: &quot; & Err.LastDllError
Exit Sub
End If

bSuccess = ReadFile(hReadPipe, mybuff, 100, bytesread, 0&)
If bSuccess = 1 Then
Text1.Text = Left(mybuff, bytesread)
Else
MsgBox &quot;ReadFile failed. Error: &quot; & Err.LastDllError
End If

ret& = CloseHandle(proc.hProcess)
ret& = CloseHandle(proc.hThread)
ret& = CloseHandle(hReadPipe)
ret& = CloseHandle(hWritePipe)
End Sub


Private Sub Command1_Click()
ExecCmd &quot;cmd.exe&quot;
End Sub

This doesn't do much except write the copyright stuff and terminate since there's no input, but another program would send its output (to the textbox, in this case).

Hope I don't get lynched for posting this here:) :) Hope that this helped! :)
 
That's exactly what i need, but I want that php runs invisible for the user, so that it does it work in the background and the result must be chunked, because winsock couldn't send long files.
 
You can probably get rid of the console window by adding
DETACHED_PROCESS to your CreateProcess() call:
NORMAL_PRIORITY_CLASS + DETACHED_PROCESS
(value = &H8). As far as spitting the blocks is concernend I guess you need to add a little to the program. (Especially since I'm not sure at what point winsock comes into the picture). :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top