你是不是觉得你写的控制台程序的字太单调了,你是不是有时候需提醒用户注意你的某行输出。呵呵感觉这两句像广告,好了言归正传,这两天写了一个可以打印红绿蓝三种颜色的printf函数并做成了动态连接库。代码说明一切。
#ifndef DllDemo_EXPORTS
#define DllAPI __declspec(dllexport)
//#else
//这个两句将在隐式的使用dll的时候导致不能连接
//#define DllAPI __declspec(dllimport)
extern "C" //原样编译
{
DllAPI int __stdcall ErrPrintf(TCHAR *format, ...) ; //__stdcall使非C/C++语言内能够调用API
DllAPI int __stdcall GreenPrintf(TCHAR *format, ...) ;
DllAPI int __stdcall BluePrintf(TCHAR *format, ...) ;
DllAPI int __stdcall RedPrintf(TCHAR *format, ...);
}
#endif
// PrintfColorDll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "PrintfColorDll.h"
#include "stdafx.h"
#include "stdio.h"
#include "tchar.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
//错误
DllAPI int __stdcall ErrPrintf(TCHAR *format, ...)
{
HANDLE hStdout = NULL ;
TCHAR szBuffer[1024] ;
va_list arg_list ;
int nBuf = 0 ;
va_start(arg_list,format);
nBuf = _vsntprintf(szBuffer,1024,format,arg_list) ;
va_end(arg_list) ;
//get console ouput handle
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(!hStdout)
{
return 0;
}
//set red colour
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_INTENSITY))
{
return 0;
}
//print
nBuf = _tprintf(_T("%s"),szBuffer) ;
//restore to white.
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN))
{
return 0;
}
return nBuf ;
}
//红色
DllAPI int __stdcall RedPrintf(TCHAR *format, ...)
{
HANDLE hStdout = NULL ;
TCHAR szBuffer[1024] ;
va_list arg_list ;
int nBuf = 0 ;
va_start(arg_list,format);
nBuf = _vsntprintf(szBuffer,1024,format,arg_list) ;
va_end(arg_list) ;
//get console ouput handle
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(!hStdout) {
return 0;
}
//set red colour
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED |
FOREGROUND_INTENSITY)) {
return 0;
}
//print
nBuf = _tprintf(_T("%s"),szBuffer) ;
//restore to white.
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED |
FOREGROUND_BLUE |
FOREGROUND_GREEN)) {
return 0;
}
return nBuf ;
}
//绿色
DllAPI int __stdcall GreenPrintf(TCHAR *format, ...)
{
HANDLE hStdout = NULL ;
TCHAR szBuffer[1024] ;
va_list arg_list ;
int nBuf = 0 ;
va_start(arg_list,format);
nBuf = _vsntprintf(szBuffer,1024,format,arg_list) ;
va_end(arg_list) ;
//get console ouput handle
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(!hStdout) {
return 0;
}
//set Green colour
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN |
FOREGROUND_INTENSITY)) {
return 0;
}
//print
nBuf = _tprintf(_T("%s"),szBuffer) ;
//restore to white.
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED |
FOREGROUND_BLUE |
FOREGROUND_GREEN)) {
return 0;
}
return nBuf ;
}
//蓝色
DllAPI int __stdcall BluePrintf(TCHAR *format, ...)
{
HANDLE hStdout = NULL ;
TCHAR szBuffer[1024] ;
va_list arg_list ;
int nBuf = 0 ;
va_start(arg_list,format);
nBuf = _vsntprintf(szBuffer,1024,format,arg_list) ;
va_end(arg_list) ;
//get console ouput handle
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(!hStdout) {
return 0;
}
//set Blue colour
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE |
FOREGROUND_INTENSITY)) {
return 0;
}
//print
nBuf = _tprintf(_T("%s"),szBuffer) ;
//restore to white.
if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED |
FOREGROUND_BLUE |
FOREGROUND_GREEN)) {
return 0;
}
return nBuf ;
}
#include "stdio.h"
#include <tchar.h>
#pragma comment(lib,"PrintfColorDll.lib") //在编辑器link时,链接到.lib文件
extern "C"
{
__declspec(dllimport) int __stdcall ErrPrintf(TCHAR *format,...);
__declspec(dllimport) int RedPrintf(TCHAR *format,...);
__declspec(dllimport) int GreenPrintf(TCHAR *format,...);
__declspec(dllimport) int BluePrintf(TCHAR *format,...);
}
//#include "iostream"
//using namespace std;
int main()
{
int inta=1;
float floata=5.0f;
char chara='a';
ErrPrintf("this is error test!/n");
GreenPrintf("this is a green test!/n");
BluePrintf("this is a blue test!/n");
RedPrintf("this is a red test!/n");
//GreenPrintf("%d, %f, %c",inta,floata,chara);
GreenPrintf("%d/t%c/n",inta,chara);
// GreenPrintf("%f",floata); //function can not work when printing float type?
printf("%f",floata);
//_tprintf("%f",1.0);
getchar();
return 0;
}
做成动态连接库后不能打印浮点型数据了不知道why,哪位大侠知道留个言不胜感激!
编译环境:WinXP sp2 VC++6.0 sp6