Thread: Put WinMain in DLL which then calls MyMain() in exe project

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    Put WinMain in DLL which then calls MyMain() in exe project

    Hello,

    Let's say I have an executable and a DLL. The executable uses the DLL. What I'm trying to achieve is to encapsulate the WinMain Method, like this:

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	// Do some other code here before Main()
    	Main();
    	return 0;
    }
    Note, I want the WinMain to be inside the DLL, but the "void Main()" to be inside the executable. The WinMain (inside the DLL) then calls the Main method inside the exe project.

    Why?
    Simply to encapsulate it and execute additional code. I know this can be done. SDL does this, but reading its code is like reading the description of Chinese noodle soup, seriously

    This is a VERY minimal code example, which demonstrates my attempt. It is not compile-able and I'm hoping that someone can tell me what I'm doing wrong.

    http://dev-ch.com/files/5629e88b-874...mymaintest.zip

    Greetings

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Note, I want the WinMain to be inside the DLL, but the "void Main()" to be inside the executable. The WinMain (inside the DLL) then calls the Main method inside the exe project.
    First you must understand that WinMain is encapsulating the call to int main().

    The function main() can not be inside your .DLL therefore WinMain can not be inside your DLL.

    Also note C/C++ is case sensitive Main() is not the same as main(). And the function main must be defined to return an int, int main().


    Jim
    Last edited by jimblumberg; 04-19-2013 at 12:46 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    Quote Originally Posted by jimblumberg View Post
    The function main() can not be inside your .DLL therefore WinMain can not be inside your DLL.
    But how does SDL encapsulate main() ? I know that SDL does this, but I just can't figure out how...

    Quote Originally Posted by jimblumberg View Post
    Also note C/C++ is case sensitive Main() is not the same as main(). And the function main must be defined to return an int, int main().
    I know. I called it like this so it will not be in conflict with the "true" main function and because I prefer upper camel case casing for function names.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Devils Child View Post
    But how does SDL encapsulate main() ? I know that SDL does this, but I just can't figure out how...
    Read this about SDL. Then, you might understand more; so, you can form a better question.
    c++ - Why SDL defines main macro? - Stack Overflow

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    With special emphasis on "very poor practice indeed" if you please.

    Soma

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    I don't think this is poor practice. It simplifies a lot to the user of the DLL.

    What do you think I should do instead?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Devils Child View Post
    I don't think this is poor practice. It simplifies a lot to the user of the DLL.

    What do you think I should do instead?
    I am guessing what you want to do is use a callback function; I have never written one. I have read about them and maybe used a few.
    NOTE: Changing the main function into a callback function is something I would considered very poor design. I would even say what SDL does with main is poor design. I would document in SDL that instead of having a main function the user needs a SDL_main function. Its the MACRO magic I consider poor design.

    c++ - Passing CALLBACK from dll to EXE - Stack Overflow

    Tim S.
    Last edited by stahta01; 04-20-2013 at 08:13 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    Quote Originally Posted by stahta01 View Post
    Its the MACRO magic I consider poor design
    That's why I don't call it "int main", but "void Main". Is this still poor design?

    Anyway, I still didn't manage to get this to work with the code you posted. What am I doing wrong?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What am I doing wrong?
    Trying to "hide" main.

    Just live with the fact that every C++ program must contain a function called main() that is in the global namespace.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinMain()
    By gadu in forum C Programming
    Replies: 3
    Last Post: 07-06-2008, 08:10 PM
  2. Which part/code of WinMain() calls WndProc()? .. Confused!
    By csonx_p in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2008, 02:47 AM
  3. Winmain()
    By anirban in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2007, 03:42 AM
  4. WinMain help!
    By dynomyte in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2006, 11:10 AM
  5. WinMain? main?
    By thanatos in forum Windows Programming
    Replies: 5
    Last Post: 05-22-2002, 04:12 AM

Tags for this Thread