1

I tried one window example, window.c in C and compiler it into window.exe by gcc on my window 7. When I run window.exe on command console, it works but the command console can NOT exit out so I can not run other command in the same console, Why ? When I try putty.exe or other window application with exe extension on command console, its command console will exit out when the program is ran or loaded. Whether the reason is there is no main() including in the window.c or not ? And other question is why the code in the window.c can be ran in self-executed mode without main() including in the program ? Please advise

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
    case WM_CLOSE:
        DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
    break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize        = sizeof(WNDCLASSEX);
wc.style         = 0;
wc.lpfnWndProc   = WndProc;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName  = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
    MessageBox(NULL, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "The title of my window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 600, 300,
    NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
    MessageBox(NULL, "Window Creation Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return 0;
}

ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
return Msg.wParam;
}
2
  • it's because it's a windows application. You neead ta console application which create a window Commented Jun 19, 2016 at 11:32
  • Could you make an example for us ? Putty.exe is console applcation , Right ? Commented Jun 19, 2016 at 11:36

1 Answer 1

1

it works but the command console can NOT exit out so I can not run other command in the same console, Why ? When I try putty.exe or other window application with exe extension on command console, its command console will exit out when the program is ran or loaded.

A Windows application launched from the console should not block further interaction with the console window. I don't really know anything about putty.exe, so let's use notepad.exe as an example. It's a standard Windows application, so if I run it from the command line, Notepad launches in a new window, and I get a C: prompt back. You are completely right in thinking that is normal.

Your code, as you've shown it here, would behave normally. Something else is wrong—possibly your compiler or linker settings. I'd suggest that you might be targeting the console subsystem, but then the linker should be complaining that you have the wrong entrypoint. (At least Microsoft's toolchain would; I'm not really sure about GCC.) That's something to check, at least. Make sure you are passing the switch -mwindows when you build.

Whether the reason is there is no main() including in the window.c or not ? And other question is why the code in the window.c can be ran in self-executed mode without main() including in the program ?

No, that has nothing to do with it. The reason there is no main() function in the code is because the WinMain() function is acting as the entry point.

Technically speaking, WinMain() is the user-defined entry point. This is the conventional name and signature for the function in Windows applications. You can think of it as being analogous to main(), but really, the main() function is still there, it is just buried in the library code. The C runtime libraries that you link when building a Windows application define the actual entry point. It performs some early initialization tasks, and then calls your user-defined entry point, WinMain().

You can see this in action if you single-step your application under a debugger, but you'll miss it if you put the breakpoint at the top of WinMain(). Although that is where your code first starts executing, as I explained, there is code that executes first before turning control over to your WinMain(). So you'll need to start stepping at the very first instruction in your EXE. In Visual Studio, you would just press F11 to Step Into. I'm not sure what debugger you're using; you'll need to check its documentation.

Or you can just not worry about this at all, because it is essentially esoterica that you never really need to understand. Just think of WinMain() as being equivalent to main() when you're writing a Windows application!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your detailed reply. There is a while loop to hold the application is continuing to run. Is the reason the console could NOT exit out ? while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); }
No. That cannot be why. That is the standard Windows message loop. All Windows applications have it, like Notepad.
So it seems the reason is more relative to gcc compiler issue with its option
stackoverflow.com/questions/597818/…, you are correct, it should compiler it with -mwindows as the attached link. After re-complier with gcc -o window.exe window.c -mwindows. it works and the command console will exit out as notepad.exe, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.