Please help in casting.

Error: error C2664: 'LookupPrivilegeNameW': cannot convert parameter 3 from 'char *' to 'LPWSTR' in the LookupPrivilegeName () function, the third parameter.

I think I tried everything I could, but nothing happens.

#include "StdAfx.h" #include <windows.h> #include <stdio.h> #pragma hdrstop /*LPWSTR CharToLPWSTR(LPCSTR char_string) { LPWSTR res; DWORD res_len = MultiByteToWideChar(1251, 0, char_string, -1, NULL, 0); res = (LPWSTR)GlobalAlloc(GPTR, (res_len + 1) * sizeof(WCHAR)); MultiByteToWideChar(1251, 0, char_string, -1, res, res_len); return res; }*/ int main() { HANDLE hToken; LUID setcbnameValue; TOKEN_PRIVILEGES tkp; DWORD errcod; LPVOID lpMsgBuf; LPCTSTR msgptr; UCHAR InfoBuffer[1000]; PTOKEN_PRIVILEGES ptgPrivileges = (PTOKEN_PRIVILEGES)InfoBuffer; DWORD dwInfoBufferSize; DWORD dwPrivilegeNameSize; DWORD dwDisplayNameSize; UCHAR ucPrivilegeName[500]; UCHAR ucDisplayName[500]; DWORD dwLangId; UINT i; if(!OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { puts("OpenProcessToken"); return; } GetTokenInformation(hToken, TokenPrivileges, InfoBuffer, sizeof InfoBuffer, &dwInfoBufferSize); printf("Account privileges: \n\n"); for(i = 0; i < ptgPrivileges->PrivilegeCount; i++) { dwPrivilegeNameSize = sizeof ucPrivilegeName; dwDisplayNameSize = sizeof ucDisplayName; LookupPrivilegeName(NULL, &ptgPrivileges->Privileges[i].Luid, (char *)ucPrivilegeName, &dwPrivilegeNameSize); /*LookupPrivilegeDisplayName(NULL, (char *)ucPrivilegeName, (char *)ucDisplayName, &dwDisplayNameSize, &dwLangId);*/ printf("%s (%s)\n", ucPrivilegeName);//, ucDisplayName); } } 
  • four
    They write to you in black and white: the function takes a pointer to wchar_t, and you give it a pointer to char! - Pavel Mayorov

3 answers 3

In WinApi, many functions have two versions:

  • one for char characters
  • other for characters of type wchar_t

The names of such functions are distinguished by the last letter: A for char , W for wchar_t . In addition, there is a macro, which, depending on the settings of the project (consider a certain constant specified via #define ) allows you to call a particular function without specifying the A / W suffix at all.

So if you need to always rely on the type of char use the A -function, for wchar_t - the W -function. In the case when you want to make a universal version, use the function without a suffix. But then the types of variables used as arguments of such functions should be implicitly specified. Those. instead of char and wchar_t you need to use TCHAR , which will be TCHAR into the required type depending on the settings of the character type of the project (essentially, whether the _UNICODE constant is _UNICODE ). For example, you can read more here .

    Use the ASCII variant of the function, LookupPrivilegeNameA() .

    The original LookupPrivilegeName() function is actually a macro that expands to either ASCII or the Unicode version, depending on the compilation flags.

    Because of its “dual” nature, such functions require “dual” data types - LPTSTR for a pointer to a string and TCHAR + TEXT() for a character. When using char or wchar_t it is necessary to use specific versions of the functions, with the suffixes A and W respectively.

    • 2
      It does not make sense to work with ANSI strings in the unicode version of the operating system, because the conversion to ANSI will be with potential losses. - VladD

    If you work with the unicode version (and with what else? 2017 is the year!), Go to the broad unicode version.

    Replace the ucPrivilegeName definition with

     WCHAR ucPrivilegeName[500]; 

    When outputting unicode strings, use wprintf , etc.