How to concatenate char* and LPWSTR string?

2.1k Views Asked by At

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

There are 2 best solutions below

3
On BEST ANSWER
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.

0
On

Just for clarification, LPWSTR is a typedef for wchar_t*. You can use wcscat_s to conctenate strings of this form. Your one char* string should just be changed to be of the same type, since you have it there as a simple literal (just prefix the literal with L and change the declared type). Since you tagged this as C++, however, you can do all of this more simply by using the std::wstring class.