【发布时间】:2016-02-23 11:04:31
【问题描述】:
前段时间,我编写了一个 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 添加一个 UI > Windows 窗体 称为 MyForm
在 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 我将 Entry Point 设置为 main
在 Configuration Properties > Linker > System 我将 SubSystem 设置为 Windows (/SUBSYSTEM/窗口)
编译(调试配置):编译时没有错误/警告
RUN:运行没有任何问题。
现在让我们尝试将 afxwin.h 添加到 MyForm.cpp:
#include <afxwin.h>
在配置属性 > 常规我将使用MFC设置为在共享DLL中使用MFC
编译(调试配置):编译时没有错误/警告
RUN:应用程序甚至无法启动,它只是在表达式 _CrtIsValidHeapPointer(block)
中显示 Debug Assertion Failed 错误现在要修复这个错误,我发现有必要删除 Entry Point,所以:
在 Configuration Properties > Linker > Advanced 我删除了 Entry Point 值(我之前设置为 主要)
编译(调试配置):编译时没有错误/警告
RUN:应用程序再次无法启动,它不再显示Debug Assertion Failed,而是显示System.AccessViolationException in an unknown module和“试图读取或写入受保护的内存。这通常表明其他内存已损坏。"
这些是我在我的应用程序中遇到的错误,我想知道简单地包含afxwin.h 怎么可能在 VS2015 中出现所有这些问题,而在 VS2013 中却没有。
在不回到 VS2013 的情况下,我可以做些什么来修复它?
【问题讨论】:
标签: winforms visual-studio-2015 c++-cli stdafx.h