4

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?

dxiv
  • 16,984
  • 2
  • 27
  • 49
0_o
  • 570
  • 6
  • 18
  • 5
    If you want to use custom entry point then you need to either stop using standard library facilities or provide CRT startup routines. – user7860670 Nov 30 '20 at 08:47
  • What is `Linker -> Advanced -> Entrypoint`? – 0___________ Nov 30 '20 at 08:53
  • @P__JsupportswomeninPoland This is in Project Properties. Right-click on your project and select Propertes then ... – 0_o Nov 30 '20 at 08:56
  • @P__JsupportswomeninPoland Note that there is [tag:visual-studio] tag – Yksisarvinen Nov 30 '20 at 09:03
  • @user7860670 I tried to change my code to C like: `#include void test() { printf("Hello\n"); }` but millions of E0147 and E0020 error occured – 0_o Nov 30 '20 at 09:06
  • 1
    `printf` is a part of CRT and still requires all that runtime stuff that is usually getting into executable. – user7860670 Nov 30 '20 at 09:17
  • The best option is to simply use the standard CRT normally, and let `main()` call `test()`, eg: `int main() { test(); }` – Remy Lebeau Nov 30 '20 at 21:22
  • Does this answer your question? [Redifining C/C++ entry point in Microsoft Visual Studio 2015](https://stackoverflow.com/questions/40044353/redifining-c-c-entry-point-in-microsoft-visual-studio-2015) – Zeus Dec 01 '20 at 01:36
  • 1
    @ZhuSong-MSFT That question is similar, indeed, but the (only) answer is unsatisfactory. As pointed out in Hans Passant's [comment](https://stackoverflow.com/questions/40044353/redifining-c-c-entry-point-in-microsoft-visual-studio-2015#comment67368272_40045581), linking the CRT while bypassing the CRT initialization is downright dangerous. My answer here is more limited in scope (no CRT, period) but it's essentially safe. Linking the CRT *and* properly initializing it would also be technically possible, though a lot more laborious and would have to rely on CRT implementation internals. – dxiv Dec 01 '20 at 07:27

1 Answers1

4

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.

dxiv
  • 16,984
  • 2
  • 27
  • 49