user12363684
user12363684

Reputation:

What is invoke_main and mainCRTStartup?

I have a CUI based and GUI based application that is written with CPP language. GUI based application has the following code:

#include <Windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    const TCHAR* title = TEXT("پیام");
    const TCHAR* text = TEXT("سلام، من یک پنجره هستم.");

    MessageBox(NULL, text, title, MB_OKCANCEL | MB_ICONINFORMATION);


    return 0;
}

and CUI based program has the following code:

#include <Windows.h>
#include <iostream>

int main(int argc, const char* argv)
{
    const char* cMessage = "Native Windows Development.\n";
    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), cMessage, strlen(cMessage), NULL, NULL);

    return 0;
}

However, when I debug these programs in WinDBG, I have seen before main and WinMain function, a lot of other functions like mainCRTStartup and invoke_main for CUI based app and WinMainCRTStartup and invoke_main for GUI based app have called.

What are these functions and what they did before executing the main? and also how can I get more information about these two function?

Upvotes: 1

Views: 2169

Answers (1)

Drake Wu
Drake Wu

Reputation: 7190

With VS tool, we could see the call stack below: enter image description here

And if you double-click on each call of the stack, you could see what it did in the code(as comments, the source code was located at ..\VC\Tools\MSVC\14.24.28314\crt\src\vcruntime).

First wWinMainCRTStartup is just to call function __scrt_common_main:

extern "C" int wWinMainCRTStartup()
{
    return __scrt_common_main();
}

In __scrt_common_main, the comments contains the detals:

// This is the common main implementation to which all of the CRT main functions
// delegate (for executables; DLLs are handled separately).
static __forceinline int __cdecl __scrt_common_main()
{
    // The /GS security cookie must be initialized before any exception handling
    // targeting the current image is registered.  No function using exception
    // handling can be called in the current image until after this call:
    __security_init_cookie();

    return __scrt_common_main_seh();
}

The function is the common main implementation to which all of the CRT main functions delegate (for executables; DLLs are handled separately) and __security_init_cookie is to initialize /GS security cookie before any exception handling for current image.

Then, it was the __scrt_common_main_seh: initialize crt, acquire startup lock, check the current native startup state, release startup lock, invoke the dynamically initialized __declspec(thread) variables, register the callback function for thread-local destructors.

After complete the Initialization, call invoke_main, as the name, it invoke main/wWinMain.

static int __cdecl invoke_main()
{
    return wWinMain(
        reinterpret_cast<HINSTANCE>(&__ImageBase),
        nullptr,
        _get_wide_winmain_command_line(),
        __scrt_get_show_window_mode());
}

Upvotes: 1

Related Questions