C++ Builder 10.3 can not assign to const wchar_t* from const char[18]

miś_kolorowy

I have a simple code for directories handling, and here is a part of it. The problem is, that in older version of builder(I guess it is 6) it was working perfectly, now it throws [bcc32c Error] Unit1.cpp(32): assigning to 'PCZZWSTR' (aka 'const wchar_t *') from incompatible type 'const char [18]'.

void __fastcall TForm1::Button2Click(TObject *Sender)
{
SHFILEOPSTRUCT fos;
String dirDest;
fos.hwnd = Handle;
//operacja kopiowania
fos.wFunc = FO_COPY;
//plik źródłowy
fos.pFrom = "C:\\Melon\\AGA\\Bazy";
}

The problem is with line fos.pFrom = "C:\\Melon\\AGA\\Bazy";. I tried assigning "C:\\Melon\\AGA\\Bazy" to const wchar_t* using linkig, but it thrown me that it can not be linked. Does somebody have a clue how to fix it?

Remy Lebeau

You are using the TCHAR-based version of SHFILEOPSTRUCT, so its string fields will be based on either wchar_t or char depending on whether UNICODE is defined or not, respectively.

In C++Builder 6 (where String was an alias for AnsiString), UNICODE was not defined by default. In C++Builder 2009 onward (where String is an alias for UnicodeString), UNICODE is defined by default, but can be turned off if needed for legacy projects.

Since you are using a TCHAR-based struct, you should use the TCHAR-based TEXT() macro when defining string literals for it, eg:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    SHFILEOPSTRUCT fos;
    fos.hwnd = Handle;
    //operacja kopiowania
    fos.wFunc = FO_COPY;
    //plik źródłowy
    fos.pFrom = TEXT("C:\\Melon\\AGA\\Bazy\0"); // don't forget the extra null terminator!
    fos.pTo = TEXT("...\0");
    ...
    SHFileOperation(&fos);
}

That will work in all C++Builder versions.

On the other hand, if you are trying to use a String variable to define strings for the struct, that will work only if UNICODE is undefined in pre-2009 versions, and defined in post-2009 versions, eg:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    String dirSrc("C:\\Melon\\AGA\\Bazy\0", 18); // don't forget the extra null terminator!
    String disDest(...);
    SHFILEOPSTRUCT fos;
    fos.hwnd = Handle;
    //operacja kopiowania
    fos.wFunc = FO_COPY;
    //plik źródłowy
    fos.pFrom = dirSrc.c_str();
    fos.pTo = dirDest.c_str();
    ...
    SHFileOperation(&fos);
}

If you don't want to rely on the UNICODE define, then you should use the ANSI or Unicode version of SHFILEOPSTRUCT explicitly, depending on whether you are working with ANSI (char) or Unicode (wchar_t) strings, eg:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    SHFILEOPSTRUCTA fos;
    fos.hwnd = Handle;
    //operacja kopiowania
    fos.wFunc = FO_COPY;
    //plik źródłowy
    fos.pFrom = "C:\\Melon\\AGA\\Bazy\0"; // don't forget the extra null terminator!
    fos.pTo = "...\0";
    ...
    SHFileOperationA(&fos);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    SHFILEOPSTRUCTW fos;
    fos.hwnd = Handle;
    //operacja kopiowania
    fos.wFunc = FO_COPY;
    //plik źródłowy
    fos.pFrom = L"C:\\Melon\\AGA\\Bazy\0"; // don't forget the extra null terminator!
    fos.pTo = L"...\0";
    ...
    SHFileOperationW(&fos);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert const char* to const wchar_t*

win32 atoi issue which can't convert argument 1 from 'wchar_t [5]' to 'const char *

C++ wifstream: Incompatible type char const*, wchar_t const*

cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'

String parameter const char* and const wchar_t*

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

How could I assign the value from lua_tostring() to a variable in type of wstring or const wchar_t* in utf-8

String conversion from System::String ^ to const wchar_t *

Is " const wchar_t* " a pointer?

How to use regex_iterator with const char/wchar_t*?

How to transform const wchar_t type to LPTSTR (C++)

Convert const wchar_t* into a WCHAR*

Convert LOWORD(wParam) to const wchar_t*

Make a condition on const wchar_t*

Convert const wchar_t* to LPWSTR

Can we assign const char* to a string in cpp?

Type conversion from string to const wchar_t* . Type does not match

Can't assign text to a const in Node

"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc" when trying to use WriteConsoleOutputCharacter()

C - can I create a const char * variable from char *?

Why is it not allowed to assign const char * to const variable?

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

Unordered_map find const wchar_t*

Casting std::wstring to const wchar_t x[]

Poco::Path compiles with const wchar_t* but behaves unexpectedly

How to make concatenations at const wchar_t* parameters?