如何在Windows Application中应用printf函数?
来源:互联网 发布:java显式和隐式的区别 编辑:程序博客网 时间:2024/06/10 16:41
下面,我们先看一个最简单的Windows应用程序:
#include <windows.h>int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){MessageBox(0, "调用了WinMain函数", "测试:", 0);return 0;}那么,如何在其中用printf函数呢?试看看下面的程序:
#include <windows.h>#include <stdio.h>int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){printf("调用了WinMain函数\n");MessageBox(0, "调用了WinMain函数", "测试:", 0);return 0;}这个程序达不到效果,为什么呢?因为根本就没有那个黑色的控制台,printf函数怎么能起作用呢?我们先来看看下面这个程序:
#include <windows.h>#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){return 0;}该程序运行后,能弹出一个黑色的控制台,所以在Windows应用程序中,可以通过下面的方式来使用printf函数:
#include <windows.h>#include <stdio.h>#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){printf("调用了WinMain函数\n");MessageBox(0, "调用了WinMain函数", "测试:", 0);return 0;}
这样,就可以了。为什么要在Windows应用程序中使用printf函数呢?便于理解程序和调试呗!
我们看到,在Windows应用程序中,不能直接使用printf, 一般需要加上#pragma那一句,但是,还有没有别的方法来看程序的结果呢?有的,虽然不能直接用printf, 但是可以用fprintf啊,这样,结果就不在控制台上,而是在文件中,同样可以方便地理解和调试程序。不得不说:在这里,printf比fprintf方便多了,也好多了。
- 如何在Windows Application中应用printf函数?
- 如何在printf中换行
- stm32中如何使用printf()函数!
- 在STM32中添加printf、scanf函数
- 在 Freescale CodeWarrior 中使用 printf( ) 函数
- Windows编程(4)-在Windows中使用printf
- STM32编译器keil中中printf函数的应用
- 如何在Windows Forms应用中使用flash
- 如何在windows运行时应用中拍照
- 如何实现printf函数
- 在Application中如何关闭Frame
- 在servlet中如何获取application实例
- 如何在win32 Application 中使用MFC
- 在ubuntu中如何管理startup application
- 如何在Application中进行异常处理
- 如何在windows批处理中添加自定义函数
- 在win32应用中使用printf代码示例
- 如何在单片机上使用printf函数(printf)(avr)(stm)(lpc)(单片机)
- 根据表名 生成Create创建脚本
- Linux 启动时的软盘参数表(linux 2.4.22 bootsec.ts)
- Servlet中Web.xml的配置详解
- uva 10714 Ants
- android Intent传递对象和集合
- 如何在Windows Application中应用printf函数?
- input file的ui定制
- UIPickerView实现省 市 区三级联动
- WdatePicker日历控件使用方法
- Java笔记4 JavaAPI<4>集合(Arrays)、Collections
- Mybatis
- asp.net在Linux下的开发与运行实践
- 情景模式(Tab切换组件)
- 项目经理如何提升产品意识