Jump to content
  • Advertisement
Sign in to follow this  
Aardvajk

Creating a window without WinMain

This topic is 4470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Driven purely by curiosity, I have been trying to create a Win32 app that starts with int main() rather than WinMain and doesn't require the Windows App switch to my compiler. Here's what I have so far:
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
bool Register(HINSTANCE HIn);

int main(int,const char **av)
{
    HMODULE HIn=GetModuleHandle(NULL);

    FreeConsole();

    HWND Hw;
    MSG Msg;
    DWORD TCount;

    if(!Register(HIn)) return 0;

    Hw=CreateWindowEx(WS_EX_TOPMOST,"MyWin","",WS_OVERLAPPEDWINDOW,
                      0,0,640,480,
                      GetDesktopWindow(),NULL,HIn,NULL);

    if(Hw==NULL) return 0;

    ShowWindow(Hw,SW_SHOW); UpdateWindow(Hw); SetFocus(Hw);

    PeekMessage(&Msg,NULL,0,0,PM_NOREMOVE);
    while(Msg.message!=WM_QUIT)
        {
        if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
            {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
            }
        }

    return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND Hw,UINT Msg,WPARAM wParam,LPARAM lParam)
{
    switch(Msg)
        {
        default: return DefWindowProc(Hw,Msg,wParam,lParam);
        }
}

bool Register(HINSTANCE HIn)
{
    WNDCLASSEX Wc;

    Wc.cbSize=sizeof(WNDCLASSEX);
    Wc.style=0;
    Wc.lpfnWndProc=WndProc;
    Wc.cbClsExtra=0;
    Wc.cbWndExtra=0;
    Wc.hInstance=HIn;
    Wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    Wc.hCursor=LoadCursor(NULL,IDC_ARROW);
    Wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    Wc.lpszMenuName=NULL;
    Wc.lpszClassName="MyWin";
    Wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);

    return RegisterClassEx(&Wc);
}

It sort of works, but if I run it from Explorer, it still creates a DOS window first, that then vanishes shortly after the Windows window appears. Is there any way to do this without that happening? I'm sort of toying with the idea of putting the window creation code in a library so my DirectX apps can just start at main() instead of having to copy paste the big old WinMain functions into each project. Ta. Paul

Share this post


Link to post
Share on other sites
Advertisement
If you are starting from a Win32 console app, you are going to get the console. You are just creating a window from within the console.

Also as long as you are running from within Windows 2000 or XP, you don't need to worry about getting the HINSTANCE value. You can just make this null.

Share this post


Link to post
Share on other sites
If you create a console subsystem application it will always create a console window when run from Explorer or otherwise run normally. Ironically, if you create a windows subsystem application it can create a console window if it wants one, but it doesn't need to.

Share this post


Link to post
Share on other sites
That's a shame. I had a feeling that might be the case.

How do things like SDL and GLUT work then? I don't know much about them but from code I've seen, it appears to me you start with int main(etc).

Share this post


Link to post
Share on other sites
EasilyConfused,

I'm not sure about those libraries, but with FLTK you can use 'int main' only if you link statically. What is happening is that they have declared a 'WinMain' in their library, which calls your main and passes the arguments to it. Its a pretty cool little trick, but it doesn't work with dynamically linked libraries. I'm not sure if thats possible, but someone else might know.

-brad

Share this post


Link to post
Share on other sites
What you want to do is possible, although not recommended.

WinMain is actually only a layer ontop of other code that is called. The function WinMainCRTStartup is actually called before WinMain, gets the instance, gets the parameters, etc. What you could do is change the entry point to the program (in the linker tab), and handle it that way. But this still gets somewhat ugly.

Creating a library which handles the initialization is probably the safer way of handling things, and just include the library, and have your main function called that way.

Share this post


Link to post
Share on other sites
@Galapaegos

Aha.

That is a neat trick but not really what I was looking for. Never mind. I suppose it was quite a lot to expect really.

Cheers for the info.

@Nytegard

I appreciate the suggestion, but I don't want to do it that badly [grin]. Looks like my idea can join the majority of the others in the bin then.

Share this post


Link to post
Share on other sites
I'm pretty sure that MinGW allows you to use main() (i.e. it will do the right thing if you use main()) instead of WinMain() for windows applications.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!