Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with console window and win32 app

Status
Not open for further replies.

venijn

Technical User
Sep 25, 2000
10
0
0
US
ok, im using gcc to create my program and im using notepad to create the source. I have a problem with my Win32 programs that everytime they are started they open a dos window then show a Windows window. the console window stays open until the program ends (the win32 window closes). Anyone have any suggestions on what to do? (yes this problem happens with even the simple hello world window app I pulled from the MS site)

I run a w98 system if that makes a difference.
 
Are you saying that you have a Win32 GUI application, and within that application, you launch a Win32 console program?

If this is the case, run your console program using the CreateProcess() API. One of its arguments is an address of a STARTUPINFO structure. In this structure, you can control the show state of the launched process.

HTH
Shyan
 
actually, what im saying is I have a win32 program and when run it launches a dos console to launch the program and i dont know why. Dont argue with stupid people, they'll just bring you down to their level and beat you with experience.
 
How do you launch your console program? Did you use CreateProcess()?

Shyan
 
Windows knows two kinds of programs: GUI and console.
If a program is marked as a console program, a console is created for it when it is started, even if the console is not used (Windows doesn't know the console isn't going to be used).
There are two exceptions on this rule:
1. A console program can inherit the console of it's parent. This means that the parent must also be a console program.
2. A console program can be started with the DETACHED_PROCESS flag, in which case a console is not created. But if you don't have control over the parent program (might be Explorer), there is no way to set it as far as I know.

So the best option is: make a GUI program of it.

How to make a GUI out of a console program:
1. include <windows.h>
2. change the entrypoint from
- int main ( int argc, char * argv[] )
to :
- int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCmdShow )
Note: You do not have the argc and argv[] arguments, only a pointer (lpCommandLine) to the command line, which is WITHOUT the first argument, the program name. If you need the program name as well, you can get a pointer to the entire command line by calling the GetCommandLine ( ) API.
You have to parse or convert the command line to argc / argv[] yourself.
3. When the linker does not recognize the entrypoint, and gives an unresolved external error on _main, add the option &quot;-subsystem:windows&quot; (without the quotes) to the link command.

In order to maintain compatibility with gcc (which I don't know), you can use (when you use Visual C in Windows) the &quot;/DWINDOWS&quot; option when compiling in Visual C. This will define WINDOWS as if there were a #define WINDOWS statement prior to compilation. Your source will contain:

Code:
....

#ifdef WINDOWS
#include <windows.h>
#endif

...

#ifdef WINDOWS

#define MAX_ARGUMENTS 100
int   argc;
char *argv[MAX_ARGUMENTS];

void ParseCommandLine ( LPSTR lpCommandLine )
{ // convert lpCommandLine to argc and argv[]
}

int WINAPI WinMain ( HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCommandLine,
                     int       nCmdShow )
{
  ParseCommandLine ( lpCommandLine );

#else

int main ( int argc, char * argv[] )
{
#endif
  // body of main
}
Marcel
 
this is the source

#include <windows.h>

LRESULT WINAPI MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

HBRUSH background;
background = CreateSolidBrush(RGB(0xFF, 0x00, 0x00));
WNDCLASS wclass;
wclass.style = CS_VREDRAW | CS_HREDRAW;
wclass.lpfnWndProc = MainWndProc;
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hInstance = hInstance;
wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wclass.hbrBackground =
background;
wclass.lpszMenuName = 0;
wclass.lpszClassName = &quot;Window&quot;;
if (!RegisterClass(&wclass)) { return 0; }
HWND hwnd;
hwnd = CreateWindow( &quot;Window&quot;,
&quot;What an ugly color of red&quot;,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if (hwnd == NULL) { return 0; }
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT WINAPI MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
};
}




It opens in a console that creates a window, i dont know why, i seem to be doing everything right :-S

This is compiled like so (assuming the source name is win.cpp)

g++ -o win.exe win.cpp -lgdi32 Dont argue with stupid people, they'll just bring you down to their level and beat you with experience.
 
ok i figured it out, for anyone that needs to know this,

-mwindows

is the same as that -subsystem:windows or whatever that was, i didn't have -mwindows added to the command line compile so it didn't want to let me do it. anyway, thanks for all your help (I eventually went and got dev-C++ to see what command line they used because i know it compiles with gcc as well.) Dont argue with stupid people, they'll just bring you down to their level and beat you with experience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top