简体   繁体   English

C++ Builder 10.3 无法从 const char[18] 分配给 const wchar_t*

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

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]' .问题是,在旧版本的构建器(我猜是 6)中它运行良好,现在它抛出[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";问题在于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.我尝试使用linkig将"C:\\\\Melon\\\\AGA\\\\Bazy"分配给const wchar_t* ,但它告诉我它无法链接。 Does somebody have a clue how to fix it?有人知道如何修复它吗?

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.您使用的是基于TCHARSHFILEOPSTRUCT版本,因此其字符串字段将分别基于wchar_tchar具体取决于是否定义了UNICODE

In C++Builder 6 (where String was an alias for AnsiString ), UNICODE was not defined by default.在 C++Builder 6(其中StringAnsiString的别名)中,默认情况下未定义UNICODE 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.在 C++Builder 2009 之后(其中StringUnicodeString的别名), UNICODE是默认定义的,但如果遗留项目需要,可以将其关闭

Since you are using a TCHAR -based struct, you should use the TCHAR -based TEXT() macro when defining string literals for it, eg:由于您使用的是基于TCHAR的结构,因此在为其定义字符串文字时应使用基于TCHARTEXT()宏,例如:

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.这将适用于所有 C++Builder 版本。

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:另一方面,如果您尝试使用String变量为结构定义字符串,则仅当UNICODE在 2009 之前的版本中未定义且在 2009 之后的版本中定义时才有效,例如:

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:如果您不想依赖UNICODE定义,那么您应该明确使用SHFILEOPSTRUCT的 ANSI 或 Unicode 版本,具体取决于您使用的是 ANSI ( char ) 还是 Unicode ( wchar_t ) 字符串,例如:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM