GetComputerName()在Windows控制台中不正确显示Unicode [英] GetComputerName() not displaying Unicode correctly in Windows console

查看:332
本文介绍了GetComputerName()在Windows控制台中不正确显示Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++中的WinAPI编程相对陌生。我试图写一个程序,将获得系统主机名使用GetComputerName()。理想情况下,我希望代码能够在英语和非英语系统上工作。下面是我使用的代码:

I'm relatively new to WinAPI programming in C++. I'm trying to write a program that will obtain the system hostname using GetComputerName(). Ideally, I want the code to be able to work on English and non-English systems. Below is the code that I'm using:

int main()
{
    wstring hostname;
    wchar_t nbtName[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD length = MAX_COMPUTERNAME_LENGTH + 1;
    GetComputerName(nbtName, &length);
    hostname = nbtName;

    wcout << hostname << endl;

    return 0;
}

代码在我的英语Windows 7系统上正常工作, t似乎在我的德语Windows 7系统(使用德语字符的主机名)正确显示。我认为wstring和wchar_t可以处理这些特殊字符。这是我的德语Windows 7系统上显示的内容。

The code works fine on my English Windows 7 system, but the code doesn't seem to display properly on my German Windows 7 system (which uses German characters for the hostname). I thought that wstring and wchar_t could handle these special characters. Here's what's displayed on my German Windows 7 system.

COMPUTER-Í─▄▀

我可以俯瞰一些愚蠢的东西吗?谢谢!

Am I overlooking something stupid? Thanks!

推荐答案

使用 _setmode(_fileno(stdout),_O_U16TEXT)在控制台窗口中显示Unicode:

Use _setmode(_fileno(stdout), _O_U16TEXT) to show Unicode in console window:

#include <iostream>
#include <string>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"ελληνικά\n";
    return 0;
}

或使用 MessageBoxW(0,hostname.c_str ),0,0) OutputDebugStringW 以查看Unicode文本显示正确:

Or use MessageBoxW(0, hostname.c_str(), 0, 0) or OutputDebugStringW to see Unicode text displayed correctly:

这篇关于GetComputerName()在Windows控制台中不正确显示Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