Question

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?

Was it helpful?

Solution

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);
}

OTHER TIPS

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.

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

/ENTRY (Entry-Point Symbol)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top