I'm relatively new to C++ and the Windows API (coming from a Java background) and I was just playing around with the Windows API calling MessageBox
and other simple functions until I tried to pass a concatenated string from a custom function to MessageBox
where I noticed a weird output in the generated window.
This is the suspicious function:
const char* addFoo(const char* strInput)
{
return ("foo-" + std::string(strInput)).c_str();
}
It just returns the original input with a foo-
added in front. (I hope I'm not doing anything incredibly wrong there)
In main I then do two calls to MessageBox
first without calling the function but instead doing all the calculation on the fly, and afterwards calling the function:
const char* a = "bar";
MessageBox(NULL, ("foo-" + std::string(a)).c_str(), "The Foobar Experiment", MB_OK);
MessageBox(NULL, addFoo(a), "The Foobar Experiment", MB_OK);
This is the result I get by doing the string concatenation on the fly (case 1):
The result I get by calling the function addFoo
(case 2):
Does anyone have any idea why I'm getting these unreadable characters on the generated window by using my addFoo function? Thanks in advance and sorry for the long post.