The problem is that you are mixing std::string
and std::wstring
data together, and doing so incorrectly.
std::wstringstream
does not have an operator<<
for char*
input, so you are actually invoking its operator<<
for void*
input instead. That operator formats the value of the pointer itself, not the data that it is pointing at. So you end up passing bad input to ToolUpdater.exe
.
You would have caught this error at compile time if you were not using c_str()
so much. Don't use c_str()
unless you really need a raw character pointer (like in the ShellExecute()
parameters). If you avoid raw pointers, the compiler can perform better type-safe validations for you:
std::string szArg = " -i " + m_strSign;
std::wstringstream wss;
wss << szArg; // ERROR!
std::wstringstream wssa;
wssa << strToolupdater; // ERROR!
ShellExecute(NULL, TEXT("open"), wssa.str().c_str(), wss.str().c_str(), TEXT(""), SW_NORMAL);
std::wcout << wssa.str() << wss.str() << std::endl;
That being said, you really don't need the std::wstringstream
objects in this example, you can remove them completely.
If you call ShellExecuteA()
instead, you can pass your std::string
values to it like this:
std::string szArg = "-i " + m_strSign;
ShellExecuteA(NULL, "open", strToolupdater.c_str(), szArg.c_str(), NULL, SW_NORMAL);
std::cout << strToolupdater << " " << szArg << std::endl;
Or, if you change everything to std::wstring
:
std::wstring szArg = L"-i " + m_strSign;
ShellExecuteW(NULL, L"open", strToolupdater.c_str(), szArg.c_str(), NULL, SW_NORMAL);
std::wcout << strToolupdater << L" " << szArg << std::endl;
If you cannot change your m_strSign
and strToolupdater
variables themselves to std::wstring
(because other code still expects std::string
), you can convert them to std::wstring
temporarily when needed:
std::wstring toWStr(const std::string &s)
{
std::wstring ret;
int len = MultiByteToWideChar(0, 0, s.c_str(), s.length(), NULL, 0);
if (len > 0)
{
ret.resize(len);
MultiByteToWideChar(0, 0, s.c_str(), s.length(), &ret[0], len);
}
return ret;
}
std::wstring szToolUpdater = toWStr(strToolupdater);
std::wstring szArg = L"-i " + toWStr(m_strSign);
ShellExecuteW(NULL, L"open", szToolUpdater.c_str(), szArg.c_str(), NULL, SW_NORMAL);
std::wcout << szToolUpdater << L" " << szArg << std::endl;