MFC Initinstance中DoModal()返回-1

  新建一个基于对话框的MFC应用程序,在App的Initinstance中调用对话框DoModal()来显示对话框,这是框架的内容,应用程序框架生成的全部是正常的。

      当把我对话框的资源文件提取到一个资源dll中(参见文章:http://www.cnblogs.com/MakeView660/p/6045949.html)去时,对话框无法打开了,DoModal()返回值为-1

  解决方法:把m_pMainWnd = &dlg删掉  

  原因:用该成员变量m_pMainWnd 去存储你的线程主窗口对象。当和m_pMainWnd 相关的窗口被关闭后,MFC会自动终止你的线程。如果该线程是应用程序主线程,程序也将会被终止。如果该数据成员为NULL,应用程序CWinApp对象的主窗口将用来决定什么时候去终止线程。m_pMainWnd是一个CWnd*类型的public变量。

      MSDN上的原文解释:

  CWinThread::m_pMainWnd                                
  Use this data member to store a pointer to your thread’s main window object. The Microsoft Foundation Class Library will automatically terminate your thread when the window referred to by m_pMainWnd is closed. If this thread is the primary thread for an application, the application will also be terminated. If this data member is NULL, the main window for the application’s CWinApp object will be used to determine when to terminate the thread. m_pMainWnd is a public variable of type CWnd*.
  Typically, you set this member variable when you override InitInstance. In a worker thread, the value of this data member is inherited from its parent thread.

原文地址:https://www.cnblogs.com/MakeView660/p/6049951.html