1

I get information from user and save them in std::string variables like the following:

std::string username;
std::string domain;
std::string hash_ntlm;
std::string process_name;

std::cout << "\n\tUsername: ";
std::getline(std::cin, username);
std::cout << "\tDomain: ";
std::getline(std::cin, domain);
std::cout << "\tNTLM Hash: ";
std::getline(std::cin, hash_ntlm);
std::cout << "\tProcess: ";
std::getline(std::cin, process_name);
std::cout << "\n";

Unfortuently, I have to work with functions which gets arguments with PCWSTR datatype like the following one:

bool Function(ProcessCreateType arg_type, PCWSTR arg_command_Line, DWORD arg_process_flags, HANDLE arg_user_token, DWORD arg_logon_flags,  PCWSTR arg_user, PCWSTR arg_domain, PCWSTR arg_password, PPROCESS_INFORMATION arg_process_infos, BOOL arg_auto_close_handle)

But when I redefine variables with PCWSTR datatype, I can't get them with std::getline(std::cin, domain); or I can't pass them std::string variable with even calling c_str() method. How should I fix this issue of working with PCWSTR and std::string.

DerStarkeBaer
  • 669
  • 8
  • 28
  • 1
    Use `std::wstring` – PaulMcKenzie Feb 05 '20 at 23:45
  • @PaulMcKenzie I get the error: error C2672: 'std::getline': no matching overloaded function found –  Feb 05 '20 at 23:47
  • You can not mix unicode and multi-byte without converting them. I use `boost::locale::conv` for a one liner. `to_wide(...)` and `to_narrow(...)`. I'll post them if you would like that for an answer. – lakeweb Feb 05 '20 at 23:47
  • @lakeweb Yeah, it is a real challenge for me and I am looking for answer. I change string to wstring but then getline gives me error which no matching overloaded found. –  Feb 05 '20 at 23:49
  • 1
    Try combing `std::wstring`, `std::getline`, and `std::wcout` / `std::wcin`. The "w" types are distinct from non "w" types, so in general you have to match up everything one way or the other (not counting any places where you convert between them). – TheUndeadFish Feb 05 '20 at 23:51
  • duplicate question: https://stackoverflow.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring – Victor Padureanu Feb 06 '20 at 00:11

1 Answers1

0

I take it you are reading an ansi/utf8 file. So with:

inline std::wstring to_wide(const char* ps) { return std::wstring(boost::locale::conv::utf_to_utf<wchar_t>(ps)); }
inline std::wstring to_wide(const std::string& str) { return to_wide(str.c_str()); }
inline std::string to_narrow(const CString& str) { return std::string(boost::locale::conv::utf_to_utf<char>((LPCTSTR)str)); }
inline std::string to_narrow(const wchar_t* ps) { return std::string(boost::locale::conv::utf_to_utf<char>(ps)); }
inline std::string to_narrow(const std::wstring& str) { return to_narrow(str.c_str()); }

You can convert between multi-byte and unicode. I have xml and other files that are ansi/utf8 that need to be presented as LPCTSTR and the likes.

cCtrl.SetWindowText(to_wide(a_std_string).c_ctr());

It means compiling with boost. There are many other ways. But I find this the simplest.

UPDATE:

Following Victor's link. It looks like by c++11 you can use the standard lib. Back in the day it was boost. But I'd go with the same principle and create inline one liners like I did with boost.

lakeweb
  • 1,859
  • 2
  • 16
  • 21