|
-
Mar 4th, 2002, 02:20 PM
#1
Thread Starter
Junior Member
c output in vb
how do i run a c pgm in dos prompt using vb and view the output in a vb text box or any other feature of vb ?
plz advise
-
Mar 4th, 2002, 03:12 PM
#2
Fanatic Member
I don't know how to show output realtime in a textbox. (Maybe a real C guru will know how to do it.) I couldn't determine from your question if you meant that.
However, I know a way to show the contents in a textbox after execution.
I found this example to run DOS programs somewhere on the net (vbexplorer I think). It seems to be better than the Shell command, but I forgot why. At least this works well for me 
VB Code:
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
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 WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName 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 String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecCmd(cmdline As String) As Long
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
ret = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
' Wait for the shelled application to finish:
ret = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret
End Function
In your program, create a variable as long. Name it something like retVal, since it will hold the return value of your program.
Create another variable as string, like sCommand. This will be your command string. In your command string, direct the output of your C app to a file: "c:\dir\blah.exe >output.txt".
Call your C app like this:
retVal = ExecCmd(sCommand)
If retVal = 0, then execution has completed successfully, and you can go further with your code. If retVal <> 0, then something has happened, and the C program produced an error code.
In your VB app, read the contents of output.txt and show it in your textbox.
I'm not 100% sure if this will work, since I haven't tested it with redirection output to a file and then to a textbox, if your program is being called from VB.
-
Mar 4th, 2002, 03:15 PM
#3
Fanatic Member
Maybe I know a workaround to show your C app's output realtime, although it's being faked a little.
Your C app could write some texts to temporary files, which can be read by your VB app. You could use the GetTickCount API to check for the existance of new files.
This can be somewhat tricky, since a file might still be open when you're trying to read it in VB, but sound error handling surely will deal with it
-
Mar 4th, 2002, 03:23 PM
#4
Black Cat
VB Code:
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
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
[b]hStdInput As Long
hStdOutput As Long
hStdError As Long[/b]
End Type
The members I made bold can be pass handles to Pipes if you set the correct dwFlags value, enabling you to redirect Standard IO to a Pipe your VB app can read.
Josh
Get these: Mozilla Opera OpenBSD
I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.
-
Mar 4th, 2002, 03:55 PM
#5
Fanatic Member
Cool, I didn't know that. But how can VB read from that pipe. Is there another API call for that? Or should they refer to the hwnd property (or a different one) of your textbox?
-
Mar 4th, 2002, 04:13 PM
#6
Fanatic Member
Never mind, I already found an example
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
|