Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA - Child process - Redirect STDIN handle

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 structu
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
doesnt 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to redirect in VBA? stephen Excel Discussion (Misc queries) 0 January 11th 08 05:13 PM
Fill handle turned into a move handle Northwoods Excel Discussion (Misc queries) 1 March 2nd 07 03:40 PM
How to get the window handle of and excel instance using it's process ID pmox Excel Programming 0 April 6th 06 04:35 PM
How to count process running time ( process not finished) miao jie Excel Programming 0 January 13th 05 09:23 AM
How to count process running time ( process not finished) miao jie Excel Programming 2 January 12th 05 06:01 AM


All times are GMT +1. The time now is 03:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"