Author Topic: Using an existing Console Window  (Read 2395 times)

0 Members and 1 Guest are viewing this topic.

Offline Ed Davis

  • Newbie
  • Posts: 40
    • View Profile
Using an existing Console Window
« on: March 23, 2019, 11:15:46 am »
I do most of my work from a Windows command prompt/console box.

It would be nice for certain QB64 programs that I write, if all output went to the console window that I'm working in.

Using notes I found on the Wiki, I was able to get it to work:

Code: QB64: [Select]
  1.  
  2. dim i%
  3.     _limit 10
  4.     i% = i% + 1
  5.     print STR$(i%)
  6. loop until i% > 15
  7.  
  8.  

If run while in a command prompt, all output goes to that command prompt.  Just what I wanted.

However, one problem remains: If I start the program from an icon, or via an explorer window, the window is closed after the program is finished (because of the system command).

How can I tell if my program was started from a console prompt or not, so I can selectively issue the system command?

« Last Edit: March 23, 2019, 11:23:18 am by Ed Davis »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Using an existing Console Window
« Reply #1 on: March 23, 2019, 11:50:51 am »
Simply pass a agruement to your program when running from CMD, like "yourProg.exe foo".
Then use COMMAND$ in your code to check whether it was run from console window.

Code: QB64: [Select]
  1.  
  2. DIM i%
  3.     _LIMIT 10
  4.     i% = i% + 1
  5.     PRINT STR$(i%)
  6. LOOP UNTIL i% > 15
  7.  
  8. IF COMMAND$(1) = "foo" THEN SYSTEM
  9.  
« Last Edit: March 23, 2019, 11:52:20 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Ed Davis

  • Newbie
  • Posts: 40
    • View Profile
Re: Using an existing Console Window
« Reply #2 on: March 23, 2019, 12:04:10 pm »
Simply pass a agruement to your program when running from CMD, like "yourProg.exe foo".
Then use COMMAND$ in your code to check whether it was run from console window.

I appreciate the reply, but I don't want to have to pass a parameter.

In a Windows program written in C - one that uses the GUI subsystem, e.g., WinMain() instead of main(), one can call AttachConsole(ATTACH_PARENT_PROCESS), and if the function returns nonzero, the GUI program was started from a command prompt.

However, when I try this from QB64, it always returns 0, no matter how the program was started.