Quantcast
Channel: Embarcadero Community - Embarcadero Community
Viewing all articles
Browse latest Browse all 2504

Problem with CopyFile and UnicodeString-Variablen

$
0
0

Hello

I am programming with the C ++ Builder 2009 and with the function CopyFile or CopyFileW my problems and only if the paths and filenames are not entered as text (eg "C: \\ Program Files \\ .....") but as a UnicodeString variable. I have encountered special problems with using UnicodeString-Literals.

Here are some code examples:

{
    int Count = 0;
    bool succesful = false;
    UnicodeString Target = "C:\\Program Files\\HotelC32W7\\HotSave\\Di\\";
    UnicodeString Source;
    FileListBox1->Directory = "C:\\Program Files\\HotelC32W7\\\HotSave\\Mo";

    for (int i = 0; i < FileListBox1->Items->Count; i++) {
        try {
            Source = "C:\\Program Files\\HotelC32W7\\HotSave\\Mo\\" + FileListBox1->Items->Strings[i];
            const wchar_t *lpExistingFileName = Source.w_str();
            const wchar_t *lpNewFileName = Target.w_str();
            succesful = CopyFileW(TEXT(lpExistingFileName), TEXT(lpNewFileName), false);             //Variante 2
            if (succesful)
                Count++;
        }
        catch(...) { }

        if (!succesful)
            ShowMessage("Error: " + GetLastError());  //Message only: "or:"

        succesful = false;
    }
    Edit1->Text = IntToStr(Count);
}

Experiments with different variants

// no error message from the compiler but also no copies made

     succesful = CopyFileW(Source.w_str(), Target.w_str(), false);                            Variante 1
     succesful = CopyFileW(TEXT(lpExistingFileName), TEXT(lpNewFileName), false);    Variante 2
     succesful = wcscpy(Source.w_str(), Target.w_str());                                        Variante 3

// Can not convert const wchar_t * to const char *

     succesful = CopyFile(TEXT(lpExistingFileName), TEXT(lpNewFileName), false);    Variante 4
     succesful = wcscpy(TEXT(lpNewFileName), TEXT(lpNewFileName));                 Variante 5

Surprisingly, the function in variant 1 has once worked and over 100 files copied, but just once only! The error with the function GetLastError () has also brought no insights, so I'm looking at the specialists, who can help me out of the trouble.

Best regards

Gert

 


Viewing all articles
Browse latest Browse all 2504

Trending Articles