Get current system locale on Windows 10 in C++? - c++

I'm trying to get the current system locale on Windows 10 in C++.
#include <locale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
cout << "LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
cout << "LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;
return 0;
}
It returns
LC_ALL: Chinese (Traditional)_Taiwan.950
LC_CTYPE: Chinese (Traditional)_Taiwan.950
I wonder if it possible to show as BCP47 language tags like zh-TW ?

Related

How to get user name from cpp in Cmake project?

I am trying to just get user name. i.e., it's equivalent to whoami in ubuntu machine. But I am unable to get. I have tried following snippets.
method-1:
std::string get_username() {
struct passwd *pwd = getpwuid(getuid());
if (pwd)
return pwd->pw_name;
else
return "(?)";
}
method-2:
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
cout << "Name is : " << text;
pclose(name);
cout << endl;
return 0;
}
method-3:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << getenv("USER") << endl;
cout << getenv("HOME") << endl;
return 0;
}
The all methods are returning value as I expected. But, When I integrate this code into my Cmake project, it always returns root. I am confused why I am always getting root as response when I try with Cmake.
How to get right value instead of root?

Print all std::locale names (Windows)

My program checks for uppercase letters in German language.
#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/locale.hpp>
using namespace std;
int main()
{
locale::global(locale("Germany_german"));
//locale::global(locale("de_DE.UTF-8")); //Also tried "de_DE.UTF-8", but does not work
string str1 = "über";
cout << boolalpha << any_of(str1.begin(), str1.end(), boost::algorithm::is_upper()) << endl;
string str2 = "Ää";
cout << boolalpha << any_of(str2.begin(), str2.end(), boost::algorithm::is_upper()) << endl;
return 0;
}
program crashes with error on console
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
I don't know what that exact locale string is, "de_DE.UTF-8" doesn't work as well.
Is there any way I can get exact locale name strings for all locales supported by OS. May be there is a list somewhere in header files, but I don't see anything <locale> header.
I wrote a program to print all supported locale names.
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ostream>
#include <iterator>
using namespace std;
vector<wstring> locals;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
locals.push_back(pStr);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);
for (vector<wstring>::const_iterator str = locals.begin(); str != locals.end(); ++str)
wcout << *str << endl;
wcout << "Total " << locals.size() << " locals found." << endl;
return 0;
}
Works great.
...
de
de-AT
de-CH
de-DE
de-DE_phoneb
de-LI
de-LU
...
Total 429 locals found.
#user1 The following might do the same as your elegant code. I can't test it because of the C1189 compiler error.
#include <Winnls.h>
#include <iostream>
#include <ostream>
using namespace std;
int size = 0;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam) {
size++;
wcout << *pStr << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);
wcout << "Total " << size << " locales found." << endl;
return 0;
}

Check GCC version using Eclipse

I try to get my gcc version using below code.
when I build and run it ,
console show "Nothing to build fro (my project name) ".
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main( int argc, char ** argv ) {
stringstream version;
version << "GCC version: "
<< __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__
<< "\nVersion string: " << __VERSION__;
cout << version.str() << endl;
return 0;
}
I found the solution.
I didn't set path variable to MinGW.
I it working properly.
Thank you all.

Visual C++ DLL that checks if file exists

I made this dll file that tries to check if a file exists. But even if I manually create the file, my dll still can't find it.
My dll retrieves the process id of the running program and looks for a file that is named after the pid.
Can anyone please tell me what I'm missing :(
Code:
#include <Windows.h>
#include <winbase.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int clientpid = GetCurrentProcessId();
ifstream clientfile;
string clientpids, clientfilepath;
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}
Supposing you are working with UNICODE
I think the problems goes in the following line:
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl; The tempcvar is a tchar, and maybe you are working with unicode, so it means tempcvar is a widechar.
The result that you get inserting tempcvar in ostr is not what you are expecting (You are mixing multibyte with widechar too). A solution to this problem is converting tempcvar into a multi byte string (const char* or char*...)
Look at this example based on your code (Look at the convertion between tchar to multibyte char)
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
// Convertion between tchar in unicode (wide char) and multibyte
wchar_t * tempcvar_widechar = (wchar_t*)tempcvar;
char* to_convert;
int bytes_to_store = WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,NULL,0,NULL,NULL);
to_convert = new char[bytes_to_store];
WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,to_convert,bytes_to_store,NULL,NULL);
// Using char* to_convert that is the tempcvar converted to multibyte
ostr << to_convert << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}
You can search more about the wide string to multibyte string convertion if this example does not works to you.
Check if you are working with Unicode, if you are, maybe this is your problem.
If you are not working with unicode, the problem in your code can be opening the file.
Hope it helps!

Obtaining the windows desktop path

This is my code:
#include <Windows.h>
#include <ShlObj.h>
#include <iostream>
using namespace std;
int main()
{
LPTSTR myPath = NULL;
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);
if(myPath != NULL)
cout << "It returns something" << endl;
else
cout << "It returns nothing" << endl;
system("PAUSE");
return 0;
}
But myPath returns nothing. I just want to obtain the Desktop path. I'm on Windows 7 64 bits.
You need to give it room to put the data into:
T_CHAR myPath[ MAX_PATH ];
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);