demerphq has asked for the wisdom of the Perl Monks concerning the following question:
Hi All,
Apologies straight off for what is essentially a VB question, but answering the question will make dealing VB code from Perl a lot easier for me, and as I have to do theis somewhat often I really would appreciate some consideration on it by you folks.
As we all know controlling external apps from Perl is easy and makes it possible to use Perl as glue. The normal and simplest means of having the processes communicate is over STDIN/STDOUT. What my problem is that it seems to be a real bitch to get VB to write to STDOUT in a useful way. Weve worked the following code out from a MS kb article
Option Explicit Public Const STD_OUTPUT_HANDLE = -11& Public Declare Function stdout Lib "kernel32" Alias "GetStdHandle" _ (Optional ByVal Handletype As Long = STD_OUTPUT_HANDLE) As Long Public Declare Function WriteFile Lib "kernel32" _ (ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToWri +te As Long, _ lpNumberOfBytesWritten As Long, Optional ByVal lpOverlapped As Long = +0&) As Long Sub Main() Dim sWriteBuffer As String Dim lBytesWritten As Long Dim hStdOut As Long sWriteBuffer = "This should go to STDOUT" hStdOut = stdout() WriteFile hStdOut, sWriteBuffer, Len(sWriteBuffer) + 1, lBytesWrit +ten End Sub
And it works. Sortof. If I compile the VB and then run it from the console it appears not to work. However if I pipe it to a file it works fine.
C:\Program Files\Microsoft Visual Studio\VB98>stdout C:\Program Files\Microsoft Visual Studio\VB98>stdout > foo.txt C:\Program Files\Microsoft Visual Studio\VB98>type foo.txt This should go to STDOUT C:\Program Files\Microsoft Visual Studio\VB98>stdout | more This should go to STDOUT C:\Program Files\Microsoft Visual Studio\VB98>stdout | perl -pne '' This should go to STDOUT
So Perl can read it etc, but its somewhat annoying to have to test it like this. Not everybody will realize that it must be piped to something more intelligent. (ha! more.exe considered more intelligent than VB).
If anyone can explain this strange behaviour or tell me what to do to have it behave as expected then Id be much obliged.
BTW, my conscience is clear for asking this question as the above code will almost certainly come in useful for someone wanting to wrap some VB app in a perl script....
Ok, ok, its not that clear. Its VB, and it makes me feel dirty too. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting VB To talk to Perl
by castaway (Parson) on Jul 23, 2003 at 13:04 UTC | |
by demerphq (Chancellor) on Jul 23, 2003 at 13:21 UTC | |
|
Re: Getting VB To talk to Perl
by BrowserUk (Patriarch) on Jul 23, 2003 at 13:41 UTC | |
by demerphq (Chancellor) on Jul 23, 2003 at 16:21 UTC | |
|
Re: Getting VB To talk to Perl
by JaWi (Hermit) on Jul 23, 2003 at 12:43 UTC | |
by demerphq (Chancellor) on Jul 23, 2003 at 13:22 UTC |