[英]C++ Builder 10.3 can not assign to const wchar_t* from 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";
}
问题在于fos.pFrom = "C:\\\\Melon\\\\AGA\\\\Bazy";
. 我尝试使用linkig将"C:\\\\Melon\\\\AGA\\\\Bazy"
分配给const wchar_t*
,但它告诉我它无法链接。 有人知道如何修复它吗?
您使用的是基于TCHAR
的SHFILEOPSTRUCT
版本,因此其字符串字段将分别基于wchar_t
或char
具体取决于是否定义了UNICODE
。
在 C++Builder 6(其中String
是AnsiString
的别名)中,默认情况下未定义UNICODE
。 在 C++Builder 2009 之后(其中String
是UnicodeString
的别名), UNICODE
是默认定义的,但如果遗留项目需要,可以将其关闭。
由于您使用的是基于TCHAR
的结构,因此在为其定义字符串文字时应使用基于TCHAR
的TEXT()
宏,例如:
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);
}
这将适用于所有 C++Builder 版本。
另一方面,如果您尝试使用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);
}
如果您不想依赖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.