A while ago i wrote a C++ CLI Windows Form app, which compiled fine in Visual Studio 2013. Now i wanted to recompile it in Visual Studio 2015 Update 1 but i'm facing a problem, and after hours of tests i figured out the culprit is afxwin.h
.
TL;DR - Is there any way i can use stdafx.h
(so afxwin.h
and all other imports coming with it) in a Windows Form app compiled with Visual Studio 2015 without having the app crash upon start?
Here's how to reproduce the same issues i'm facing in my app.
Since Windows Form is no longer available as project template in VS2015, i created a CLR Empty Project called Test
Ctrl-Shift-A to add a UI > Windows Form called MyForm
In MyForm.cpp i added this:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int main(cli::array<System::String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Test::MyForm form;
Application::Run(%form);
}
Under Configuration Properties > Linker > Advanced i set Entry Point to main
Under Configuration Properties > Linker > System i set SubSystem to Windows (/SUBSYSTEM/WINDOWS)
COMPILE (DEBUG CONFIGURATION): compiles with no errors/warnings
RUN: runs without any problem.
Now lets try adding afxwin.h to MyForm.cpp:
#include <afxwin.h>
Under Configuration Properties > General i set Use of MFC to Use MFC in a shared DLL
COMPILE (DEBUG CONFIGURATION): compiles with no errors/warnings
RUN: the app wont even start, it just shows Debug Assertion Failed error in expression _CrtIsValidHeapPointer(block)
Now to fix this error i found that it's necessary to remove the Entry Point, so:
Under Configuration Properties > Linker > Advanced i removed Entry Point value (which i previously set to main)
COMPILE (DEBUG CONFIGURATION): compiles with no errors/warnings
RUN: the app again wont even start, it no longer shows Debug Assertion Failed but System.AccessViolationException in an unknown module and "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
These are the errors i get in my app, and i'm wondering how is it possible that simply including afxwin.h
gives all these problems in VS2015 while it didnt in VS2013.
Is there anything i can do to fix it, without going back to VS2013?
The C runtime library was significantly rewritten for VS2015 by James McNellis. He's a big C++ fan, the new code he's written suffers from the chronic SIOF problem that's so common in a C++ program. The Static Initialization Order Fiasco was surely present in your VS2013 project as well but happened not to byte, the original CRT code was exposed to SIOF for many years so likely to behave better.
Excessively hard to debug in this case, the code that fails comes from a CRT source code file that is not included in the install named thread_safe_statics.cpp. Not 100% sure what it does given that there's no source code to look at but the name of the file leaves little to the imagination.
MFC has static state that needs to be initialized before it is usable. In particular, the program must have a static CWinApp variable that is initialized at Just the Right Time. That requires the entrypoint to be WinMain(), implemented in MFC, and an explicit declaration of a CWinApp instance in your source code. Like this:
Reset the linker's EntryPoint setting back to its default (blank) so the CRT is initialized first and MFC's WinMain function runs next. And beware I took a shortcut, you get no
args
. And I fixed a bug in your main() function, it used stack semantics incorrectly.This hack gets your program running again. Whether it is actually correct is rather doubtful. This suffers from the "Who is the Boss" syndrome that's associated with big frameworks. Don't depend on any MFC window to work correctly since it is Winforms that is dispatching messages. But you should have had that problem in VS2013 as well. "Don't do it" is the only solid advice.