2

Currently, I'm dealing with WMI for collecting the log files from a remote machine. I have the username of the remote machine initialized as given below.

wchar_t pszName[]=L"My username";

pszName[] is a wchar_t array. The trouble I face is that when I pass my username as a parameter of string datatype, I need to initialize wchar_t array using a string.

I cannot use a wchar_t* pointer because it gives me an error on the later part of the program. I need to initialize something like

string username = "My username";
wchar_t pszName[] = .....?.....;
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
Anand
  • 69
  • 1
  • 7
  • https://stackoverflow.com/questions/10546514/initialize-wide-char-array – user202729 Jul 25 '18 at 04:58
  • https://stackoverflow.com/questions/2573834/c-convert-string-or-char-to-wstring-or-wchar-t – user202729 Jul 25 '18 at 05:00
  • Does your `string` contain only ASCII characters or some encoding of Unicode characters? – xskxzr Jul 25 '18 at 07:27
  • Yes sir, my string contains only ASCII characters – Anand Jul 25 '18 at 07:42
  • @xskxzr Thank you for the reply. I reffered to the links you have provided But I couldn't find any initialization of a wchat_t[] array. I don't find a difficulty in finding the length of the array. But to initialize the wchar_t[] array using a string is what makes me think. Just like I mentioned in the question, a pointer doesn't suit my requirement. – Anand Jul 25 '18 at 07:53
  • Do you have to use `std::string` for username or you can also use `std::wstring`? – Killzone Kid Jul 25 '18 at 08:08
  • Actually, I'm passing a string from java to c++. The value in the string is to be passed on to a wchar_t[] array. – Anand Jul 25 '18 at 09:29

2 Answers2

0
#include <string>
#include <vector>
#include <windows.h>

std::wstring str_to_wstr(std::string const & str)
{
    int length = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), 0, 0);
    if (!length)
        return L"utf-8 to utf-16 conversion error!";

    std::vector<wchar_t> buffer(length + 1);
    if (!MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), buffer.data(), length))
        return L"utf-8 to utf-16 conversion error!";

    return std::wstring(buffer.data(), length);
}

// ...

std::string username{ "My username" };
std::wstring utf_16_username{ str_to_wstr(username) };

// ... use:
utf_16_username.data();  // wherever you would have used pszName
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • I would opt to throw an exception instead of return an error string on failure. And on the final return, I would opt to use the `wstring` constructior that takes a length as input, instead of relying on `buffer` being null terminated. The way you are using `MultiByteToWideChar`, it doesn't output a null terminator, you are relying on one being provided by the `vector` constructor. But `MultiByteToWideChar` returns the number of chars written to `buffer`, so no need for `wstring` to count them again. – Remy Lebeau Jul 25 '18 at 06:22
  • @Remy Lebeau "`MultiByteToWideChar` returns the number of chars written to buffer, so no need for wstring to count them again." You are right, no need to count twice. – Swordfish Jul 25 '18 at 07:11
  • You should use hardcoded CP_UTF8 as your error really indicates it expects UTF8 input... – rubenvb Jul 25 '18 at 07:15
  • Now that I take another look, you can just write directly into the allocated wstring. You're making a superfluous copy now. Pre-C++17 (but strictly only after C++110, use `&buffer[0]`, otherwise just pass `buffer.data()`. – rubenvb Jul 25 '18 at 07:25
  • @RemyLebeau this looks like copy paste for example [2nd post](https://cboard.cprogramming.com/cplusplus-programming/170009-unicode-string-convertion-help.html) – Killzone Kid Jul 25 '18 at 07:29
  • Thank you for the reply. Your reply seems to be working. But this wasn't what I expected. – Anand Jul 25 '18 at 12:17
0

You can use the std::mbstowcs function to convert your string into wchar_t*:

std::string username = "My username"; //set your username
wchar_t pszName[] = L"My username";   //initialize pszName with a certain length wide string
std::mbstowcs(pszName, name.c_str(), std::wcslen(pszName)); //copy and convert name from username to pszName

And you need to include:

#include <string>
#include <cstdlib>

Be aware, that you have to give a maximum length for pszName, and that this value actually has to have allocated at least this length in memory! Otherwise you get runtime crashes!

With the current implementation, you can just insert a dummy name of the right length into pszName and get the right length with the std::wcslen function.

jan.sende
  • 750
  • 6
  • 23