error : C2664 'void RecursiveDelete(LPWSTR,LPWSTR)': cannot convert argument 2 from 'const wchar_t [12]' to 'LPWSTR'

543 Views Asked by At

Any Idea how to fix? Would be really useful. I tried changing conformance mode to off and it worked but the other parts of the program failed. Any other fixes?

Line that has problem :

RecursiveDelete(path, L"desktop.ini");

Edit :

The program is to change windows registry files and is connected with my c++ loader. When the project is standalone not connected to the loader it works perfectly fine.

1

There are 1 best solutions below

7
Remy Lebeau On

The RecursiveDelete() function takes 2 LPWSTR parameters. LPWSTR is an alias for wchar_t*, ie a pointer to non-const wchar_t data. However, a string literal is const data, in this case L"desktop.ini" is a const wchar_t[12] (including the null terminator), which decays into a const wchar_t* pointer. You can't use a pointer-to-const where a pointer-to-non-const is expected, that is what the error message is complaining about.

IF the function DOES NOT alter the contents of its second parameter, then that parameter should be implemented as a pointer-to-const, eg:

void RecursiveDelete(LPWSTR, LPCWSTR);

(aka void RecursiveDelete(wchar_t*, const wchar_t*);).

If changing the declaration of the function is not an option (ie, it is from an existing API, etc), you could instead cast away the string literal's constness using const_cast (be VERY CAREFUL doing this!), eg:

RecursiveDelete(path, const_cast<LPWSTR>(L"desktop.ini"));

However, making a non-const duplicate of the string literal's data would be much safer, eg:

wchar_t copy[] = L"desktop.ini";
RecursiveDelete(path, copy);