En d’autres termes, d’un projet win32 vierge (pas d’assistant).
C’est là où je suis:
Définitions du préprocesseur: WIN32
Éditeur de liens-> Système-> Sous-système = Console
int _tmain() { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); return nRetCode = 1; } MyWinApp* app = new MyWinApp(); app->InitApplication(); app->InitInstance(); app->Run(); AfxWinTerm(); return 0; } class MyWinApp: public CWinApp { public: BOOL InitInstance(); int Run(); }; BOOL MyWinApp::InitInstance() { return TRUE; } int MyWinApp::Run() { return CWinThread::Run(); }
Ignorer CWinApp :: Run () car il recherche une fenêtre principale.
Dans CWinThread :: Run (), cependant, ASSERT_VALID échoue. En haut de quickwatch, MyWinApp est invalide.
Dois-je créer MyWinApp d’une autre manière?
Vous échouez probablement parce que vous créez l’application CWinApp
après avoir appelé AfxWinInit
. Dans une application MFC normale, CWinApp
est une variable globale, construite avant l’ CWinApp
main
. De cette façon, lorsque MFC est initialisé, il dispose d’une CWinApp
globale valide. Essayer:
MyWinApp* app = new MyWinApp(); // ^moved up^ // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); return nRetCode = 1; }