不久前,我编写了一个 C++ CLI Windows Form 应用程序,该应用程序在 Visual Studio 2013 中编译得很好。现在我想在 Visual Studio 2015 Update 1 中重新编译它,但我遇到了一个问题,经过数小时的测试,我找到了罪魁祸首是afxwin.h
。
TL;DR - 有什么方法可以在使用 Visual Studio 2015 编译的 Windows 窗体应用程序中使用stdafx.h
(afxwin.h
以及所有其他导入),而不会在启动时应用程序崩溃?
以下是如何重现我在我的应用程序中遇到的相同问题。
由于 Windows 窗体在 VS2015 中不再可用作项目模板,因此我创建了一个名为Test的CLR 空项目
Ctrl-Shift-A 添加名为MyForm的UI > Windows 窗体
在MyForm.cpp我添加了这个:
#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);
}
在Configuration Properties > Linker > Advanced下,我将入口点设置为main
在Configuration Properties > Linker > System我将SubSystem设置为Windows (/SUBSYSTEM/WINDOWS)
编译(调试配置):编译时没有错误/警告
RUN:运行没有任何问题。
现在让我们尝试将afxwin.h添加到MyForm.cpp:
#include <afxwin.h>
在配置属性>常规下,我将使用 MFC设置为在共享 DLL中使用 MFC
编译(调试配置):编译时没有错误/警告
RUN:应用程序甚至无法启动,它只是在表达式_CrtIsValidHeapPointer(block)中显示Debug Assertion Failed错误
现在要修复这个错误,我发现有必要删除Entry Point,所以:
在Configuration Properties > Linker > Advanced我删除了入口点值(我之前设置为main)
编译(调试配置):编译时没有错误/警告
RUN:应用程序甚至无法启动,它不再显示Debug Assertion Failed但System.AccessViolationException in an unknown module和“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”
这些是我在我的应用程序中遇到的错误,我想知道如何简单地包含
afxwin.h
在 VS2015 中给出所有这些问题,而在 VS2013 中却没有。
我有什么办法可以解决它,而不回到 VS2013?