Question

I'm trying to build a string from a substring which comes from an error message:

// the error message
const char* error_msg = e.what();

size_t const cchDest = 100;
TCHAR pszDest[cchDest]; 

LPCTSTR pszFormat = TEXT("%s %s");
TCHAR* pszTxt = TEXT("The error is: ");

HRESULT hr = StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, error_msg );

I was hoping that the second %s will be replaced with the value of error_msg, however the output is:

The error is: ☐☐a

How do I have to modify above code so that the substring shows up?

EDIT1 I've also tried the following, but all I get is a box again.

TCHAR* pszTxt = TEXT("The error is: %c", error_msg );
HRESULT hr = StringCchPrintf(pszDest, cchDest, pszTxt);
Was it helpful?

Solution

This works:

LPCTSTR pszFormat = TEXT("%s %hs");
TCHAR* pszTxt = TEXT("The error is: ");

HRESULT hr = StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, error_msg);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top