# Writefile API inside same procedure

**URL:** <https://clarionhub.com/t/writefile-api-inside-same-procedure/7967>\
**Category:** questions\
**Tags:** winapi\
**Created:** [February 28, 2025, 11:02am UTC](https://clarionhub.com/t/writefile-api-inside-same-procedure/7967 "2025-02-28T11:02:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel1](https://clarionhub.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daniel1](https://clarionhub.com/u/Daniel1)\
**Post date:** [February 28, 2025, 11:02am UTC](https://clarionhub.com/t/writefile-api-inside-same-procedure/7967/1 "2025-02-28T11:02:27Z")

</div>

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:

```auto
      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')
 .

```

---

<div class="post-metadata">

**Author:** ![PaulAttryde](https://clarionhub.com/user_avatar/clarionhub.com/paulattryde/32/3519_2.png) [@PaulAttryde](https://clarionhub.com/u/PaulAttryde)\
**Post date:** [February 28, 2025, 2:00pm UTC](https://clarionhub.com/t/writefile-api-inside-same-procedure/7967/2 "2025-02-28T14:00:32Z")

</div>

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 [https://learn.microsoft.com/en-us/windows/console/input-and-output-methods](https://learn.microsoft.com/en-us/windows/console/input-and-output-methods)

---

<div class="post-metadata">

**Author:** ![Daniel1](https://clarionhub.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daniel1](https://clarionhub.com/u/Daniel1)\
**Post date:** [February 28, 2025, 2:48pm UTC](https://clarionhub.com/t/writefile-api-inside-same-procedure/7967/3 "2025-02-28T14:48:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vitesse](https://clarionhub.com/user_avatar/clarionhub.com/vitesse/32/837_2.png) [@vitesse](https://clarionhub.com/u/vitesse)\
**Post date:** [February 28, 2025, 11:38pm UTC](https://clarionhub.com/t/writefile-api-inside-same-procedure/7967/4 "2025-02-28T23:38:50Z")

</div>

> [@Daniel1](#):
>
> but I don’t know how to write result to stdOut

two thoughts:

1. have a look at this thread and use -11 for STD\_OUTPUT\_HANDLE

> [@Using WriteConsoleA from a Clarion application](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734):
>
> Today I was inspired to respond to a [StackOverflow question](https://stackoverflow.com/questions/49221612/generate-output-to-commandline) and pulled this example together from pieces I found on the newsgroup slight_smile Compile the program, run it from a command line and mash the button! PROGRAM HANDLE EQUATE(UNSIGNED) DWORD EQUATE(ULONG) BOOL EQUATE(BYTE) VOID EQUATE(LONG) LPVOID EQUATE(ULONG) LPDWORD EQUATE(ULONG) ATTACH\_PARENT\_PROCESS EQUATE(-1) STD\_OUTPUT\_HANDLE EQUATE(-11) MAP MODULE('API') AttachConsole(DWORD dw…

or

1. consider using OddJob

[https://www.capesoft.com/accessories/OddJobsp.htm](https://www.capesoft.com/accessories/OddJobsp.htm)

“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.”

---

<div class="post-metadata">

**Author:** ![seanh](https://clarionhub.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@seanh](https://clarionhub.com/u/seanh)\
**Post date:** [March 1, 2025, 12:11am UTC](https://clarionhub.com/t/writefile-api-inside-same-procedure/7967/5 "2025-03-01T00:11:38Z")

</div>

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
