2014-05-03 72 views
-1

我真的想使用MFC的一些功能,但它的工作很痛苦。將MFC添加到現有的Win32 C++導致錯誤/崩潰

我最初想要做的就是添加兩個自旋控制和它們各自的編輯控制。一旦我實現了適當的方法來設置範圍,我發現我需要使用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); 
    } 

與在最後

當地人的箭頭示出了}的hInstance的值作爲紅色+
的hInstance 0x000d0000 {Win32Project1.exe!_IMAGE_DOS_HEADER 基址} {未使用= 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 

回答

0

到目前爲止,這不能正常工作。刪除你的WinMain。

您需要一個適用於您的應用程序的CWinApp對象。與此CWinApp對象是一個MFC特定的WinMain入口點。

MFC繼承一個指向您的CWinApp對象的內部單例。沒有它,幾乎所有的東西都會失敗,並且可能會拋出ASSERT。

0

我建議您創建一個示例MFC應用程序,然後將MFC代碼從示例應用程序移動到您的Win32應用程序。如果Win32應用程序較小,則可以改爲將Win32代碼移動到MFC應用程序。

通常,而不是WinMain,您可以使用示例應用程序中的CWinApp類。

如果你不打算使用MFC UI類&只希望使用一些支持類如CString等,那麼你可以創建一個示例控制檯應用程序與MFC的支持,它會告訴你如何在控制檯應用程序中使用CString。控制檯應用程序將爲您提供有關如何在Win32項目中添加所需標題的信息。

+0

難道會有與代碼衝突? MFC的代碼不同於標準的Win32嗎?也可以Win32做一切MFC可以?我認爲它更難實施? – Nonlin

+0

是的,會有衝突。您將對MFC有很好的理解以解決衝突。這不是直截了當的。我不確定你的Win32應用程序中有多少代碼。所以衝突將取決於此。 –

0

由於以前的答案已經提到,但我想我會澄清 - 嘗試在你的代碼來定義的WinMain添加CWinApp(或CWinAppEx)對象,例如:

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;