VBA - Child process - Redirect STDIN handle

S

Sergio Neves

Hi!

I am a civil engineering and I do research at University of Porto.
I have developed a small application in C defined by the followind code:

'------------------------------------------------------------------------------

#include <stdio.h>

#define F_MAX_STRING_LENGTH 512


int main()
{
char szWord[F_MAX_STRING_LENGTH];

printf("Enter a string: ");
scanf("%s", szWord);
printf("You wrote: %s\n", szWord);

printf("Press the return key... ");
getchar();
getchar();
}

'------------------------------------------------------------------------------

It basically reads a string and does a printf. Then I developed a program in
Visual Basic that creates a process and calls the previous program. I want to
redirect the STDIN handle of the child process to a string defined in the
Visual Basic application (szAux). I developed the following two modules:

'------------------------------------------------------------------------------

Sub Test()

Dim hAux As Long
Dim lBytesWritten As Long
Dim hReadPipe As Long
Dim hWritePipe3 As Long
Dim szAux As String
Dim szCurrentDirectory As String
Dim sa As SECURITY_ATTRIBUTES
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&

szAux = “ABCDâ€

'A pipe for redirection of STDIN
hAux = CreatePipe(hReadPipe, hWritePipe, sa, 0)

' Initialize the STARTUPINFO structure:
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES

'Preparing to start the process with redirected handles
start.hStdInput = hReadPipe

szPrefemix = M:\femix\Misc\Launcher\Test\Prog\Debug\Prog.exe
szCurrentDirectory = "M:\femix\Misc\Launcher\Test\Prog\Debug\"

'Writing to the started process's STDIN
hAux = WriteFile(hWritePipe, szAux, 100, lBytesWritten, ByVal 0&)

' Start the shelled application:
hAux = CreateProcess(vbNullString, szPrefemix, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, szCurrentDirectory, start, proc)

' Wait for the shelled application to finish:
hAux = WaitForSingleObject(proc.hProcess, INFINITE)

Call GetExitCodeProcess(proc.hProcess, hAux)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)

End Sub

'------------------------------------------------------------------------------

Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

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

Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Public 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


Public Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, _
lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, _
lpOverlapped As Any) As Long

Public Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
lpOverlapped As Any) As Long

Public Declare Function FlushFileBuffers Lib "kernel32" ( _
ByVal hFile As Long) As Long

Public 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

Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Public Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long

Public Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

'------------------------------------------------------------------------------

The problem is that when I run all this code it opens the DOS windows but it
doesn’t appear anything.
Basically all of these are test files. In the future I want to use Excel to
open my executable files instead of using the DOS batch files. I want to use
this code instead of the following DOS command:

(echo ABCD) | M:\femix\Misc\Launcher\Test\Prog\Debug\Prog.exe

I appreciate any help! Thanks a lot

Best regards

Sergio Neves
 

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