【问题标题】:Adding MFC to existing Win32 C++ Causing Errors/Crashes将 MFC 添加到现有 Win32 C++ 导致错误/崩溃
【发布时间】:2014-05-03 15:55:14
【问题描述】:

我真的很想使用 MFC 的一些功能,但是开始工作很痛苦。

我最初想做的只是添加两个旋转控件和它们各自的编辑控件。一旦我实现了他们适当的方法来设置 Range,我发现我需要使用 MFC。

所以 VS 抱怨没有 MFC。所以我去项目属性并添加使用 MFC 共享 DLL。运行它,崩溃! Win32Project1.exe 中 0x5964D8D2 (mfc120ud.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0x00000000

所以我尝试了静态,错误!很多链接器错误,要列出很多。

所以我回到了共享。错误发生在这个区域。

       /////////////////////////////////////////////////////////////////////////////
      // export WinMain to force linkage to this module
      extern int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 _In_ LPTSTR lpCmdLine, int nCmdShow);

      extern "C" int WINAPI
      _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow)
     #pragma warning(suppress: 4985)
     {
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
     } 

最后一个箭头 }

Locals 用 +
将 hInstance 的值显示为红色 hInstance 0x000d0000 {Win32Project1.exe!_IMAGE_DOS_HEADER ImageBase} {unused=9460301 } HINSTANCE *

在 lpCmdLine + lpCmdLine 0x00831f8c L"" wchar_t *

这超出了我的调试专业知识,坦率地说,我会考虑另一种不使用 MFC 的旋转框的替代方案,但我似乎越来越需要 MFC,因为我想包含更多功能,所以它让 MFC 工作会很好,但它似乎也更脆弱。也许对像我这样的粗鲁程序员很敏感。

我想知道#includes 是否可能是导致此错误的原因?无论是订单?还是缺少?

这是我目前在 stdafx.h 中的内容

#pragma once
#pragma comment ( lib, "user32.lib" ) 
#pragma comment ( lib, "comctl32.lib" ) 
#pragma comment ( lib, "winmm.lib")//to play audio
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#include "targetver.h"
//#include <WinSDKVer.h>
//#include <afxwin.h>
#include <afxcmn.h>//for spinControl
#include <Afx.h>
#include <stdio.h>
#include <tchar.h>
#include "iostream"
#include "string.h"
#include <math.h>
//#include <ctime>//more time related stuff
#include <fstream>//for file io
#include <thread>//for threads
//#include <chrono> //for time related stuff
//#include <windows.h>
#include <Mmsystem.h>//to play audio
#include <commctrl.h>
#include <atlstr.h>//for some type of string
#include <io.h>
#include <fcntl.h>
#include <commctrl.h>  //For button sytles, maybe other styles

【问题讨论】:

    标签: c++ mfc linker visual-studio-2013 access-violation


    【解决方案1】:

    这永远行不通。删除你的 WinMain。

    您的应用程序需要一个 CWinApp 对象。有了这个 CWinApp 对象,你就是一个 MFC 特定的 WinMain 入口点。

    MFC 在指向您的 CWinApp 对象的内部单例上进行中继。没有它,几乎所有东西都会失败,并且会抛出 ASSERT。

    【讨论】:

      【解决方案2】:

      我建议您创建一个示例 MFC 应用程序,然后将 MFC 代码从示例应用程序移动到您的 Win32 应用程序。如果 Win32 应用程序较小,您可以将 Win32 代码移至 MFC 应用程序。

      通常,您将使用示例应用程序中的 CWinApp 类而不是 WinMain。

      如果您不打算使用 MFC UI 类并且只希望使用一些支持类,例如 CString 等,那么您可以创建一个支持 MFC 的示例控制台应用程序,它将告诉您如何在控制台应用程序中使用 CString。控制台应用程序将让您深入了解如何在 Win32 项目中添加所需的标头。

      【讨论】:

      • 会不会和代码冲突? MFC 的代码不是和标准的 Win32 不同吗? Win32也能做MFC能做的一切吗?我认为它更难实施?
      • 是的,会有冲突。您将对 MFC 有很好的了解以解决冲突。这不是直截了当的。我不确定您的 Win32 应用程序中有多少代码。所以冲突将取决于此。
      【解决方案3】:

      正如前面的答案所提到的,但我想我会澄清一下 - 尝试在您的代码中添加一个 CWinApp(或 CWinAppEx)对象来定义 WinMain,例如:

      class MyApp : public CWinApp
      {
      public:
          virtual BOOL InitInstance()
          {
              // Add initialisation code here e.g. show a dialog or main window, etc.
      
              return TRUE; // enter the message loop;
                           // could return FALSE to just exit the application if for example the
                           // dialog response is all you need to then quit. 
          }
      };
      
      // And somewhere in your code be sure to instantiate the object so that it can be linked
      MyApp theApp;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        相关资源
        最近更新 更多