I have written some VB 6.0 code which runs under Windows XP Pro that seems to be having some trouble. On MSDN, I found some code which should allow be to create a process that executes an Oracle TNSPING from a command line. Then the code should read the output of the created process and stip out the IP address of the Oracle database server. However, when I run the code, the DOS window is created, but no commands appear and nothing runs. After about 10 seconds the DOS window closes. I have included the VB code below and would appreciate some assitance to help me determine what's going wrong.
Here is the VB code:
Public Sub ExecAndCapture(ByVal sCommandLine As String, _
cTextBox As String, _
Optional ByVal sStartInFolder As _
String = vbNullString)
Const BUFSIZE As Long = 1024 * 10
Dim hPipeRead As Long
Dim hPipeWrite As Long
Dim sa As SECURITY_ATTRIBUTES
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim baOutput(BUFSIZE) As Byte
Dim sOutput As String
Dim lBytesRead As Long
With sa
.nLength = Len(sa)
.bInheritHandle = 1 ' get inheritable pipe
' handles
End With 'SA
If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
Exit Sub
End If
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW Or _
STARTF_USESTDHANDLES
.wShowWindow = SW_SHOW ' hide the window
.hStdOutput = hPipeWrite
.hStdError = hPipeWrite
End With 'SI
If CreateProcess(vbNullString, sCommandLine, ByVal 0&, _
ByVal 0&, 1, 0&, ByVal 0&, vbNullString, si, pi) _
Then
Call CloseHandle(hPipeWrite)
Call CloseHandle(pi.hThread)
hPipeWrite = 0
Do
DoEvents
If ReadFile(hPipeRead, baOutput(0), BUFSIZE, _
lBytesRead, ByVal 0&) = 0 Then
Exit Do
End If
sOutput = Left$(StrConv(baOutput(), vbUnicode), _
lBytesRead)
cTextBox = sOutput
Loop
Call CloseHandle(pi.hProcess)
End If
' To make sure...
Call CloseHandle(hPipeRead)
Call CloseHandle(hPipeWrite)
End Sub
Here is the VB code:
Public Sub ExecAndCapture(ByVal sCommandLine As String, _
cTextBox As String, _
Optional ByVal sStartInFolder As _
String = vbNullString)
Const BUFSIZE As Long = 1024 * 10
Dim hPipeRead As Long
Dim hPipeWrite As Long
Dim sa As SECURITY_ATTRIBUTES
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim baOutput(BUFSIZE) As Byte
Dim sOutput As String
Dim lBytesRead As Long
With sa
.nLength = Len(sa)
.bInheritHandle = 1 ' get inheritable pipe
' handles
End With 'SA
If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
Exit Sub
End If
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW Or _
STARTF_USESTDHANDLES
.wShowWindow = SW_SHOW ' hide the window
.hStdOutput = hPipeWrite
.hStdError = hPipeWrite
End With 'SI
If CreateProcess(vbNullString, sCommandLine, ByVal 0&, _
ByVal 0&, 1, 0&, ByVal 0&, vbNullString, si, pi) _
Then
Call CloseHandle(hPipeWrite)
Call CloseHandle(pi.hThread)
hPipeWrite = 0
Do
DoEvents
If ReadFile(hPipeRead, baOutput(0), BUFSIZE, _
lBytesRead, ByVal 0&) = 0 Then
Exit Do
End If
sOutput = Left$(StrConv(baOutput(), vbUnicode), _
lBytesRead)
cTextBox = sOutput
Loop
Call CloseHandle(pi.hProcess)
End If
' To make sure...
Call CloseHandle(hPipeRead)
Call CloseHandle(hPipeWrite)
End Sub