Reputation: 4428
I have a legacy Windows application that logs an error. It would be nice to find what kind of error is thrown, but after finding the source code for the project I realize this is C++ and I have virtually no experience with C++.
The error code is a DWORD
, and the input to the log function takes a LPCTSTR
.
The code is as follows right now:
DWORD dwError;
dwError = SomeFunction();
if (dwError)
{
Log(_T("Something failed"));
}
So I would like to have the value of dwError
put into the log message. Coming from a C# background I tried Log(_T("Something failed (" + dwError + ")"));
but that obviously didn't work.
I have also tried reading up on concatenating LPCTSTR
values. I did find some hits but they seem very complicated and I don't know how to knead them into something for this legacy code.
How can I get dwError
to be a part of the log message, preferably in HEX notation.
Upvotes: 0
Views: 459
Reputation: 30605
The question is tagged C++, but the code is and question is written in a C style - the solutions in each vary;
For C++, make use of std::basic_string
;
DWORD dwError;
dwError = SomeFunction();
if (dwError)
{
std::basic_string<TCHAR> msg(_T("Something failed: "));
msg += std::to_string(dwError);
Log(msg.c_str());
}
A more C style would be;
DWORD dwError;
dwError = SomeFunction();
if (dwError)
{
TCHAR buffer[1024] = {};
_stprintf(buffer, _T("Something failed: %u"), dwError);
Log(buffer);
}
The documentation for the sprintf
links to the format specifiers, and for HEX, it would be %X
.
_stprintf(buffer, _T("Something failed: %X"), dwError);
Upvotes: 3