Hello All
on application A, I have 3rd party which call application B. Application B use WriteFile API to return result to StdOut (code is below).
My question is - is it possible to somehow add that code into application A?
With other words, instead of calling external exe, I would like to call procedure from inside same app. Of course, I added same code to app A but I don’t know how to write result to stdOut (form same app).
Code is here:
program
include('winequ.clw')
STD_INPUT_HANDLE EQUATE(-10)
STD_OUTPUT_HANDLE EQUATE(-11)
!- For WriteFile() API
OVERLAPPED GROUP,TYPE
Internal DWORD
InternalHigh DWORD
Offset DWORD
OffsetHigh DWORD
hEvent HANDLE
END
map
MODULE('win32.lib')
GetStdHandle(DWORD),HANDLE,PASCAL
ReadFile(HANDLE,LPVOID,DWORD,*DWORD,*OVERLAPPED),BOOL,PASCAL,RAW
WriteFile(HANDLE,*CSTRING,DWORD,*DWORD,<*OVERLAPPED>),BOOL,PASCAL,RAW
GetEnvironmentVar(*LPCSTR,*LPSTR,DWORD),DWORD,PASCAL,RAW,NAME('GetEnvironmentVariableA')
getlasterror(),dword,pascal
end
end
r ulong
!- For sending and receiving IO from server
hStdIn LONG
hStdOut LONG
!- For GetEnvironmentVar()
VarLen EQUATE(129)
RtnVar LPSTR(VarLen) ! Hold contents of returned variable.
VarName LPCSTR(40) ! Name of environment variable.
formdata LPSTR(VarLen)
!- Writefile vars
lBytesWritten ULONG
OutCString CSTRING(255)
OVERLAPPED_ LIKE(OVERLAPPED)
code
hStdIn = GetStdHandle(STD_INPUT_HANDLE)
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)
!- Retreive environment settings for IO parms
VarName = 'QUERY_STRING'
Rslt# = GetEnvironmentVar(VarName,formdata,VarLen)
!- Send header to start HTML form back to STDOUT
OutCString = 'Content-type: text/html<13,10><13,10><<HTML><<BODY>' & |
'<<P>Hello World from - ' & formdata & '.<</BODY><</HTML>'
r = WriteFile(hStdOut,OutCString,LEN(OutCString),lBytesWritten,OVERLAPPED_)
if r = false then
r = GetLastError()
i# = message(r,'WriteFile Rtn')
.
Are you starting your program from a command prompt? What worked for me was using AttachConsole to connect to the existing command prompt (if you did) or CreateConsoleScreenBuffer/SetConsoleActiveScreenBuffer to create a new console (if you didn’t)
Then use WriteFile like normal, but you don’t need an overlapped structure on the end.
Ultimately WriteFile is just a mechanism to write data to an existing console, hence the console APIs listed above. You can also use WriteConsole as well, once you have the console defined.
See Input and Output Methods - Windows Console | Microsoft Learn
I use program A and call program B from it, which uses the previously posted code and works perfectly.
Program B creates some value and closes, and I get the value in program A.
Now I’m trying to avoid calling program B and I want to call some procedure (inside program A) that will do the same as program B did before, that is to create some value and return it. The point is that it must return it as StdOut. So I want to avoid calling an external exe program and use a procedure in an existing program that will return the result to stdOut. I don’t know if that’s possible. Program A in which I need stdOut is not a console but a normal Window.
“Provides the ability to write data to a process’ StdIn, and read from the StdOut - allowing communication with command line processes such as servers, compilers, script interpreters (such as PHP, Python and so on), etc.”
I’m a little confused as to what is going on.
Is Program A a command line program?
When you say
Program B creates some value and closes, and I get the value in program A.
Does that mean you get the stdout output? and is it the stdout output you want only or does that also have to go to stdout for the user to see it?
Because the way I read it the stdout thig can be removed altogether and just get the results returned