When translating error messages I get back a pointer to a string (pMsgBuf) that looks like this
pMsgBuf = "The stra...."
The message is there but separated by nulls. Its got to be a parameter that I am passing to Format Message, but have no idea how to fix it.
{
char* pMsgBuf;
// windows will allocate memory for err string and make our pointer point to it
const DWORD nMsgLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(&pMsgBuf), 0, nullptr
);
// 0 string length returned indicates a failure
if (nMsgLen == 0)
{
return "Unidentified error code";
}
char joe[100]{};
for (int i = 0, y = 0; i < nMsgLen *2; i++)
{
if (pMsgBuf[i] != '')
joe[y++] = pMsgBuf[i];
}
// copy error string from windows-allocated buffer to std::string
std::string errorString = pMsgBuf;
// free windows buffer
size_t size = sizeof(pMsgBuf);
LocalFree(pMsgBuf);
return errorString;
}
Test code with watch active
Should have gotten back my string pointer back with trailing not each character with a null.
Created a test block to confirm that was what I was getting back from format message.
char joe[100]{};
for (int i = 0, y = 0; i < nMsgLen *2; i++)
{
if (pMsgBuf[i] != '')
joe[y++] = pMsgBuf[i];
}
sure enough Joe the string comes out correctly.
Clark Dixon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.