• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

visual C++

Beattie

Golden Member
Any one here familiar with visual c++? I have a question...

I am working on a simple app that contains only 1 .cpp file. Now, when I build and execute it, it will open a command window that shows me all of the stdout stuff in. I just want to have it open and do the work in the "background". I dont need to see that output now that it works. How do I make it not show that command window.

Any idea how to fix that?
 
You created an app that runs under what Windows calls the CUI subsystem: console user interface. You need it to run under the GUI subsystem. There is a way to change the byte in the PE (portable executable) file to change the subsystem type from CUI to GUI, but... lets not do that.

The easiest way to do what you need is to simply recreate the project as a Windows application. If you want to do it yourself, look up WinMain in MSDN. You'll need to create the entry point for the app as WinMain and not main.

Post back if the above needs elucidation...
 
So, I made a new project and a new .cpp file where I changed the old main to...

int WinMain(hInstance, NULL, NULL, SW_SHOWNORMAL)
// as a side note, how does hInstance work? it seems that it's supposed to be a pointer from the program to an
// arg within it's own main function call. like normally you would declare a variable within the main function... when
// where does hInstance come from?

and the old return 0
to..

PostQuitMessage(0);


but now it says that it cant execute the program when I do a ctrl f5. What else do I need?
 
I don't think it's appropriate to create a GUI program without creating/registering a Window. What exactly are you doing in the "background"?
 
Originally posted by: singh
I don't think it's appropriate to create a GUI program without creating/registering a Window. What exactly are you doing in the "background"?

So, you are saying that there's no way to create a process in windows that doesnt attach to an actual GUI window? that's absurd.

It's a program that keeps track of different variables around the system. It pops us MessageBox's on certain conditions. It's really just a program that I wrote to learn the internals of some windows functions
 
So, you are saying that there's no way to create a process in windows that doesnt attach to an actual GUI window? that's absurd.


That's not what I said. I said it's not appropriate to do so. A proper GUI application should create a window (even if it is hidden and the user never sees it).


It's a program that keeps track of different variables around the system. It pops us MessageBox's on certain conditions. It's really just a program that I wrote to learn the internals of some windows functions

When the Create New Project Wizard asks you what type of Win32 Application you want, select "A typical hello world application". It will write the skeleton code for you and you will not have to worry about any problems.

 
All you need to do is #include <windows.h>, add the WinMain, and compile. A simple skeleton is as follows:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "See?", "...", 0);

return 0;
}

Then just link it with user32.lib and you're set.

Originally posted by: singh
I don't think it's appropriate to create a GUI program without creating/registering a Window. What exactly are you doing in the "background"?

I do this all the time without a CreateWindowEx(). I often do this when another application needs to call mine and give the user the perception of it being integrated. I often have to do this with antiquated systems that I can't directly integrate with.
 
wow, that worked...

now to make my app, which is more complicated, work like that. Should it compile and run just by changing the name of main?


Thanks
 
Originally posted by: Beattie
wow, that worked...

now to make my app, which is more complicated, work like that. Should it compile and run just by changing the name of main?


Thanks

I'd have to see the code before I could say unequivocally yes. So long as you're not interfacing with the console: stdin, stdout, stderr, etc., you should be ok. Of course, if you need your GUI app to interface with a console, look at AllocConsole(). You can then get the stdin/stdout/stderr of that console with GetStdHandle().
 
Back
Top