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?
See Question&Answers more detail:
os