GetComputerName() not displaying Unicode correctly in Windows console - c++

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;
}
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!

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;
}
Or use MessageBoxW(0, hostname.c_str(), 0, 0) or OutputDebugStringW to see Unicode text displayed correctly:

this function has two versions GetComputerNameW for unicode and GetComputerNameA for ANSI. if UNICODE is defined in your environment the the first one will be called. So either make sure UNICODE is defined or try to call the GetComputerNameW directly.

Windows Console output was the culprit. The Unicode characters display correctly in other non-console output. Thanks everyone!

Related

How to search in unicode using FindWindow in C++

I am stuck about searching for a Chinese program name while using FindWindowW(NULL, "program name") function.
When I searched for English, it works perfectly.
Can someone give me a clue about how to search using a unicode?
I couldn't figure out yet, can someone guide me how to do?
#include <windows.h>
#include <stdio.h>
int main(){
HWND hWnd = FindWindowW(NULL,L"\uAA5A\uAA4C\uB873\uAB4C\uB6C7");
if(NULL == hWnd){
printf("NotFound!");
}else {
printf("Found!");
}
}
Use the Unicode (wide) version of FindWindow and use wide strings for the search. I also recommend saving the source in UTF-8 encoding and using the /utf-8 compiler switch for the Microsoft compiler; otherwise, the compiler will assume a localized ANSI encoding to interpret the wide string. That's fine if you're localized encoding is a Chinese-variant, but if you're on a US or Western European version of Windows the Microsoft IDE will probably prompt you to save in UTF-16 if you use Chinese characters in string constants:
Example:
#include <windows.h>
#include <stdio.h>
int main(void)
{
//HWND h = FindWindowW(NULL,L"马克"); // works if saved in UTF-8 encoding
// // and compiled with /utf-8.
HWND h = FindWindowW(NULL,L"\u9a6c\u514b");
if(h == NULL)
printf("err = %ld\n",GetLastError());
else
printf("handle = %p\n",h);
}
On Windows I changed the terminal window to a matching Chinese title with title 马克 and this code found the window:
C:\>title 马克
C:\>test
handle = 00000000000B0258
C:\>test
handle = 00000000000B0258
Microsoft's Spy++ tool confirms the handle:

Farsi character utf8 in c++

i m trying to read and write Farsi characters in c++ and i want to show them in CMD
first thing i fix is Font i add Farsi Character to that and now i can write on the screen for example ب (uni : $0628) with this code:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"\u0628 \n";
wcout << L"ب"<<endl;
system("pause");
}
but how i can keep this character ... for Latin characters we can use char or string but how about Farsi character utf8 ?!
and how i can get them ... for Latin characters we use cin>>or gets_s
should i use wchar_t? if yes how?
because with this code it show wrong character ...
wchar_t a='\u0628';
wcout <<a;
and i can't show this character بـ (uni $FE91) even though that exist in my installed font but ب (uni $0628) showed correctly
thanks in advance
The solution is the following line:
wchar_t a=L'\u0628';
The use of L tells the compiler that your type char is a wide char ("large" type, I guess? At least that's how I remember it) and this makes sure the value doesn't get truncated to 8 bits - thus this works as intended.
UPDATE
If you are building/running this as a console application in Windows you need to manage your code pages accordingly. The following code worked for me when using Cyrillic input (Windows code page 1251) when I set the proper code page before wcin and cout calls, basically at the very top of my main():
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
For Farsi I'd expect you should use code page 1256.
Full test code for your reference:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
SetConsoleOutputCP(1256); // to manage console output
SetConsoleCP(1256); // to properly process console input
wchar_t b;
wcin >> b;
wcout << b << endl;
}

I want to print in Hebrew (from right to left) to the console

Unlike English and Latin languages, Hebrew - in it's awesomeness, is written right to left. I wan't to get this message across to the computer realm.
I understand I have to use unicode/wchar for the actual string.
I understand I have to set the console to support unicode/wchar.
I understand I have to tell the console to use a supported font.
So I run this code:
#include "stdafx.h" // MS Visual Studio precompiled header file
#include <fcntl.h> // for _setmode
#include <io.h> // for _setmode
#include <windows.h> // for SetCurrentConsoleFontEx
int main()
{
// use unicode/wchar as the actual string.
wchar_t *hebrewString = L"עברית";
// set the console to support unicode/wchar.
_setmode(_fileno(stdout), _O_U16TEXT);
// tell the console to use a supported font.
CONSOLE_FONT_INFOEX info = { 0 };
info.cbSize = sizeof(info);
info.dwFontSize.Y = 20;
wcscpy_s(info.FaceName, L"Courier New");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, &info);
// print
wprintf(L"%s", hebrewString);
// wait for input, so that console won't disappear immediately
getchar();
// return
return 0;
}
This isn't bad, but the actual print is in reverse:
Is there a way to properly configure it so that it will print in the right order, or do I have to manually flip the string before passing it to the console?

Win32 C++: How to print a smily face ☻ onto console output stream? [duplicate]

This question already has answers here:
Output unicode strings in Windows console app
(16 answers)
Closed 7 years ago.
I tried setlocale, wchar, usual char... how to define:
char c = '☻'
cout << c << endl;
to print onto console anething (this is relevant to VS2012 and http://ideone.com/LJtDUz)
The problem is that windows by default does not handle Unicode characters and the windows console is not able to display them.
A solution I found at This CPP Thread is as follows:
#include <fcntl.h>
#include <io.h>
#include <iostream>
int
main(int argc, char* argv[])
{
wchar_t* c = L"☻"; /* needed because the project is using 'Unicode Character Set' */
_setmode(_fileno(stdout), _O_WTEXT); /* set stdout to UTF16 */
std::wcout << c << std::endl; /* use a wide cout call to output characters */
getchar();
return 0;
}
When you try and use the ☻ symbol directly in your code, windows will tell you that i needs to convert your files to Unicode, make sure to accept that and use Unicode Character Set as your projects character set type.

Airplane symbol using Unicode C++

I'm trying to print the Airplane symbol using Unicode in my CodeBlocks. I found out that the code of Airplane is \u2708. So I tried the following code:
#include <iostream>
using namespace std;
int main() {
wchar_t a = '\u2708';
cout << a;
return 0;
}
It outputs 40072 when I replace wchar_t with char
char a = '\u2708';
I get this symbol: ł
Im really stuck, thanks for any help.
If you are in Linux and dont use codepage conversion from unicode to console try this:
std::locale::global(std::locale(""));
wchar_t plane = L'\u2708';
std::wcout << plane << std::endl;
In windows is a bit more complicated, you need a compatible unicode font on the default console and the correct codepage.