MFC中使用WriteConsole向系統控制檯屏幕輸入跟蹤信息

MFC中使用WriteConsole向系統控制檯屏幕輸入跟蹤信息

編者:李國帥

qq:9611153 微信lgs9611153

時間:2006-11-10

背景原因:

有時侯會有一種需要,在windows的窗口應用中,彈出黑色的系統控制檯命令窗口,輸出一些日誌信息,這時候就會有這種技術。

 

所需資源:

Vc

 

解決方案:

例子

 

轉載別人的寫法

// crt_strlen.c

// Determine the length of a string. For the multi-byte character

// example to work correctly, the Japanese language support for

// non-Unicode programs must be enabled by the operating system.

 

#include <windows.h>

#include <string.h>

#include <locale.h>

#include <stdio.h>

#include <stdlib.h>

#include <mbstring.h>

#include <stdio.h>

#include <tchar.h>

#include <iostream>

#include <string>

#include <Windows.h>

#include <fstream>

 

int main()

{

         char* str1 = "Count.";

         wchar_t* wstr1 = L"Count.我是程序員小心";

         char * mbstr1;

         char * locale_string;

         TCHAR tch[4];

 

         tch[0] = 0x0061; // a

 

         tch[1] = 0x2014; // EM_DASH

 

         tch[2] = 0x0063; // b

 

         tch[3] = 0x0000; // NULL

         DWORD z;

         // strlen gives the length of single-byte character string

         printf("Length of '%s' : %d\n", str1, strlen(str1) );

 

         HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

         if (hStdOut == INVALID_HANDLE_VALUE) return 1;

 

         DWORD dwBytesWritten;

 

         WriteConsole(hStdOut, wstr1, (DWORD)_tcslen(wstr1), &dwBytesWritten, NULL);

 

         WriteConsole(hStdOut, L"\n", 1, &dwBytesWritten, NULL);

         // wstrlen gives the length of a wide character string

         //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),wstr1,sizeof(wstr1),&z,NULL);

         wprintf(L"Length of '%s' : %d\n", wstr1, wcslen(wstr1) );

 

         // A multibyte string: [A] [B] [C] [katakana A] [D] [\0]

         // in Code Page 932. For this example to work correctly,

         // the Japanese language support must be enabled by the

         // operating system.

         mbstr1 = "ABC" "\x83\x40" "D";

 

         locale_string = setlocale(LC_CTYPE, "Japanese_Japan");

 

         if (locale_string == NULL)

         {

                   printf("Japanese locale not enabled. Exiting.\n");

                   exit(1);

         }

         else

         {

                   printf("Locale set to %s\n", locale_string);

         }

 

         // _mbslen will recognize the Japanese multibyte character if the

         // current locale used by the operating system is Japanese

         //printf("Length of '%s' : %d\n", mbstr1, _mbslen(mbstr1) );

 

         // _mbstrlen will recognize the Japanese multibyte character

         // since the CRT locale is set to Japanese even if the OS locale

         // isnot.

         printf("Length of '%s' : %d\n", mbstr1, _mbstrlen(mbstr1) );

         printf("Bytes in '%s' : %d\n", mbstr1, strlen(mbstr1) );

 

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章