1

How can I completely disable all of the run-time error messages provided by Visual Studio?

I mean, not have them be included in my app at all.

I handle the errors myself in a vectored exception handler, and I do not want to have to keep calling _CrtSetReportMode.

Thanks!

orORorOR
  • 143
  • 9
  • Are you interested in turning them off only when run under Visual Studio, or outside Visual Studio as well? – R Sahu Jul 05 '20 at 21:21
  • 1
    I want to turn it off when I run it outside of VS's debugger, as that's when they pop up – orORorOR Jul 05 '20 at 21:23
  • 2
    Are you talking about the runtime checks such as the one that gives you a popup when you access an invalid vector index? If you do a release build they shouldn't be included. – eesiraed Jul 05 '20 at 21:42
  • [`_CrtSetReportMode`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreportmode?view=vs-2019) only exists in `_DEBUG` builds, and in that case it *is* how you control error reporting. – dxiv Jul 05 '20 at 22:20
  • 1
    Why not just fix the errors in your code that are causing these messages in the first place? – Ken White Jul 05 '20 at 23:00

2 Answers2

1

Old question but whatever.
"Short" answer, this post:
https://hero.handmade.network/forums/code-discussion/t/94-guide_-_how_to_avoid_c_c++_runtime_on_windows

Long answer:
I don't like how some things were done in the post (link above), but I like how this video did (he looks at this link in the video):
Watch this video (3h long, but worth it, also, haven't watched the entire video, but even when he says he is finished with removing the CRT stuff he will talk about the potential problems that you can find, or read this answer first :D): https://www.youtube.com/watch?v=sE4tUVaxiV0

I will give the steps for Visual Studio's compiler, for other compilers I don't know how it would work.
Also also, I'm Brazilian, so if the command names etc don't match exactly, just go to the closest.

First, let's do all the changes in configs that you need to do:

  1. You need to change the subsystem in the configuration of your project to your need, in case of windows it will be:

    • Linker->System->SubSystem => Windows(/SUBSYSTEM:WINDOWS)

    I don't know how it is for Linux.

  2. Set so it doesn't include additional libraries:

    • Linker->Entry->Ignore all default libraries => Yes (/NODEFAULTLIB)

    All other libraries that you need you can either link using
    #pragma comment(lib, "lib name here using the quotation marks") or you can use the field above of the "Ignore default libs".

  3. Remove all checking stuff of Visual Studio: I don't know if you need to remove all of this, but if you are like me, since you are removing the CRT, you probably don't want any type of checking or whatever. Anyway, here is my complete list of things:

    • C/C++ -> Code generation -> Enable c++ exceptions => No
    • C/C++ -> Code generation -> Basic runtime verifications => Default
    • C/C++ -> Code generation -> Security checks => /GS-
    • C/C++ -> Code generation -> Enable floating point exceptions => No (/fp:except-)

Second, your entry point; note that it's not WinMain(...); it's WinMainCRTStartup(), like this:

// No parameters
DWORD WINAPI WinMainCRTStartup()  
{  
    ...  
}

WINAPI is just a macro for __stdcall.
Technically the definition of it is using DWORD, but I normally use int.

The docs say that you should use ExitProcess(_return value_); to exit the code, I myself do that, and if you do that you don't even need to use return ~; even though some people would say it's good practice, I just think it's useless.

And last thing:
Compilers nowadays love to use the CRT, which I hate. So if you try to compile code with zero stuff in it with everything about it will work, but you may have the problem with some more stuff:
You need to define your own memset() and define a _fltused, memeset is memset, the compiler uses it for when you create big arrays and _fltused (float used, didn't know that) when you use float stuff.
To be honest, just copy this file I made, also, Visual Studio doesn't like if you do that on a header file, so do that on a .c file mine_msvc.c for example:

extern "C"
{
    // Don't actually know if you need the value, since looks like it works even without it, I myself leave it with the value.
    int _fltused = 0x9875;

#pragma warning( push )
#pragma warning( disable : 28251 )
#pragma warning( disable : 6001 )

#pragma function(memset)
    void* __cdecl memset(void* dest, int val, size_t tam)
    {
        char* retDest = (char*)dest;

        while (tam--)
            *retDest++ = (char)val;

        return retDest;
    }
}

#pragma warning( pop )

Also, remember basic C++ stuff, you can't just use this in every place, because of multiple definitions, I myself use it in my library and I have an EXTERN macro, but you can do a header file:

extern "C" 
{
    extern int _ftlused;

    void* __cdecl memset(void* dest, int val, size_t tam);
}

I would say this is all (look at the final paragraph), what I used to do to check if there was really any more stuff would be to use "Dependencies.exe", but for me at least it just got less reliable, so what you can do is to use IDA or Ghidra, I don't know how to use Ghidra and I just use the free version of IDA.
At least in "IDA" in the left panel you can see functions, everything that is called sub_~number~ is yours, and you can even use your assembly knowledge and check the functions, some other stuff may not be called like that, for example in my case I had a Windows hook called fn and a thread called StartAddress, plus the actual start. Now, if you see a lot of functions with weird names, something got linked without you asking.

I personally don't know too much how the executable is composed, but in "IDA" you can look at near the end of the file, or search for the place that says

;
; Import names for "dll name"
;

and see extra functions that your code uses, most, if not all, will be Windows obligatory functions or the API functions that you used.

And just to finish, if you use that, of course you can't use anything from the CRT, but you can't use new nor delete, you can still use STL, since they are templates, I personally don't like them and don't use them; you can't use C++ error handling, since it's part of the CRT.

At the time Im writing this, I still didn't had any problem using these options, but you can have extra problems, like stack size, thread stack sizes, etc. About that, read the link or watch the video, because for example, for the stack size check you could implement your own __chkstk, but I don't know how to do that + since nowadays 1MB is nothing, you can just use compiler options to ask for the 1MB since the start; again, just read the first link.

tripleee
  • 175,061
  • 34
  • 275
  • 318
EnderMega
  • 79
  • 10
  • Too lazy to edit again, but you can do the same for dlls; __DllMainCRTStartup, but dlls have the same arguments as the DllMain one. – EnderMega Mar 10 '23 at 15:30
0

>>How to disable CRT completely

As far as I'm concerned, in generally you couldn't disable CRT completely:

1,Entry point (gets input from console)

2,Provide common functions used in C and C++

If you want to compile your app without C-Runtime Library (CRT) ,I suggest you could try to use /MT, /NODEFAULTLIB linker options and redefine entry point at Linker -> Advanced -> Entry Point to function defined in your code.

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20