FormatMessageV() of CString replaces \n with \r\n

119 Views Asked by At

There is a function called as FormatMessageV which is part of CString class that is supposed to 'Format a message string using a variable argument list' according to MSDN. Other than the said operation unfortunately it also replaces \n in the message with \r\n which I don't want.

I tried to pass the ASCII value of LF instead of \n in the message but not surprisingly that didn't alter the routine's behaviour. I couldn't come across anything online which mentions about why this happens and how to prevent it.

Does any of you good folks here have any idea about this? Following is a minimum reproducible example, unfortunately, I couldn't find any free online Microsoft Visual C++ compiler to run the sample and link, but it prints Has CR in it! on my machine in the Visual Studio C+ project.

#include <atlstr.h>
#include <iostream>
 
CString FormatV(LPCTSTR first, ...)
{
 
    va_list vaList = nullptr;
    CString buffer;
    va_start(vaList, first);
    buffer.FormatMessageV(first, &vaList); //this adds \r before \n
    va_end(vaList);
 
    return buffer;
}
 
 
int main() 
{
    CString text = L"\nint Fun(LPCTSTR commandLine, UINT number)\n{ Perform(commandLine,% 1); \n }"; // Notice that it has only \n s
    CString formattedText = FormatV(text, 1); //formattedText has \r\n s
    if (formattedText.Find(L'\r', 0) != -1)
        std::cout << "Has CR in it!";
}
0

There are 0 best solutions below