Overriding App Entry Point
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.
What I've seen thus far, many libraries tackle this with some preprocessor magic, like:
Edit: The forum seems unable to handle the backslashes in the macro...
#ifdef _WIN32#define MAIN_FUNCTION int WINAPI WinMain( HINSTANCE hInst, HINSTANCE prevInst, LPCSTR cmdLine, INT nCmdShow )#else#define MAIN_FUNCTION int main( int argv, char *argv[] )#endif#define MYLIB_MAIN_CLASS(clzName) MAIN_FUNCTION { clzName __myStartClass; __myStartClass.run(); }
Edit: The forum seems unable to handle the backslashes in the macro...
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement