0

I have a programm thats run on commandline. I call it like: myexe.exe parameter. Now I want to output a message to my commandline (eg. success or error)

I tried:

   return('there is an error')   --> noting is displayed
   Halt(0,'there is an error')   --> a Windows-message is displayed

how can I output a message or perhaps some other data?

ratmalwer
  • 700
  • 5
  • 14

1 Answers1

1

Here is an example that uses AttachConsole, GetStdHandle and WriteConsoleA and in particular, does it from a Clarion GUI apptype rather than CUI like in a different example.

  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
brahnp
  • 2,276
  • 1
  • 17
  • 22
  • Hi branp this is exactly what I was looking for. Now compiling this script I get the linkerror attachConsole is unresolved. what do I miss? I tried it in my App and as plain clw in C5. – ratmalwer Mar 19 '18 at 15:03
  • Oh, for C5 you might need to create a LIB file containing the linking info for the WinApi dlls. In more recent versions of Clarion this is included in shipping WIN32.LIB – brahnp Mar 19 '18 at 23:04
  • I have a WIN32.LIB (perhaps from C6) but it looks as if Attachconsole is not included. I found the Libmaker too. But I have no Idea where to find the needed WinAPI dlls. Any hint or can someone provide a WIN32.LIB containing what I need? – ratmalwer Mar 22 '18 at 14:49
  • I think you just point libmaker at %windir%\System32\kernel32.dll You lib can be called whatever you want, just include the ones you need and make sure to include the lib in your project and you should be sorted – brahnp Mar 23 '18 at 06:17