c++windowsstring-concatenationlpwstr

How to concatenate char* and LPWSTR string?


I want to use MoveFile function, this function use two LPWSTR arguments, but I have one char* and LWSTR, how to concatenate them?

//move file
    LPWSTR latestFile = L"test.SPL";
    char*  spoolFolder = "C:\\Windows\\System32\\spool\PRINTERS\\";
    LPWSTR fileToMove = spoolFolder + latestFile;
    BOOL moved = MoveFile(latestFile, L"C:\\UnprocessedFiles\\" + latestFile);

Solution

  • std::wstring latestFile = wstring("test.SPL");
    std::wstring spoolFolder = wstring("C:\\Windows\\System32\\spool\PRINTERS\\");
    std::wstring fileToMove = spoolFolder + latestFile; 
    BOOL moved = MoveFile(latestFile.c_str(), fileToMove.c_str());
    

    In deed, LPWSTR is just a typdef for w_char*. so if you consult MSDN you will see that:

    typded wchar_t* LPWSTR;
    

    here the w_char* means that your string will be encoded as UNICODE not ANSI scheme. So under windows a UNICODE string will be an UTF16 one ( 2 bytes for each char).

    std::wstring is also a typedef for std::basic_string<wchar_t,char_traits<>> so by declaring your inputs as wstring and calling wasting.c_str() this will do the stuff.