在 Win32 Application 和 Win32 Console Application 中使用 MFC

   在 Win32 Application Win32 Console Application 中使用 MFC

Virtual C++ 6.0创建的Win32 Application Win32 Console Application 中使用 MFC 是可能的,主要的困难在于绕过MFC提供的WinMain函数。下面我提供一个方法以供参考:

 

进入 Project-->Setting--> C/C++ Page,做以下修改:

 

1.    Preprocessor definitions中加入_AFXDLL,加入后的设置大概是这样的:

 

WIN32,_DEBUG / NODEBUG,[_CONSOLE],[_MBCS],_AFXDLL

 

加入的_AFXDLL是关键 ,它欺骗MFC LIB,避免连接 MFC WinMain 函数。

 

2 修改Project Options,将 /MT或者 /ML标志改为 /MD

原因是在 afxver_.h 中会检查_AFXDL, _MT, _DLL 标志是否同时设置,否则报错。尽管链接 For Multi-Threaded 版本的 Library 会损失一些性能,但是这个标志的存在并不导致编译器把 Project 编译成 DLL

 

3.  Project stdafx.h 中包含必要的头文件,或者直接从MFC AppWizard创建的stdafx.h中拷贝:

 

#define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers

      

#include <afx.h>

#include <afxwin.h>         // MFC core and standard components

#include <afxext.h>         // MFC extensions

#include <afxdtctl.h>           // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT

#include <afxcmn.h>                  // MFC support for Windows Common Controls

#endif // _AFX_NO_AFXCMN_SUPPORT

 

4.  ProjectWinMain / main中加入MFC的初始化代码,以下是_tWinMain_tmain的情况:

 

extern "C" int WINAPI

_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

       LPTSTR lpCmdLine, int nCmdShow)

{

       int nRetCode = 0;

 

       if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))

       {

              TRACE0("Fatal Error: MFC initialization failed./n");

              nRetCode = 1;

       }

       else

       {

              // Actual WinMain codes ...

 

              AfxWinTerm();

       }

 

       return nRetCode;

}

 

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])

{

       int nRetCode = 0;

 

       if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))

       {

              cerr << _T("Fatal Error: MFC initialization failed") << endl;

              nRetCode = 1;

       }

       else

       {

              // Actual main codes ...

 

              AfxWinTerm();

       }

 

       return nRetCode;

}

 

此外,在Virtual C++ 6.0创建的Win32 Dynamic-Link Library中也可以单独使用MFC,这样可以避免ProjectMFC AppWizardATL COM AppWizard添加CWinApp实例,方法简单,如下构造ProjectDllMain函数即可:

 

#include <afxdllx.h>

 

static AFX_EXTENSION_MODULE ProjectDLL     = { NULL, NULL };

 

extern "C" int APIENTRY

 

DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)

