"L" and "LPCWSTR" in the WIndows API

I found that

NetUserChangePassword(0, 0, L"ab", L"cd");

changes user password from ab to cd. Nonetheless,

NetUserChangePassword(0, 0, (LPCWSTR) "ab", (LPCWSTR) "cd");

does not work. The return value indicates an invalid password.

I need to pass const char*as the last two parameters to this function call. How can i do this? For instance,

NetUserChangePassword(0, 0, (LPCWSTR) vs[0].c_str(), (LPCWSTR) vs[1].c_str());

Where vs std::vector<std::string>.

+3
source share
5 answers

These are two completely different L .. The first is part of the C ++ syntax. The prefix of the string literal is c Land it becomes a wide string literal; instead of an array charyou get an array wchar_t.

L LPCWSTR . . , , . L 16- Windows, . , - 64 , , . API-, LP. ; Microsoft , .

LPCWSTR, , W. - char LPCWSTR . , -cast , , , , . . , , , .

const char*, -, L. . ( Windows, LPCSTR - no W.) , , const wchar_t*. , L .

, , . , . std::wstring, std::string, wchar_t char. c_str() a const wchar_t*. wstring, wchar_t.

std::string, - . , , std::string. " ANSI" ; CP_ACP. MultiByteToWideString, .

int required_size = MultiByteToWideChar(CP_ACP, 0, vs[0].c_str(), vs[0].size(), NULL, 0);
if (required_size == 0)
  ERROR;
// We'll be storing the Unicode password in this vector. Reserve at
// least enough space for all the characters plus a null character
// at the end.
std::vector<wchar_t> wv(required_size);
int result = MultiByteToWideChar(CP_ACP, 0, vs[0].c_str(), vs[0].size(), &wv[0], required_size);
if (result != required_size - 1)
  ERROR;

, wchar_t*, : &wv[0]. wstring, :

// The vector is null-terminated, so use "const wchar_t*" constructor
std::wstring ws1 = &wv[0];
// Use iterator constructor. The vector is null-terminated, so omit
// the final character from the iterator range.
std::wstring ws2(wv.begin(), wv.end() - 1);
// Use pointer/length constructor.
std::wstring ws3(&wv[0], wv.size() - 1);
+5

.

- - . . L - , (a wchar_t). L - (a char). C- (LPCWSTR) "ab", char s . , .

, MultiByteToWideChar. , ; , , CP_ACP . , wstring, (one, ). wstring , string, wstring .c_str() wchar_t s.

:

const char * . ?

, . , . ( ) , , , , , . , , , , :

UNICODE . API- , , , ( ANSI () API, A W - A W, , CreateWindowW - . . .) , API - UNICODE, .

+6

C, , . , , .

ASCII Unicode API. NetUserChangePasswordA, char *, , .

+4

LPWSTR wchar_t * (whcar_T - 2 char -), , 1 .

+1

LPCWSTR , wchar_t *.

vs std::vector<std::wstring>, char vs [0].c_str()

http://msdn.microsoft.com/en-us/library/aa370650(v=vs.85).aspx, , UNICODE, wchar_t.

0

Source: https://habr.com/ru/post/1789405/


All Articles