在win32应用中使用printf代码示例

来源:互联网 发布:微盘交易平台源码 编辑:程序博客网 时间:2024/05/01 12:36

 

 #include <windows.h>#include <stdio.h>/**If the program is supposed to run in the GUI subsystem - ie. the entry function is WinMain and the compiler/linker settings indicate GUI, then my AllocConsole etc. code will do the trick. If the program is supposed to run in the Console subsystem - ie. the entry function is main and the compiler/linker settings indicate Console, there''s no need for AllocConsole etc. This includes Glut based programs as well as siaspete''s comment. In fact, the suggestion is really the easiest. Change WinMain to main and use GetModuleHandle(NULL) to get the hInstance. Then use printf statements as you want. That the entry function is main rather than WinMain doesn''t mean you can''t register a class, create a window and enter into a message loop and so on.Or you can use the code pattern as below to show messages on a console.*/int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpszCmd,int nCmd){ AllocConsole();   // Create a new console window ::freopen("CONOUT$", "w", stdout); printf("Hello World!"); Sleep(5000);  // Sleep for 5000 ms=5 s FreeConsole();  // Close the console window return 0;}