0

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);
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
BILL
  • 4,711
  • 10
  • 57
  • 96
  • Besides all the rest, here is a more fundamental problem: you cannot rely on the spool folder being always in that location - for starters, the Windows directory may be on another drive. – Matteo Italia Dec 25 '14 at 12:09

2 Answers2

2

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.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
1
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.

Mohamed Ali
  • 173
  • 5
  • It would be good to explain why these lines of code resolve the OP question. – рüффп Dec 25 '14 at 10:35
  • 1
    std::wstring("test.SPL") **does not** work. In fact, that constructor does not exist in the standard library. Maybe you wanted to use `std::wstring(std::begin(myStr), std::end(myStr));`, or, more simply, just put an `L` in front of the string literals. Also, you're not consistent in your use of the `std` prefix, and you forgot to escape a backslash in the second string. Maybe it would be useful to mention the use of raw strings to avoid that last problem. – bogdan Dec 25 '14 at 12:02
  • Should UTF16 be UTF-16LE for Windows? – Marichyasana Dec 25 '14 at 12:07