QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Ed Davis on March 23, 2019, 11:15:46 am

Title: Using an existing Console Window
Post by: Ed Davis 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?

Title: Re: Using an existing Console Window
Post by: Ashish 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.  
Title: Re: Using an existing Console Window
Post by: Ed Davis 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.