Author Topic: Console window for a Windows application (C only)  (Read 1354 times)

0 Members and 1 Guest are viewing this topic.

Offline Mika

  • 28
Console window for a Windows application (C only)
Let me first describe what I need to do:
I need a debugging window that is able to show the values of vectors and matrices inside a program I'm writing. But in order to get some feedback from the program itself, I need it to be able to report something back to me, as something inevitably goes wrong, at least in my case (call it an iterative programming approach  :lol:). The next step would be to call some of the functions that I wrote earlier for testing purposes from the console.

The problem is, I'm pretty much stumped on how to do this. I can get a new window to open, but I can't get my head around over what to do next. At the moment, I plan to use only Windows API since I'm mainly familiar with C (I use Pelles C compiler). I know that there are a multitude of examples of consoles, but a lot of them use libraries whose contents I don't know and I would prefer to keep it simple so that the complexity doesn't blow out of proportions for a rather simple application.

Why C? The reason is I do mainly numerical programming and I'm most familiar with C (even more with MATLAB but that doesn't count here), but definitely not with Windows APIs or writing simple GUIs.
Relaxed movement is always more effective than forced movement.

 

Offline Tomo

  • 28
Re: Console window for a Windows application (C only)
GUI and Console are generally incompatible concepts. A GUI application does not use the Console, and vice versa.
(Not really true, but generally. You can get a console in any application using the Windows API "AllocConsole")

The Windows console is "CMD.exe", and your application does not create it, it runs inside it.
By default, Windows will launch CMD when you run a console application and then close it immediately when the application terminates. If you don't want this to happen, start CMD first and then run your application from the command line.

STDOUT goes to the console, STDERR to the Windows debugging system. You can redirect STDERR to a file or elsewhere if you want - see the MSDN for details.

Most IDEs have viewers for STDERR.

"DEBUGVIEW" may be useful, though I'm not certain if STDERR goes there as I don't use STDERR directly, but use a framework debug that compiles appropriately depending on the target.
- Note: This will show the output from all running applications! You'll want to filter a bit to see just your stuff.

If you are writing a GUI application, I strongly recommend using C++ and either Qt or wxWidgets as the C bindings for the Windows API are horrible, and you'll spend longer figuring out why the GUI doesn't work than on your program logic.
« Last Edit: February 19, 2011, 07:44:15 am by Tomo »

 

Offline Mika

  • 28
Re: Console window for a Windows application (C only)
There is a misunderstanding somewhere, the console is needed in this case for user defined macros and functions and should really be inside the GUI. But I don't mean command prompt type console here.

I can create a new window for echoing the library function's output, but I can't figure out how to pass a string and numbers from a running function to that new window.

What it comes to other languages, they are an option for later. Right now I just want to see if the library functions work and wouldn't like to compile a test function again and again for different parameters that could be set from the GUI, or from the console. Pelles C isn't that bad environment for small scale projects, just take a look at it.
Relaxed movement is always more effective than forced movement.

 

Offline Tomo

  • 28
Re: Console window for a Windows application (C only)
Oh, I see. Yes, I was barking up the wrong forest there!

In 'pure algorithm' terms, create a logging function that outputs the string to the window.
- For additional fun, it can have additional parameters for 'severity level' and perhaps 'log type'.

Call that whenever you need to display something.

In pure C you'd probably need multiple log() functions, one for each data type that you need to display (vectors might want special formatting, numbers need to convert to strings etc)

- C++ makes this much easier as you can overload.

 

Offline Mika

  • 28
Re: Console window for a Windows application (C only)
Quote
In pure C you'd probably need multiple log() functions, one for each data type that you need to display (vectors might want special formatting, numbers need to convert to strings etc)

 :lol:

It took me several minutes to understand that the log() function in that case was for logging, not for the logarithm. Boy was I wondering what does that have to do with arrays and vectors...

By the way, this starts to sound like I would need to write a dynamic string allocation routine too. As it is not possible to know how many rows or columns does an array have beforehand. In order to be able to display a dynamic vector of doubles, I need to be able to allocate a dynamic string. Hmm, interesting.

I'll come back here when I get stuck again.
Relaxed movement is always more effective than forced movement.