Convert char * to LPCWSTR ???

How can i convert

char* appdata = getenv("APPDATA");

to LPCWSTR ??

i have tried this but seems not working

1
2
3
4
 
char* appdata = getenv("APPDATA");
std::string out = appdata;
LPCWSTR str2 = _T(outfilename.c_str());


What am i doing wrong????
You can use MultiByteToWideChar(...):

https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx

But why do you want to do this?
What am i doing wrong????

Why is the string on line 3 called out, but you're using outfilename on line 4? _T is for use with string literals. out is not a string literal.

If you're using windows as the code seems to suggest, why not just use LPCWSTR appdata = _wgetenv("APPDATA"); to begin with?
A different way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string appdata(1024, ' ');

  DWORD len = GetEnvironmentVariableA("APPDATA", &appdata[0], appdata.size());
  appdata.resize(len);

  cout << "Appdata: " << appdata << "\n\n";

  system("pause");
  return 0;
}
Topic archived. No new replies allowed.