Sign in to follow this  

Overriding App Entry Point

Recommended Posts

Hello, I think the title says it all. How could I implement, lets say, a library, in which the application's entry point (main()) is overriden?. I've seen such behaviour in SDL, and in wxwidgets. I think the former uses a simply new name for main() -IIRC-, and the latest uses a class method as the entry point. Maybe I didnt clarify my question very well, so, what I meant was not how to make an entry point in the sense of, for example, creating an OO library where you instanciate a class and call a function like Library l=new Library(...); l.init(); But rather how to modify the entry point of the app itself, which would be main() in a plain c console project or WinMain() in a windowed win32 app. Is this easy to implement? Thanks /Edit I guess I was wrong about SDL, what it really does is to create a main() to replace WinMain() in win32 platform, and I guess its pretty much the same for other platforms; and this is still a "mistery" for me on how its done.

Share this post

Link to post
Share on other sites
What I've seen thus far, many libraries tackle this with some preprocessor magic, like:

#ifdef _WIN32
#define MAIN_FUNCTION int WINAPI WinMain( HINSTANCE hInst, HINSTANCE prevInst, LPCSTR cmdLine, INT nCmdShow )
#define MAIN_FUNCTION int main( int argv, char *argv[] )

clzName __myStartClass;;

Edit: The forum seems unable to handle the backslashes in the macro...

Share this post

Link to post
Share on other sites
SDL does it by re-#defining the app's main() to SDL_Main (IIRC). SDL's main() will actually be called by the OS and in turn call SDL_Main.

On Windows, there is a simpler way: just implement WinMain and call main() from there. Note that this is not strictly C++ conformant (you aren't supposed to call main() directly *).
You can go one level deeper and set the program entry point via advanced linker settings. From there, you do any initialization that needs to happen before CRT init (which includes C++ constructors being called), and then call mainCRTStartup (which is the normal entry point). In my case, that is handy for initialization of emulated POSIX routines (so "if(!is_initialized)" or singletons aren't needed).

* I suspect VC actually makes use of this; when calling main(), the stack was inexplicably shifted up 4 bytes.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this