Reputation: 720
I'm having trouble writing a WString to the STDIN of a child process. If I have only acii character string (eg: @WSX3edc), the code works fine, but if it contains a non ascii character (eg: @WSX3edcß) it fails.
The child process is 7zr.exe (7Zip cmd line version). The input I'm writing to the STDIN is the password to extract the file.
// inject password
wPassword.append(password);
wPassword.append(L"\n"); \\For carriage return
...
DWORD dwBytesToWrite = wPassword.length()*sizeof(wchar_t);
DWORD dwBytesWritten = 0;
char szBuffer[1024] = "\0";
wcstombs(szBuffer, wPassword.c_str(),wcslen(wPassword.c_str())+1);
dwBytesToWrite = strlen(szBuffer);
if (!WriteFile(hInput, szBuffer, dwBytesToWrite, &dwBytesWritten, NULL)) {
std::cout<<"write file failed"<<GetLastError()<<std::endl;
goto Cleanup;
}
The writefile always succeed but some how the file extraction is not successful due to faulty password injecting mechanism.
Createprocess for this looks like : (si object has the STDIN and STDOUT streams set using a CreatePipe earlier)
if(!CreateProcess((LPWSTR)cmd, (LPWSTR)cmdArgs, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS,
NULL, NULL, &si, &pi)) {
std::cout<<"7zr.exe process creation failed "<<GetLastError()<<std::endl;
goto Cleanup;
}
Note : 7zr.exe works just fine with this particular password, if we run it on command-line and paste this password. The extraction works fine.
Upvotes: 0
Views: 256
Reputation: 120021
wcslen(wide)
returns the number of wide characters in its argument wide
(see).
wcstombs(narrow,wide,len)
writes no more than len
bytes to narrow
(see).
Now if we always had one wide character = one narrow character = one byte, it wouldn't have much sense to have two varieties of characters, would it?
As soon as you have a wide character that translates to more than one narrow character, there is undefined behaviour.
Since your szBuffer
is of fixed size, you could just as well write
wcstombs(szBuffer, wPassword.c_str(), sizeof(szBuffer));
Upvotes: 1
Reputation: 145359
If the narrow character set doesn't have the relevant password character, you can't use this approach. Instead find what option 7zr
has for specifying the password. I don't have an executable called 7zr
but I do have 7z
, and the command 7z | find /i "pass"
worked nicely.
In other news:
The variable dwBytesToWrite
is initialized with one value, only to be reassigned a few lines later, without having been used.
goto Cleanup
is generally ungood in C++. If you want guaranteed cleanup use a destructor (the technique called RAII, read up on it).
Microsoft's Hungarian notation, with prefixes such as sz
and dw
, is generally an abomination. It once, in the 1980's, supported the help system in Microsoft's Programmer's Workbench. AFAIK that product hasn't existed the last 30 years or so.
The C cast in (LPWSTR)cmd
can easily introduce a bug. Use const_cast
where you want to cast constness. Then it would be more clear that this cast is incorrect: you need a mutable buffer.
Instead of reporting a failure to the standard output stream, via std::cout
, consider using the standard error stream, via either std::cerr
or std::clog
. Better, don't do i/o at the place where a failure is detected, but throw an exception to let the calling code deal with it. The calling code can't remove output that's already, uh, output.
Upvotes: 2