Results 1 to 6 of 6

Thread: c output in vb

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    india
    Posts
    18

    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

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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:
    1. Private Type STARTUPINFO
    2.   cb As Long
    3.   lpReserved As String
    4.   lpDesktop As String
    5.   lpTitle As String
    6.   dwX As Long
    7.   dwY As Long
    8.   dwXSize As Long
    9.   dwYSize As Long
    10.   dwXCountChars As Long
    11.   dwYCountChars As Long
    12.   dwFillAttribute As Long
    13.   dwFlags As Long
    14.   wShowWindow As Integer
    15.   cbReserved2 As Integer
    16.   lpReserved2 As Long
    17.   hStdInput As Long
    18.   hStdOutput As Long
    19.   hStdError As Long
    20.   End Type
    21.  
    22. Private Type PROCESS_INFORMATION
    23.   hProcess As Long
    24.   hThread As Long
    25.   dwProcessID As Long
    26.   dwThreadID As Long
    27. End Type
    28.  
    29. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    30.   hHandle As Long, ByVal dwMilliseconds As Long) As Long
    31.  
    32. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    33.   lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
    34.   lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
    35.   ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    36.   ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
    37.   lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    38.   PROCESS_INFORMATION) As Long
    39.  
    40. Private Declare Function CloseHandle Lib "kernel32" _
    41.   (ByVal hObject As Long) As Long
    42.  
    43. Private Declare Function GetExitCodeProcess Lib "kernel32" _
    44.  (ByVal hProcess As Long, lpExitCode As Long) As Long
    45.  
    46. Private Const NORMAL_PRIORITY_CLASS = &H20&
    47. Private Const INFINITE = -1&
    48.  
    49. Public Function ExecCmd(cmdline As String) As Long
    50.   Dim proc As PROCESS_INFORMATION
    51.   Dim start As STARTUPINFO
    52.   Dim ret As Long
    53.  
    54.   ' Initialize the STARTUPINFO structure:
    55.   start.cb = Len(start)
    56.  
    57.   ' Start the shelled application:
    58.   ret = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
    59.      NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
    60.  
    61.   ' Wait for the shelled application to finish:
    62.   ret = WaitForSingleObject(proc.hProcess, INFINITE)
    63.   Call GetExitCodeProcess(proc.hProcess, ret)
    64.   Call CloseHandle(proc.hThread)
    65.   Call CloseHandle(proc.hProcess)
    66.   ExecCmd = ret
    67.  
    68. 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.

  3. #3
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    VB Code:
    1. Private Type STARTUPINFO
    2.   cb As Long
    3.   lpReserved As String
    4.   lpDesktop As String
    5.   lpTitle As String
    6.   dwX As Long
    7.   dwY As Long
    8.   dwXSize As Long
    9.   dwYSize As Long
    10.   dwXCountChars As Long
    11.   dwYCountChars As Long
    12.   dwFillAttribute As Long
    13.   dwFlags As Long
    14.   wShowWindow As Integer
    15.   cbReserved2 As Integer
    16.   lpReserved2 As Long
    17.   [b]hStdInput As Long
    18.   hStdOutput As Long
    19.   hStdError As Long[/b]
    20.   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.

  5. #5
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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?

  6. #6
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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
  •  



Click Here to Expand Forum to Full Width