Print all std::locale names (Windows) - c++

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

Related

CryptUnprotectData function returning encrypted value

I have been trying to wrap my head around how to use the CryptUnprotectData function in c++, but I can seem to get it correct. As of now, the function doesn't seem to be doing anything (the value returned is the value given. When I use GetLastError(), I get an error code of 87 (invalid parameter).
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string data = ("data I want to decode");
cout << "Encrypted data: " << data << endl;
DATA_BLOB DataOut;
BYTE* pbDataOutput = (BYTE*)data.data();
DataOut.pbData = pbDataOutput;
DataOut.cbData = strlen((char*)pbDataOutput);
DATA_BLOB DataVerify;
LPWSTR pDescrOut = NULL;
if (CryptUnprotectData(&DataOut, NULL, NULL, NULL, NULL,0,&DataVerify)){
cout << "decrypted data: " << DataVerify.pbData << endl;
LocalFree(DataVerify.pbData);
}
else{
cout << "Decryption error: ";
cout << GetLastError() << endl;
}
return 0;
}
I have tried copying the code from the documentation here and whittling it down until it just decrypts a string, but I get the same error.

Is there a function for sleeping a set amount in C++ for windows?

I know there is one in C, Sleep(ms), but is there one for C++? I am trying to return an error, then print to the console, then sleep enough for the user to read it before returning the errorcode. Code in C would be:
#include <stdio.h>
#include <windows.h>
int main (int argc, const char *argv[]) {
char *err = "Have an error!";
printf("Error: %s. Program terminating in 5 seconds...", err);
Sleep(5000);
return 1;
}
You could include <windows.h> and just call the WinApi function Sleep just as you would from C. This is mostly pure C++ :
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
int main (int argc, const char *argv[]) {
string err = "Have an error!";
cout << "Error: " << err << ". Program terminating in 5 seconds..." << endl;
std::chrono::milliseconds timespan(5000);
std::this_thread::sleep_for(timespan);
return 1;
}

Communicating pthread with pipes

im trying to communicate a pthread with a process, using pipes, for a college proyect. i make a struct with the pipes and i pass that structure to the pthread so it can listen on the pipe[0], and on the rest of the code i try to send a string to that running pthread.
Here is my code:
#include <unistd.h>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <pthread.h>
using namespace std;
struct Pipefd{
int pipe[2];
string name;
};
void* listenProcess(void* x){
Pipefd* pf = reinterpret_cast<Pipefd*>(x);
close(0);
dup(pf->pipe[0]);
//here i try to see if the struct i send is ok, but this is not printed.
cout << "pf.name: " << pf->name << endl;
string recive;
while(getline(cin,recive)){
cout << "recive: " << recive << endl;
}
cout << "Problem with getline" << endl;
}
int main(int argc, char *argv[]) {
Pipefd myPipe;
myPipe.name = "Test";
pipe(myPipe.pipe);
void* test = reinterpret_cast<void*>(&myPipe);
pthread_t tid;
pthread_create(&tid,NULL, &listenProcess,test);
close(1);
dup(myPipe.pipe[1]);
cout << "This is a message" << endl;
pthread_join(tid,NULL);
}
if someone can reply me with some ideas of how to make this work it would be awesome, if not, thank you for your time.

C++ get current file running exe

So, I've now fought with this for 2 days, still the same error.
I've been on over 300 results with google, still same FAILURE. It shows up as HEX all the time or it doesn't work at all.
This is not using any external librarys and no .net framework.
100% non-dependent.
I've tried over 30 methods.
TCHAR szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
^ Doesn't work; returns hex.
The code is in a void.
#include "SharedHeader.h"
#include <Psapi.h>
#include "CommandLine_Pres.h"
#include <TlHelp32.h>
using namespace std;
void filePath()
{
// Figure out file path of current file
char cCurrentPath[FILENAME_MAX];
if(!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
{
cout << "error" << endl;
}
cCurrentPath[sizeof(cCurrentPath) -1] = '\0';
cout << cCurrentPath << endl;
// Get process id, filename
//cout << GetCommandLine();
int procId = GetCurrentProcessId();
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
/*
DOES NOT WORK BELOW [debug]
HANDLE Handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,GetCurrentProcessId());
if(Handle)
{
TCHAR Buffer[MAX_PATH];
if(GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
}
else
{
}
CloseHandle(Handle);
}*/
}
To get the address of the current running process you can use:
#include <iostream>
int main(int argc, char** argv)
{
std::cout << argv[0] << std::endl;
getchar();
return 0;
}
or:
#include <iostream>
#include <windows.h>
int main()
{
char szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
std::cout << szExeFileName;
getchar();
return 0;
}

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