_WCRTLINK int putch( int c ) { char ch; DWORD written; HANDLE h; ch = c; #ifdef DEFAULT_WINDOWING if( _WindowsPutch != 0 ) { LPWDATA res; res = _WindowsIsWindowedHandle( STDOUT_FILENO ); _WindowsPutch( res, c ); } else { #endif written = 0; h = __NTConsoleOutput(); // obtain a console output handle if( h != INVALID_HANDLE_VALUE ) { WriteConsole( h, &ch, 1, &written, NULL ); } if( written == 0 ) return( -1 ); #ifdef DEFAULT_WINDOWING } #endif return( c ); }
DWORD WINAPI MyThreadFunction( LPVOID lpParam ) { HANDLE hStdout; PMYDATA pDataArray; TCHAR msgBuf[BUF_SIZE]; size_t cchStringSize; DWORD dwChars; // Make sure there is a console to receive output results. hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if( hStdout == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type. // The pointer is known to be valid because // it was checked for NULL before the thread was created. pDataArray = (PMYDATA)lpParam; // Print the parameter values using thread-safe functions. StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), pDataArray->val1, pDataArray->val2); StringCchLength(msgBuf, BUF_SIZE, &cchStringSize); WriteConsole(hStdout, msgBuf, (DWORD)cchStringSize, &dwChars, NULL); return 0; }
extern void _tMyPrintf(IN LPCTSTR tracemsg, ...) { TCHAR buf[MAX_PATH + 2048] = { 0 }; HRESULT ret; __try { va_list ptr; va_start(ptr, tracemsg); ret = StringCchVPrintf( buf, MAX_PATH + 2048, tracemsg, ptr ); if (ret == S_OK) { DWORD ws; WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), buf, (DWORD)_tcslen(buf), &ws, NULL); } } __finally { ; } return; }
/* * Retired functions. Use color.c functions instead. */ int Cputs (int attr, const char *buf) { static BOOL init = FALSE; int rc; if (use_colours && !init) { init_console(); atexit (exit_console); } init = TRUE; if (stdout_hnd == INVALID_HANDLE_VALUE) rc = fputs (buf, NORMAL_STREAM); else { DWORD written = 0; if (attr == 0) attr = csbi.wAttributes; else attr |= csbi.wAttributes & 0xF0; SetConsoleTextAttribute (stdout_hnd, attr); WriteConsole (stdout_hnd, buf, (DWORD)strlen(buf), &written, NULL); SetConsoleTextAttribute (stdout_hnd, csbi.wAttributes); rc = written; } return (rc); }
static void cvt_wprintf(FILE *dest,const wchar *fmt,va_list arglist) { // This buffer is for format string only, not for entire output, // so it can be short enough. wchar fmtw[1024]; PrintfPrepareFmt(fmt,fmtw,ASIZE(fmtw)); #ifdef _WIN_ALL safebuf wchar Msg[MaxMsgSize]; if (dest==stdout && StdoutRedirected || dest==stderr && StderrRedirected) { // Avoid Unicode for redirect in Windows, it does not work with pipes. vswprintf(Msg,ASIZE(Msg),fmtw,arglist); safebuf char MsgA[MaxMsgSize]; WideToChar(Msg,MsgA,ASIZE(MsgA)); CharToOemA(MsgA,MsgA); // Console tools like 'more' expect OEM encoding. // We already converted \n to \r\n above, so we use WriteFile instead // of C library to avoid unnecessary additional conversion. HANDLE hOut=GetStdHandle(dest==stdout ? STD_OUTPUT_HANDLE:STD_ERROR_HANDLE); DWORD Written; WriteFile(hOut,MsgA,(DWORD)strlen(MsgA),&Written,NULL); return; } // MSVC2008 vfwprintf writes every character to console separately // and it is too slow. We use direct WriteConsole call instead. vswprintf(Msg,ASIZE(Msg),fmtw,arglist); HANDLE hOut=GetStdHandle(dest==stderr ? STD_ERROR_HANDLE:STD_OUTPUT_HANDLE); DWORD Written; WriteConsole(hOut,Msg,(DWORD)wcslen(Msg),&Written,NULL); #else vfwprintf(dest,fmtw,arglist); // We do not use setbuf(NULL) in Unix (see comments in InitConsole). fflush(dest); #endif }
int log_printStr(const int level, char *const logStr, void* hStdout) { if(!logStr || (level < LOG_CONSOLEEX || level > LOG_DEBUGEX)) { return -1; } #ifdef WIN32 if(INVALID_HANDLE_VALUE == hStdout) { return -2; } WORD wOldColorAttrs; CONSOLE_SCREEN_BUFFER_INFO csbiInfo; if(GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) { wOldColorAttrs = csbiInfo.wAttributes; SetConsoleTextAttribute(hStdout, COLORS[level]); DWORD ws; WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),logStr,strlen(logStr),&ws,NULL); SetConsoleTextAttribute(hStdout, wOldColorAttrs); } OutputDebugString(logStr); #else fprintf(stdout, "%s%s%s", COLORS[level], logStr, SEQ_DEFAULT_COLOR); #endif return 0; }
DWORD WINAPI MyThread( LPVOID lpParam ) { HANDLE hStdout; PMYDATA pData; TCHAR msgBuf[BUF_SIZE]; size_t cchStringSize; DWORD dwChars; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if( hStdout == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type. pData = (PMYDATA)lpParam; // Print the parameter values using thread-safe functions. // StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), // pData->val1, pData->val2); // StringCchLength(msgBuf, BUF_SIZE, &cchStringSize); WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, NULL); return 0; }
void Message( ) { MSG nMsg = { 0 }; /* while( GetMessage( &nMsg, NULL, 0, 0 ) ) { TranslateMessage( &nMsg ); DispatchMessage( &nMsg ); }*/ while( 1 ) { if( PeekMessage( &nMsg, NULL, 0, 0, PM_NOREMOVE ) ) { if( !GetMessage( &nMsg, NULL, 0, 0 ) ) { return; }else{ TranslateMessage( &nMsg ); DispatchMessage( &nMsg ); } }else{ //¿ÕÏд¦Àí WriteConsole( g_hOutput, "ONIDLE", 6, NULL, NULL ); } } }
/* * 文字列出力メソッド。(非公開) * @return なし */ void ScreenControl::out( const std::string& str // [in] 出力文字列 ) { WriteConsole(mHandle, str.c_str(), str.length(), &mDwNumberOfCharsWrite, NULL); return; }
int GDebugger::WriteToLog(char *fmt,...){ FILE* __fStdOut = NULL; HANDLE __hStdOut = NULL; if(_bConsoleOutput) __hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); else __fStdOut = fopen(_outFile,"a"); if(__fStdOut == NULL && __hStdOut == NULL) return 0; char s[300]; va_list argptr; int cnt; va_start(argptr, fmt); cnt = vsprintf(s, fmt, argptr); va_end(argptr); DWORD cCharsWritten; if(__hStdOut) WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL); if(__fStdOut){ fprintf(__fStdOut, s); fclose(__fStdOut); } return(cnt); }
static void ConsoleWriteReadDefault(LPSTR pReadBuf, DWORD lenReadBuf, LPCSTR pText) { static HANDLE handle = INVALID_HANDLE_VALUE; if (handle == INVALID_HANDLE_VALUE) { AllocConsole(); handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTitle(title); isConsoleOpen = TRUE; } if (pText) { DWORD cnt; if (GetFileType(handle) == FILE_TYPE_CHAR) WriteConsole(handle, pText, lstrlen(pText), &cnt, NULL); else WriteFile(handle, pText, lstrlen(pText), &cnt, NULL); } if (pReadBuf && lenReadBuf > 0) { if (lenReadBuf > 1 && ReadConsole(GetStdHandle(STD_INPUT_HANDLE), pReadBuf, lenReadBuf - 1, &lenReadBuf, 0)) { pReadBuf[lenReadBuf] = 0; } else { pReadBuf[0] = 0; } } }
void DisplayError(char *pszAPI) { LPVOID lpvMessageBuffer; TCHAR szPrintBuffer[512]; DWORD nCharsWritten; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpvMessageBuffer, 0, NULL); const size_t size = 1024; WCHAR wszAPI[size]; mbstowcs(wszAPI, pszAPI, strlen(pszAPI)); _stprintf(szPrintBuffer, _T("ERROR: API = %s.\n error code = %d.\n message = %s.\n"), wszAPI, GetLastError(), (char *)lpvMessageBuffer); WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),szPrintBuffer, lstrlen(szPrintBuffer),&nCharsWritten,NULL); LocalFree(lpvMessageBuffer); ExitProcess(GetLastError()); }
int main() { HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE); _TCHAR buffer[25][80] = { _T(' ') }; _TCHAR ramp[] = _T(".:-=+*#%@"); float t = 0.0f; for (;; t += 0.1f) { int sy = 0; float s = sinf(t); float a = s * s * s * s * 0.2f; float z = 1.3f; for (; z > -1.2f; z -= 0.1f) { _TCHAR* p = &buffer[sy++][0]; float tz = z * (1.2f - a); float x = -1.5f; for (; x < 1.5f; x += 0.05f) { float tx = x * (1.2f + a); float v = f(tx, 0.0f, tz); if (v <= 0.0f) { float y0 = h(tx, tz); float ny = 0.01f; float nx = h(tx + ny, tz) - y0; float nz = h(tx, tz + ny) - y0; float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz); float d = (nx + ny - nz) * nd * 0.5f + 0.5f; *p++ = ramp[(int)(d * 5.0f)]; } else *p++ = ' '; } } for (sy = 0; sy < 25; sy++) { COORD coord = { 0, sy }; SetConsoleCursorPosition(o, coord); WriteConsole(o, buffer[sy], 79, NULL, 0); } Sleep(33); } }
static void debug_out(char *s) { startblit(); WriteConsole(consf, s, strlen(s), NULL, NULL); endblit(); rpclog("%s", s); }
void FlushBuffer( void ) { DWORD nWritten; if (nCharInBuffer <= 0) return; WriteConsole( hConOut, ChBuffer, nCharInBuffer, &nWritten, NULL ); nCharInBuffer = 0; }
int waitkey(const char* prompt) { #ifdef OS_Windows DWORD mode; HANDLE hstdin; INPUT_RECORD inrec; DWORD count; /* Set the console mode to no-echo, raw input, */ /* and no window or mouse events. */ hstdin = GetStdHandle(STD_INPUT_HANDLE); if (hstdin == INVALID_HANDLE_VALUE || !GetConsoleMode(hstdin, &mode) || !SetConsoleMode(hstdin, 0)) return 0; /* Instruct the user */ WriteConsole( hConsole, prompt, strlen(prompt), &count, NULL ); FlushConsoleInputBuffer(hstdin); /* Get a single key RELEASE */ do ReadConsoleInput(hstdin, &inrec, 1, &count); while ((inrec.EventType != KEY_EVENT) || !inrec.Event.KeyEvent.bKeyDown); /* Restore the original console mode */ SetConsoleMode(hstdin, mode); return inrec.Event.KeyEvent.wVirtualKeyCode; #else struct termios savedState, newState; int c; if (-1 == tcgetattr(STDIN_FILENO, &savedState)) return EOF; /* error on tcgetattr */ newState = savedState; /* disable canonical input and disable echo. set minimal input to 1. */ newState.c_lflag &= ~(ECHO | ICANON); newState.c_cc[VMIN] = 1; if (-1 == tcsetattr(STDIN_FILENO, TCSANOW, &newState)) return EOF; /* error on tcsetattr */ c = getchar(); /* block (without spinning) until we get a keypress */ /* restore the saved state */ if (-1 == tcsetattr(STDIN_FILENO, TCSANOW, &savedState)) return EOF; /* error on tcsetattr */ return c; #endif }
static void hb_gt_cgi_termOut( PHB_GTCGI pGTCGI, const char * szStr, HB_SIZE nLen ) { #ifdef HB_GT_CGI_WIN #define HB_WIN_IOWRITE_LIMIT 10000 /* https://tahoe-lafs.org/trac/tahoe-lafs/ticket/1232#no1 */ if( pGTCGI->fIsConsole ) { HANDLE hFile = DosToWinHandle( pGTCGI->hStdout ); HB_SIZE nWritten = 0; HB_SIZE nCount; LPTSTR lpString; #if defined( UNICODE ) lpString = hb_cdpnStrDupU16( HB_GTSELF_TERMCP( pGTCGI->pGT ), HB_CDP_ENDIAN_NATIVE, szStr, nLen, &nCount ); #else nCount = nLen; lpString = hb_cdpnDup( szStr, &nCount, HB_GTSELF_TERMCP( pGTCGI->pGT ), hb_setGetOSCP() ); #endif while( nCount ) { DWORD dwToWrite; DWORD dwWritten; /* Determine how much to write this time */ if( nCount > ( HB_SIZE ) HB_WIN_IOWRITE_LIMIT ) { dwToWrite = HB_WIN_IOWRITE_LIMIT; nCount -= ( HB_SIZE ) dwToWrite; } else { dwToWrite = ( DWORD ) nCount; nCount = 0; } if( ! WriteConsole( hFile, lpString + nWritten, dwToWrite, &dwWritten, NULL ) ) break; nWritten += ( HB_SIZE ) dwWritten; if( dwWritten != dwToWrite ) break; } hb_xfree( lpString ); } else hb_fsWriteLarge( pGTCGI->hStdout, szStr, nLen ); #else hb_fsWriteLarge( pGTCGI->hStdout, szStr, nLen ); #endif }
void ConsoleListener::WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Text, size_t Len) { /* const int MAX_BYTES = 1024*10; char Str[MAX_BYTES]; va_list ArgPtr; int Cnt; va_start(ArgPtr, Text); Cnt = vsnprintf(Str, MAX_BYTES, Text, ArgPtr); va_end(ArgPtr); */ DWORD cCharsWritten; WORD Color; switch (Level) { case NOTICE_LEVEL: // light green Color = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; case ERROR_LEVEL: // light red Color = FOREGROUND_RED | FOREGROUND_INTENSITY; break; case WARNING_LEVEL: // light yellow Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; case INFO_LEVEL: // cyan Color = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; case DEBUG_LEVEL: // gray Color = FOREGROUND_INTENSITY; break; default: // off-white Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break; } if (Len > 10) { // First 10 chars white SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); WriteConsole(hConsole, Text, 10, &cCharsWritten, NULL); Text += 10; Len -= 10; } SetConsoleTextAttribute(hConsole, Color); WriteConsole(hConsole, Text, (DWORD)Len, &cCharsWritten, NULL); }
VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime ) { CHAR szText[256] = { 0 }; sprintf( szText, "定时器:%d\n", idEvent ); WriteConsole( g_hOutput, szText, strlen(szText), NULL, NULL ); }
int ConsoleLog::Write(LPCSTR str, int length) { DWORD count = 0; if (bShowOutput && (hConsole != nullptr) && (length > 0)) WriteConsole(hConsole, str, length, &count, NULL); return count; }
inline const bool ConsoleUI::WriteChar(TCHAR ch) const { DWORD cchOutput; // Characters actually displayed in console // Display the single character at the current cursor position return (WriteConsole(m_hout, &ch, 1, &cchOutput, NULL) == TRUE); }
void OnMouseWheel( HWND hWnd, WPARAM wParam ) { short nDelta = HIWORD(wParam);//偏移量 char szText[256] = { 0 }; sprintf( szText, "偏移量:%d\n", nDelta ); WriteConsole( g_hOutput, szText, strlen(szText), NULL, NULL ); }
InternalOutput &operator<<(InternalOutput &out, const WCHAR *string) { DWORD charWritten; WriteConsole(out.outputHandle, string, static_cast<DWORD>(wcslen(string)), &charWritten, 0); return out; }
void xlwLogger::Display(){ WriteConsole(theScreenHandel, theInnerStream.str().c_str(), theInnerStream.str().size(), &CharsWritten, 0); theInnerStream.str(EmptyString); }
CMom::~CMom(){ int iTemp = 0; if (pList){ if (iType == MASTER){ char *szTemp; DWORD dCount; DEBUG_DUMP("CMom::~CMom: Entering Critical Section gCS") szTemp = "\n\n====================================\nCMom::~CMom: WE ARE DELETING CMom! OH MY GOD! NO!!! DON'T DO IT!!!\n\n\n"; WriteConsole(ghDebug, szTemp, strlen(szTemp), &dCount, 0); EnterCriticalSection(&gCS); #ifdef _DEBUG DebugConnection("CMom Mother structure destroying.\n", 0); #endif }else{ /* DELETE THIS BLOCK: If this is not a MASTER then it is a DAUGHTER and a Daughter is only controlled by one thread so we should not need a CS since a thread can not preempt itself. EnterCriticalSection(&gCSMom); */ #ifdef _DEBUG DebugConnection("CMom Daughter structure destroying.\n", 0); #endif } while(iTemp < iNumOfEntries){ if(pList[iTemp]){ switch(iType){ case MASTER: delete (CMom *) pList[iTemp]; pItems[iTemp] = FREE_ITEM; pList[iTemp] = 0; break; case DAUGHTER: // Next line will be used once ODBC_TYPE is an // object class // delete (ODBC_TYPE *) pList[iTemp]; // In the meantime... #ifdef _DEBUG DebugConnection("CMom daughter deleting connetion...", 0); #endif DeleteConn(((ODBC_TYPE *)pList[iTemp])->conn); break; } } iTemp++; } delete [] pList; if (iType == MASTER){ DeleteCriticalSection(&gCSMom); LeaveCriticalSection(&gCS); DEBUG_DUMP("CMom::~CMom: Left Critical Section gCS ") }else{
void ConsoleOutput::Write(LogLevel level, const std::string& loggerName, const std::string& message) { if (this->outHandle != nullptr) { EnterCriticalSection(&this->writeLock); switch (level) { case LogLevel::Debug: SetConsoleTextAttribute(this->outHandle, FOREGROUND_INTENSITY); break; case LogLevel::Warning: SetConsoleTextAttribute(this->outHandle, FOREGROUND_RED | FOREGROUND_GREEN); break; case LogLevel::Error: SetConsoleTextAttribute(this->outHandle, FOREGROUND_RED); break; case LogLevel::Fatal: SetConsoleTextAttribute(this->outHandle, FOREGROUND_RED | FOREGROUND_INTENSITY); break; } DWORD written; WriteConsole(this->outHandle, "[", 1, &written, nullptr); WriteConsole(this->outHandle, loggerName.c_str(), loggerName.length(), &written, nullptr); WriteConsole(this->outHandle, "]", 1, &written, nullptr); WriteConsole(this->outHandle, LevelStrings[(int)level].c_str(), LevelStrings[(int)level].length(), &written, nullptr); WriteConsole(this->outHandle, " ", 1, &written, nullptr); WriteConsole(this->outHandle, message.c_str(), message.length(), &written, nullptr); WriteConsole(this->outHandle, "\n", 1, &written, nullptr); SetConsoleTextAttribute(this->outHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); LeaveCriticalSection(&this->writeLock); } }
int Screen::writeText (const char *text) { DWORD charsWritten; if (text == 0) throw AppException (WHERE, ERR_INTERNAL); if (WriteConsole (mScreenBuf, text, (DWORD) strlen (text), &charsWritten, 0) != TRUE) throw AppException (WHERE, ERR_WINDOWS_FMT, "WriteConsole", GetLastError ()); return charsWritten; }
static void CPROC DrawString( PCONSOLE_INFO pdp, int x, int y, RECT *r, char *s, int nShown, int nShow ) { uint32_t dwSize; lprintf( "Adding string out : %s %d %d at %d,%d", s, nShown, nShow,x,y,r->left,r->top ); move( y-1, x ); r->right = r.left + nShow; r->bottom = r.top + 1; WriteConsole( pdp->consolecon.hStdout, s + nShown, nShow, &dwSize, NULL ); }
int Print(const char * str, int x, int y) { COORD coPos = {(SHORT)x, (SHORT)y}; unsigned int cell = 0; SetConsoleCursorPosition(g_console.hWork, coPos); WriteConsole(g_console.hWork, str, strlen(str), &cell, NULL); return (int)cell; }
void OnLButtonDown(HWND hWnd,WPARAM wParam,LPARAM lParam) { char szText[256] = { 0 }; sprintf( szText, "WM_LBUTTONDOWN: 按键状态:%08X, x=%d,y=%d\n", wParam, LOWORD(lParam), HIWORD(lParam) ); WriteConsole( g_hOutput, szText, strlen(szText), NULL, NULL ); }