64,654
社区成员
发帖
与我相关
我的任务
分享
#if !defined(__CONSOLE_H_)
#define __CONSOLE_H_
#include <windows.h>
#define OUTPUT(msg) CConsole::GetInstance().Output(msg)
class CConsole
{
public:
// write output to console
void Output(LPCTSTR szOutput = NULL, ...);
public:
static CConsole& GetInstance();
private:
CConsole();
private:
// create the console
bool Create(LPCTSTR szTitle);
private:
HANDLE hConsole;
};
#endif
#include "stdafx.h"
#include "Console.h"
// macro for the Clear() function
#define ERR(bSuccess) { if(!bSuccess) return; }
// macro to check whether hConsole is valid
#define CHECK(hHandle) { if(hHandle == NULL) return; };
CConsole& CConsole::GetInstance()
{
static CConsole *_instance = new CConsole();
return *_instance;
}
CConsole::CConsole()
{
hConsole=NULL;
GetInstance().Create(__TEXT("console"));
}
bool CConsole::Create(LPCTSTR szTitle)
{
// Has console been already created?
if(hConsole != NULL)
return false;
// Allocate a new console for our app
if(!AllocConsole())
return false;
// Create the actual console
hConsole = CreateFile(__TEXT("CONOUT$"), GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
if(hConsole == INVALID_HANDLE_VALUE)
return false;
if(SetConsoleMode(hConsole, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT ) == 0)
return false;
// set the console title
if(szTitle != NULL)
SetConsoleTitle(szTitle);
return true;
}
void CConsole::Output(LPCTSTR szOutput, ...)
{
CHECK(hConsole);
DWORD dwWritten;
WCHAR out[256];
va_list va;
// if not parameter set, write a new line
if(szOutput == NULL)
wsprintfW(out,__TEXT("\n"));
// process arguments
else
{
va_start(va, szOutput);
wvsprintf(out, szOutput, va);
va_end(va);
}
// write to the console
WriteConsole(hConsole,out,wcslen(out),&dwWritten,0);
}
#include "Console.h"
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MESSAGEQUEUE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// OUTPUT(__TEXT("main"));
CConsole::GetInstance();//--》出现错误LKN2019