[英]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.