{

       // Remove this if you use lpReserved.

       UNREFERENCED_PARAMETER(lpReserved);

 

       if (dwReason == DLL_PROCESS_ATTACH)

       {

              // Extension DLL one-time initialization.

              if (!AfxInitExtensionModule(ProjectDLL, hInstance))

              {

                     TRACE0("Project.DLL initialize its extension module failed!/n");

                     return FALSE;

              }

              

              // CDynLinkLibrary’s destructor will be called in AfxTermExtensionModule.

              new CDynLinkLibrary(ProjectDLL);

       }

       else if (dwReason == DLL_PROCESS_DETACH)

       {

              TRACE0("Project.DLL terminating.../n");

              // Terminate the library before destructors are called.

              AfxTermExtensionModule(ProjectDLL);

       }

 

       return TRUE;   // ok.

}

 

 

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
MFC调用win32窗口显示调试信息,使用AllocConsole 函数(2010-11-16 15:33:25)转载标签: 调试win32mfc杂谈 分类: 编译器 AllocConsole Function 为主调进程分配一个新的控制台。 语法 C++ : BOOL WINAPI AllocConsole(void); 参数: 无 返回值:如果函数成功,返回值是非零值;如果函数失败,返回值是零值。 备注: 一个进程仅能关联一个控制台,所以该函数在主调进程已经具有控制台时将会失败。 一个进程可以使用 FreeConsole 函数来释放与之关联的控制台,之后它就可以调用该函数来创建一个新的控制台或使用 AttachConsole 函数来关联另一个控制台。 如果主调进程创建了一个子进程,则子进程也将继承这个新创建的控制台。 该函数为新的控制台初始化标准输入、输出、错误句柄等。 标准输入句柄是一个控制台输入缓冲的句柄,标准输出和标准错误句柄则是控制台屏幕缓冲的句柄。为了获得这些句柄,可以使用 GetStdHandle 函数。 该函数主要用于GUI应用程序来创建一个控制台窗口。 GUI应用程序初始化时时没有控制台的,而控制台应用程序则以控制台来初始化的。 要求 : Minimum supported client Windows 2000 Professional Minimum supported server Windows 2000 Server Header: Wincon.h (include Windows.h) Library: Kernel32.lib DLL Kernel32.dll ---------------------------------------------------------------------------------------- 虽然WIN32时代是图形界面时代,但偶尔程序还需要用到命令行模式,比如批处理,这时再搞个图形界面出来显得似乎就不那么专业了。但客户还需要在正常状态下(对于命令行模式,我认为用户是非正常状态,比如脑子进水。)使用图形界面,这决定我们不能开一个控制台工程,而需要使用MFC exe程序。 OK,理所当然的,通过条件控制,命令行下我关掉对话框界面的调用代码,再使用 cout << "Hello world!" << endl; 来向这个友好的世界打个招呼,并坚持认为这句问候应该显示在CMD那个漆黑的窗口里。 很沮丧地说,事实给我与痛击。cmd窗口里仍然漆黑一片,系统完全不理会我的友好。 邓爷爷说,改革开放好!也许,我也需要个改革。 在同事mr. zhang的指导下,我找到一组API:Console Functions!正是这组API,最终让我的友好得以正当地表达。 一。创建一个Console,AllocConsole 直接使用 AllocConsole(); 马上,若是进程内第一次调用这个函数,一个空的cmd窗口会蹦出来。需要注意,一个进程只能创建一个console,多次调用会返回FALSE;而且,这个窗口是个独立的控制台窗口。 MSDN的解释:A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. 还有段:If the calling process creates a child process, the child inherits the new console. 二。显示Hello World,WriteConsole 有了console,我们还需要获取它的句柄HANDLE,然后才能在上面显示。方法是 GetStdHandle,它会获取前面我们AllocConsole得到的cmd窗口的句柄;若未调用AllocConsole,将获取标准的输入输出窗口句柄。 MSDN的解释:The GetStdHandle function returns a handle for the standard input, standard output, or standard error device. HANDLE hdlWrite = GetStd
Title: Software Application Development: A Visual C++, MFC, and STL Tutorial Author: Bud Fox Ph.D., Tan May Ling M.Sc., Zhang Wenzu Ph.D. Length: 1216 pages Edition: 1 Language: English Publisher: Chapman and Hall/CRC Publication Date: 2012-08-08 ISBN-10: 1466511001 ISBN-13: 9781466511002 Software Application Development: A Visual C++, MFC, and STL Tutorial (Chapman & Hall/CRC Computer and Information Science Series) Software Application Development: A Visual C++, MFC, and STL Tutorial provides a detailed account of the software development process using Visual C++, MFC, and STL. It covers everything from the design to the implementation of all software modules, resulting in a demonstration application prototype which may be used to efficiently represent mathematical equations, perform interactive and intuitive model-building, and conduct control engineering experiments. All computer code is included, allowing developers to extend and reuse the software modules for their own project work. The book’s tutorial-like approach empowers students and practitioners with the knowledge and skills required to perform disciplined, quality, real-world software engineering. Table of Contents Chapter 1 - Object-Oriented Analysis and Design Chapter 2 - Initial Graphical User Interface Chapter 3 - Constructing Blocks Chapter 4 - Constructing Block Ports Chapter 5 - Constructing Connections Chapter 6 - Moving Blocks and Connections Chapter 7 - Automatic Block Placement Chapter 8 - Connection-Based Bend Points Chapter 9 - Block Dialog Windows Chapter 10 - Conversion of String Input to Double Data Chapter 11 - Moving Multiple Items Chapter 12 - Addition of a Tree View Control Chapter 13 - Review of Menu and Toolbar-Based Functionality: Part I Chapter 14 - Context Menu Extension Chapter 15 - Setting Port Properties Chapter 16 - Key-Based Item Movement Chapter 17 - Reversing Block Direction Chapter 18 - Model Validation Chapter 19 - Non-Feedback-Based Signal Propagation Chapter 20 - Graph Drawing Chapter 21 - Block Operations Chapter 22 - Preparation for Feedback-Based Signal Propagation Chapter 23 - Feedback-Based Signal Propagation Chapter 24 - Placing an Edit Box on a Toolbar Chapter 25 - Serialization Chapter 26 - Review of Menu and Toolbar-Based Functionality: Part II Chapter 27 - Printing and Print Preview Chapter 28 - Implementing a Scroll View Chapter 29 - Edit Menu Chapter 30 - Annotations Chapter 31 - Tools Menu Chapter 32 - Help Menu Chapter 33 - Finalizing the Project Chapter 34 - Conclusion Appendix A: ControlEng: Win32 Console Application Appendix B: Constructing Connections: An Exploration Appendix C: NodeArcConnectivity: Win32 Console Application Appendix D: Debugging: An Introduction Appendix E: MatrixInversion: Win32 Console Application Appendix F: Using DiagramEng

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

In355Hz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值