c++visual-studiowinapiconfigurationentry-point

Set custom EntryPoint for a simple application


I have this simple hello world c++ application:

#include <windows.h>
#include <iostream>

using namespace std;

void test()
{
    cout << "Hello world" << endl;
}

I want to use test as my custom entry point. So far I tried to set Linker -> Advanced -> Entrypoint to test But I got lots of lnk2001 errors. Is it possible to somehow remove any main() wmain() WinMain() and use my function with just Visual studio settings?


Solution

  • Using a custom entry point in a Windows app bypasses the entire CRT startup and global C++ initializations. Because of that, it requires not using the CRT, and turning off compiler features that depend on the CRT such as /GS buffer checks and other /RTC run-time error checks.

    The following is an example of a minimal app with the custom entry point test.

    #include <sdkDdkVer.h>
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    
    // compile with /GS- lest
    // LNK2001: unresolved external symbol @__security_check_cookie@4
    //#pragma strict_gs_check(off)
    
    // turn off /RTC*
    #pragma runtime_checks("", off)
    
    #pragma comment(linker, "/nodefaultlib /subsystem:windows /ENTRY:test")
    
    int __stdcall test(void)
    {
        OutputDebugStringA("custom /entry:test\n");
    
        ExitProcess(0);
    }
    

    More insight can be found in Raymond Chen's WinMain is just the conventional name for the Win32 process entry point.