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

This is a discussion on Put WinMain in DLL which then calls MyMain() in exe project within the C++ Programming forums, part of the General Programming Boards category; Hello, Let's say I have an executable and a DLL. The executable uses the DLL. What I'm trying to achieve ...

  1. #1
    Registered User Devils Child's Avatar
    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
    2,698
    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 Devils Child's Avatar
    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
    2,521
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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

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

    Soma

  6. #6
    Registered User Devils Child's Avatar
    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
    2,521
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User Devils Child's Avatar
    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
    2,698
    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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21