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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integrating VB with VC++

Status
Not open for further replies.

michaelkrauklis

Programmer
Dec 5, 2001
226
US
I have a VC++ program that runs in the comman prompt. I'd like to put a GUI on top of it. I created a simple VB application and I can easily run the C++ program using the shell command, but is there any way to get the output from my VC++ program back to the VB program? MYenigmaSELF:-9
myenigmaself@yahoo.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
Thanks. That was interesting and it was the answer I was expecting to get. Not exactly what I want though. My program has deeply nested functions, and needs to stay in scope throughout execution since there are global variables. The problem is that I don't want to return a huge string at the end of all this. I want to send back the output a buffer at a time. Is this possible in the other method? I don't see how if it is.

Also, is there a good place to get info on VB? Specificaly the shell command? Thanks for the help. MYenigmaSELF:-9
myenigmaself@yahoo.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
Since I was playing with consoles yesterday to figure out how to make a VB application into a Console Application, I continued that playing this evening to see if your query could be answered. here's the example code I came up with. Note that it is only illustrative. You will need to do additional work to get it to do exactly what you want, but I believe all the essentials are here:
[tt]
Option Explicit
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Private Declare Function SetStdHandle Lib "kernel32" (ByVal nStdHandle As Long, ByVal nHandle As Long) As Long
Private Const STD_ERROR_HANDLE = -12&
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long

Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long

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


Private Sub Command1_Click()
Dim OldStdOut As Long
Dim hReadPipe As Long
Dim hWritePipe As Long
Dim result As Long
Dim ConsoleOutBuffer As String
Dim cbRead As Long
Dim cbBytesToRead

Dim SA As SECURITY_ATTRIBUTES

If AllocConsole() Then

' Redirect STDOUT to a pipe
SA.nLength = Len(SA)
result = CreatePipe(hReadPipe, hWritePipe, SA, 0&)
OldStdOut = GetStdHandle(STD_OUTPUT_HANDLE)
Call SetStdHandle(STD_OUTPUT_HANDLE, hWritePipe)

' Shell a console application which then inherits our console,
' rather than creating it's own. When the shelled console app
' produces output it goes through our pipe, and we can read
' it at the other end
Shell "c:\windows\ipconfig"
ConsoleOutBuffer = Space(10 * 1024) ' Allow 10K

result = ReadFile(hReadPipe, ByVal ConsoleOutBuffer, Len(ConsoleOutBuffer), cbRead, ByVal 0&)
If result <> 0 Then

' Do your processing of the buffer here

' The, for completeness of the example send the output to the original standard stream
' so we can see it on the console we created
result = WriteFile(OldStdOut, ByVal ConsoleOutBuffer, cbRead, cbRead, ByVal 0&)
End If
' Clean up
SetStdHandle STD_OUTPUT_HANDLE, OldStdOut
FreeConsole ' Console will vanish here
CloseHandle hReadPipe
CloseHandle hWritePipe
Else
MsgBox &quot;Could not create console&quot;
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top