Adding MFC to existing Win32 C++ Causing Errors/Crashes

-1

I really want to use some features from MFC but its been a pain to get working.

All I originally wanted to do was add two Spin Control and their respective Edit Controls. Once I implemented their appropriate methods to set Range, I found out I needed to use MFC.

So VS complain no MFC. So I go to project properties and add Use MFC shared DLL. Run it, Crash! Unhandled exception at 0x5964D8D2 (mfc120ud.dll) in Win32Project1.exe: 0xC0000005: Access violation reading location 0x00000000

So I tried Static, ERRORS! Lots of linker errors, to many to list.

So I went back to Shared. The error occurs right in this area.

       /////////////////////////////////////////////////////////////////////////////
      // 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);
     } 

with an arrow at the last }

Locals shows the value of hInstance as red with +
hInstance 0x000d0000 {Win32Project1.exe!_IMAGE_DOS_HEADER ImageBase} {unused=9460301 } HINSTANCE *

and at lpCmdLine + lpCmdLine 0x00831f8c L"" wchar_t *

This is beyond my expertise of debugging and quite frankely would consider another alternative to the spin box that doesn't use MFC, but it seems like I'm need MFC more and more as I want to include more functionality, so it would be nice to get MFC working, but it also seems a lot more fragile. Perhaps to sensitive for a crude programmer such as myself.

I'm wondering if #includes could be a cause of this error? Either the order? Or lack thereof?

Here is what I have so far in 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
asked on Stack Overflow May 3, 2014 by Nonlin

3 Answers

0

This can never work. Remove your WinMain.

You Need a CWinApp object for your application. And with this CWinApp Object thee is a MFC specific WinMain entry point.

The MFC relays on an internal singleton that points to your CWinApp object. Without it nearly everything can fail, and can throw ASSERTs.

answered on Stack Overflow May 4, 2014 by xMRi
0

I would suggest that you create a sample MFC app, then move the MFC code from the sample app to your Win32 app. If the Win32 app is smaller, you can instead move the Win32 code to the MFC app.

Typically, instead of WinMain, you would use CWinApp class from the sample app.

If you do not intend to use the MFC UI classes & only wish to use some supporting classes like CString etc, then you can create a sample console app with MFC support which will tell you how to use CString in console app. The console app will give you an insight on how to add the required headers in your Win32 project.

answered on Stack Overflow May 4, 2014 by Gautam Jain
0

As the previous answers have mentioned, but I thought I'd clarify - try adding a CWinApp (or CWinAppEx) object in your code to define the WinMain, for example:

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;
answered on Stack Overflow Mar 16, 2016 by derke

User contributions licensed under CC BY-SA 3.0