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) );

 

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值