|
-
Jul 21st, 2003, 12:17 PM
#1
Thread Starter
Addicted Member
Command lines in VB
I'm trying to make a program that reads the dump file from an exe in the command prompt.
I have the exe that does what I want, but it's run in a command prompt. It can dump its output to a text file. So, I'm going to have it so the person enters a computer name (which goes into variable ComputerName), and when the program runs it will shell the command line which dumps to a text file, then the "front end" will import the data into a list box.
At a command prompt, you can type:
program.exe \\computername >c:\text.txt
and it'll dump the info to a text file. But if I do
Code:
Shell "C:\program.exe " & ComputerName & " >c:\text.txt", vbNormalFocus
it runs, but the exe says it can't find the process >c:\text.txt (info on specific processes is another thing it can do)
If I run just
Code:
Shell "C:\program.exe " & ComputerName
It works fine, of course it shows the data in the window, and doesn't dump into a text file that way 
So it looks like the problem is happening after the variable. I've tried using vbKeySpace instead of an actual space and some other silly stuff that didn't work. Anyone know what VB might be doing when it passes the command through, that would be making it see the command differently than plain old "program.exe \\computername >c:\text.txt"?
-
Jul 21st, 2003, 12:58 PM
#2
PowerPoster
could be that shell doesn't support pipes and an even less likely issue is that you might need a space after the pipe symbol
-
Jul 21st, 2003, 01:14 PM
#3
Thread Starter
Addicted Member
Tried the space after the pipe, the program doesn't recognize the command then.. Ack, shell doesn't support pipes? If that's the case it would certainly explain it
-
Jul 21st, 2003, 01:17 PM
#4
Hyperactive Member
Try this
VB Code:
Shell chr(34) & "C:\program.exe " & ComputerName & " >c:\text.txt" & chr(34), vbNormalFocus
-
Jul 21st, 2003, 01:56 PM
#5
You could try the Shell API command...
VB Code:
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
-
Jul 23rd, 2003, 02:10 PM
#6
Thread Starter
Addicted Member
thanks Woka, I tried that but now it doesn't launch at all. I changed ComputerName to include the blurb about dumping it to a text file so I could include it all as one command.
Code:
ComputerName = ComputerName & " >c:\text.txt"
ShellExecute 0, vbNullString, "C:\program.exe", ComputerName, "C:\", SW_SHOWNORMAL
The cmd window doesn't even come up 
I saw a post about assigning it to a long value to see if it's getting an error message when opening.
lngWhat returns 42. Have I found the meaning of the universe? At any rate I haven't found a way to get this darn thing to launch
-
Jul 23rd, 2003, 02:50 PM
#7
Code:
Shell "C:\program.exe " & ComputerName & " > c:\text.txt", vbNormalFocus
Note the space after the ">"
-
Jul 23rd, 2003, 08:22 PM
#8
The picture isn't missing
Originally posted by jdc2000
Code:
Shell "C:\program.exe " & ComputerName & " > c:\text.txt", vbNormalFocus
Note the space after the ">"
it should work regardless of space.
you could try this... redirects output of command window:
VB Code:
'Redirects output from console program to textbox.
'Requires two textboxes and one command button.
'Set MultiLine property of Text2 to true.
'
'Original bcx version of this program was made by
'Visit Jernejs site at [url]http://www2.arnes.si/~sopjsimo/[/url]
'
'Note: don't run plain DOS programs with this example
'under Windows 95,98 and ME, as the program freezes when
'execution of program is finnished.
Option Explicit
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId 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 Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type OVERLAPPED
ternal As Long
ternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2
Private Sub Command1_Click()
Command1.Enabled = False
Redirect Text1.Text, Text2
Command1.Enabled = True
End Sub
Private Sub Form_Load()
Text1.Text = "ping"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Command1.Enabled = False Then Cancel = True
End Sub
Sub Redirect(cmdLine As String, objTarget As Object)
Dim i%, t$
Dim pa As SECURITY_ATTRIBUTES
Dim pra As SECURITY_ATTRIBUTES
Dim tra As SECURITY_ATTRIBUTES
Dim pi As PROCESS_INFORMATION
Dim sui As STARTUPINFO
Dim hRead As Long
Dim hWrite As Long
Dim bRead As Long
Dim lpBuffer(1024) As Byte
pa.nLength = Len(pa)
pa.lpSecurityDescriptor = 0
pa.bInheritHandle = True
pra.nLength = Len(pra)
tra.nLength = Len(tra)
If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
sui.cb = Len(sui)
GetStartupInfo sui
sui.hStdOutput = hWrite
sui.hStdError = hWrite
sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
sui.wShowWindow = SW_HIDE
If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
SetWindowText objTarget.hwnd, ""
Do
Erase lpBuffer()
If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
DoEvents
Else
CloseHandle pi.hThread
CloseHandle pi.hProcess
Exit Do
End If
CloseHandle hWrite
Loop
CloseHandle hRead
End If
End If
End Sub
Remember, if someone's post was not helpful, you can always rate their post negatively  .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|