# Using WriteConsoleA from a Clarion application

**URL:** <https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734>\
**Category:** Snippets\
**Created:** [March 16, 2018, 10:19am UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734 "2018-03-16T10:19:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![brahn](https://clarionhub.com/user_avatar/clarionhub.com/brahn/32/756_2.png) [@brahn](https://clarionhub.com/u/brahn)\
**Post date:** [March 16, 2018, 10:19am UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/1 "2018-03-16T10:19:55Z")

</div>

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 🙂

Compile the program, run it from a command line and mash the button!

> ![image](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/1X/240a9caa4d732b97fae3eff4a8ff9753538a980a.png)

```auto
  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 dwProcessId),BYTE,PASCAL,RAW
      FreeConsole(),BYTE,PROC,PASCAL,RAW
      GetStdHandle(DWORD nStdHandle),LONG,PASCAL,RAW
      WriteConsoleA( |
        HANDLE hConsoleOutput, |
        VOID lpBuffer, |
        DWORD nNumberOfCharsToWrite, |
        *LPDWORD lpNumberOfCharsWritten, |
        LPVOID lpReserved),BYTE,PASCAL,RAW,PROC
    END
WriteLine PROCEDURE(STRING pMessage)
  END

Window WINDOW('Test WriteConsole'),AT(,,101,43),GRAY,FONT('Microsoft Sans Serif',8)
            BUTTON('Write To Stdout'),AT(17,12),USE(?ButtonTest)
          END
  CODE

  Open(Window)
  
  ACCEPT
    IF Event() = EVENT:Accepted AND Accepted() = ?ButtonTest
      WriteLine('Hi!')
    END
  END
  
WriteLine PROCEDURE(STRING pMessage)
conHandle HANDLE
outLen LPDWORD
bufferStr &CSTRING
  CODE
  bufferStr &= New(CSTRING(Len(pMessage)+2))
  bufferStr = pMessage & '<10><0>'
  IF AttachConsole(ATTACH_PARENT_PROCESS)
    conHandle = GetStdHandle(STD_OUTPUT_HANDLE)
    WriteConsoleA(conHandle,Address(bufferStr),LEN(bufferStr),outLen,0)
    FreeConsole()
  END
  Dispose(bufferStr)
  RETURN

```

---

<div class="post-metadata">

**Author:** ![Graham\_Dawson](https://clarionhub.com/user_avatar/clarionhub.com/graham_dawson/32/1331_2.png) [@Graham\_Dawson](https://clarionhub.com/u/Graham_Dawson)\
**Post date:** [March 16, 2018, 8:08pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/2 "2018-03-16T20:08:44Z")

</div>

Hi Brahn,

Interesting, note you don’t need the string reference with it’s new and dispose you can just use LEN to create a cstring of the required length, as in …

```auto
WriteLine PROCEDURE(STRING pMessage)
conHandle HANDLE
outLen LPDWORD
bufferStr CSTRING(LEN(pMessage)+2)
	CODE
		  bufferStr = pMessage & '<10><0>'
		  IF AttachConsole(ATTACH_PARENT_PROCESS)
		         	conHandle = GetStdHandle(STD_OUTPUT_HANDLE)
			        WriteConsoleA(conHandle,Address(bufferStr),LEN(bufferStr),outLen,0)
			        FreeConsole()
		  END
		  RETURN

```

---

<div class="post-metadata">

**Author:** ![brahn](https://clarionhub.com/user_avatar/clarionhub.com/brahn/32/756_2.png) [@brahn](https://clarionhub.com/u/brahn)\
**Post date:** [March 17, 2018, 9:41am UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/3 "2018-03-17T09:41:12Z")

</div>

Oh neat, I forgot about that!  
I’m not sure how necessary the FreeConsole() is either but I was trying it out.

---

<div class="post-metadata">

**Author:** ![elankreijer](https://clarionhub.com/user_avatar/clarionhub.com/elankreijer/32/1466_2.png) [@elankreijer](https://clarionhub.com/u/elankreijer)\
**Post date:** [February 17, 2021, 4:01pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/4 "2021-02-17T16:01:19Z")

</div>

FreeConsole() is needed, i tried to remove it but then there was no output.

n.b. Used it in a small app writing a few lines to the console and exiting directly after. it does not return to show the command prompt again, but it’s ready and you can type a new command.

---

<div class="post-metadata">

**Author:** ![maverick](https://clarionhub.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@maverick](https://clarionhub.com/u/maverick)\
**Post date:** [September 29, 2021, 12:46pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/6 "2021-09-29T12:46:15Z")

</div>

In case anyone is interested: this example will compile just fine as-is under Clarion 11. Clarion 9.1 must have a bit of a bug.

Under Clarion 9.1:

- If you are compiling in **DLL mode** , you have to use LibMaker.exe to create a .lib file that contains just the _AttachConsole_ entry point from _C:\WINDOWS\System32\kernel32.dll_
- If you’re compiling in **LIB mode** , you _do not_ need to make this custom .lib file, or do anything special to include the _AttachConsole_ entry point lib

Thanks to [Rick Martin](https://clarionhub.com/u/rick_upperpark/summary) for helping me solve that one.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [September 29, 2021, 3:45pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/7 "2021-09-29T15:45:05Z")

</div>

There has been some maneuvering of stuff in the shipping WIN32.LIB and other libs. AttachConsole is in there now, but was not in 9.1.

---

<div class="post-metadata">

**Author:** ![mriffey](https://clarionhub.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mriffey](https://clarionhub.com/u/mriffey)\
**Post date:** [April 3, 2023, 5:59pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/8 "2023-04-03T17:59:57Z")

</div>

Used this today. Works great.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [April 3, 2023, 10:08pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/9 "2023-04-03T22:08:31Z")

</div>

Brahn did create a `ConsoleSupport Class` and an example on GitHub. IIRC on my Laptop I have an enhanced version I’ll try to find.

This is the [Console INC file](https://github.com/fushnisoft/ClarionClasses/blob/master/_classes/ConsoleSupport.inc) Class to give an idea of methods in the [CLW file](https://github.com/fushnisoft/ClarionClasses/blob/master/_classes/ConsoleSupport.clw).

```auto
ConsoleSupport Class(),Type,Module('ConsoleSupport.Clw'),LINK('ConsoleSupport.Clw',1)
! Properties
inputHandle HANDLE
outputHandle HANDLE
textBuffer CSTRING(2048)
outBuffer CSTRING(2048)
inBuffer CSTRING(2048)
bytesWritten LONG
bytesRead LONG
prefix CSTRING(21)
! Methods
Construct PROCEDURE
Destruct PROCEDURE(),VIRTUAL
Init PROCEDURE(),BYTE,PROC,VIRTUAL
WriteLine PROCEDURE(STRING pText),BYTE,PROC,VIRTUAL
ReadKey PROCEDURE(),STRING,PROC,VIRTUAL
GetLastSystemError PROCEDURE ( LONG pLastErr=0 ),STRING
              END

```

One important setting is the Project EXP file must have the First line end with [CUI not GUI](https://clarionhub.com/t/what-does-cui-mean-in-the-export-file/2100). You’ll see this in his [Console Test](https://github.com/fushnisoft/ClarionClasses/blob/master/ConsoleSupport/ConsoleTest.clw) project [EXP file](https://github.com/fushnisoft/ClarionClasses/blob/master/ConsoleSupport/ConsoleTest.exp).

```auto
NAME 'CONSOLETEST' CUI

```

> **[GitHub - fushnisoft/ClarionClasses: Some handy classes that I use.](https://github.com/fushnisoft/ClarionClasses)**
>
> Some handy classes that I use.

---

<div class="post-metadata">

**Author:** ![MarkGoldberg](https://clarionhub.com/user_avatar/clarionhub.com/markgoldberg/32/1511_2.png) [@MarkGoldberg](https://clarionhub.com/u/MarkGoldberg)\
**Post date:** [April 4, 2023, 4:29pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/10 "2023-04-04T16:29:53Z")

</div>

> [@CarlBarnes](#):
>
> [GitHub - fushnisoft/ClarionClasses: Some handy classes that I use.](https://github.com/fushnisoft/ClarionClasses)

It’s probably appropriate for someone to fork and maintain that repo given that fushnisoft was @brahnp / @brahn and he has passed away.

---

<div class="post-metadata">

**Author:** ![mriffey](https://clarionhub.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mriffey](https://clarionhub.com/u/mriffey)\
**Post date:** [February 1, 2024, 11:12pm UTC](https://clarionhub.com/t/using-writeconsolea-from-a-clarion-application/1734/12 "2024-02-01T23:12:19Z")

</div>

[GitHub - enercalc/ClarionClasses: Some handy classes that I use.](https://github.com/enercalc/ClarionClasses) I guess I did this sometime last year:)
