7

WinMain is a function that 'replaces' the default main entry point 'main'.

The user can then define its main entry point like

int WINAPI WinMain(...) { }


How is this kind of encapsulation done?

Well, most likely, at some point it looks like this:

int main() // This must be defined somewhere in windows.h
{
    return WinMain(...);
}

Question: How can I accomplish such an encapsulation of my own, which then calls WinMain? Note: The library which I made is a DLL, so it will look like this:

// This is the minimal code for the application which uses 'MyLibrary'
#pragma comment(lib, "MyLibrary.lib")
include "MyLibrary.h"

void Main(MyCustomParameter *params)
{
    // Write user code here
}

The problem however is, that the DLL doesn't 'know' the Main() function and therefore throws an 'unresolved external symbol' compile error. So how can I encapsulate it like this?

bytecode77
  • 14,163
  • 30
  • 110
  • 141

3 Answers3

5

You have to decide on a signature of your custom main function and declare it as "extern" (extern "C" in case of C++). Then, application code will have to define that function and link against your static library that has the actual _main entry point. For example:

extern "C" int my_main(int argc, char *argv[]);

int main(int argc, char *argv[])
{
    return my_main(argc, argv);
}
  • 1
    I still get an unresolved external, even with `extern "C"`. Is it because I use WinMain as inner entry point instead of main like you do? – bytecode77 Mar 28 '13 at 19:17
  • @DevilsChild: It is hard to tell what you do. If your entry point is `WinMain` — call your own main from `WinMain`. If it is just `main()` — call it from `main()`. Get your code to compile w/o changes first, then make the change and see what breaks. Or at least put a minimal complete example demonstrating your problem... otherwise it is hard to debug what you are doing. –  Mar 28 '13 at 19:20
  • Apparently, it is a problem with the include structure. I put some very minimalistic code together here: http://dev-ch.com/files/a8dce463-65a2-54fe-ef43-c9d4160dea95/mylibrarywithcustommain.zip When I don't include 'ExampleClass.h' it works. – bytecode77 Mar 28 '13 at 19:32
  • @DevilsChild: there is no need to include "MyLibrary.h" from "main.cpp", and you forgot to define `Main()` as `extern "C"`. –  Mar 28 '13 at 19:36
  • I don't understand. I did put `extern "C" void Main();` in `MyLibrary.h` and I thought I have to include `MyLibrary.h` in `main.cpp`? – bytecode77 Mar 28 '13 at 19:40
  • Also, when you remove the two lines with `include "ExampleClass.h"` it works fine. So the problem has to do with these include files. – bytecode77 Mar 28 '13 at 19:42
  • Ok, so I found out I can solve this problem by including the "include everything" header by another header which defines WinMain. Thanks for your answer, it wasl helpful :) – bytecode77 Mar 28 '13 at 22:23
4

Actually, the real entry point is neither main nor WinMain. The real entry point is one of wWinMainCRTStartup, WinMainCRTStartup, wmainCRTStartup, and mainCRTStartup. But they're not defined in Windows.h, they're part of the CRT. You can see their code in <VS installation folder>\VC\crt\src\crtexe.c. They each do some initialization and then call one of wWinMain, WinMain, wmain, and main, respectively.

As mentioned by someone else you can override the entry point with the /ENTRY switch, but you still can't have custom parameters, which is the whole reason you want to do this in the first place.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • 1
    I knew about the MainCRTStartup thing, but I indeed can use custom parameters. My attempt is here: http://dev-ch.com/files/a8dce463-65a2-54fe-ef43-c9d4160dea95/mylibrarywithcustommain.zip It doesn't work because of some unknown include problems, but when you remove the `ExampleClass`es, you will see that it works. The remaining problem now is the include structure which doesn't work yet. – bytecode77 Mar 28 '13 at 20:13
3

The linker default entry point name is "main". You can override the default to start with any function you want.

/ENTRY (Entry-Point Symbol)

bytecode77
  • 14,163
  • 30
  • 110
  • 141
brian beuning
  • 2,836
  • 18
  • 22