how to compile C++ code with input file using API

R

rach.ng

I'm trying to compile a C++ file with the Microsoft C/C++ compiler in
VBA.
Thru the use of API (CreateProcess) I was able to compile the C++ file.

However, as I tried to include input file, it doesn't work.
The result is simply the compilation without an input file.

Any help is appreciated. Thanks!!



In main:
========
Call compileAndWriteOutput("cl " & "inputfile.txt" & " " &
"program.cpp", outputFileName)


VBA code
========
Option Explicit
' Contants
Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const STARTF_USESTDHANDLES As Long = &H100
Private Const SW_HIDE As Integer = 0
' Types
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
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


' Declares
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long,
_
phWritePipe As
Long, _
lpPipeAttributes As
Any, _
ByVal nSize As
Long) 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 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 CloseHandle Lib "kernel32" (ByVal hObject As
Long) As Long

'Delete file using deleteFile API
Private Declare Function DeleteFile Lib "kernel32.dll" Alias
"DeleteFileA" (ByVal lpFileName As String) As Long ' Declaration


'---------------------------------------------------
' Call this sub to compile and write output thru a console app.
Public Sub compileAndWriteOutput(ByVal sCommandLine As String, _
ByVal outputFile 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
Dim waitError As Long
Dim aFile As Integer

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_HIDE ' hide the window
.hStdOutput = hPipeWrite
.hStdError = hPipeWrite
End With 'SI

If CreateProcess(vbNullString, sCommandLine, ByVal 0&, ByVal 0&, 1,
0&, ByVal 0&, sStartInFolder, si, pi) Then

Call CloseHandle(hPipeWrite)
Call CloseHandle(pi.hThread)
hPipeWrite = 0

aFile = FreeFile
Open outputFile For Output As #aFile
Do
DoEvents
If ReadFile(hPipeRead, baOutput(0), BUFSIZE, lBytesRead,
ByVal 0&) = 0 Then
Exit Do
End If
sOutput = Strings.Left$(StrConv(baOutput(), vbUnicode),
lBytesRead)
Print #aFile, sOutput
Loop
Call CloseHandle(pi.hProcess)
Close #aFile
End If
' To make sure...
Call CloseHandle(hPipeRead)
Call CloseHandle(hPipeWrite)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top