FormatMessage is not always providing the correct output - Certain values being duplicated - why?

107 Views Asked by At

STRINGTABLE Item:

  • ID: IDS_TPL_SWAP_CONFIRMATION
  • Value: 5149
  • Caption: Swap assignments:\n\n%1 - %2 [%3]\n%4 - %5 [%6]\n\nPlease confirm.

Sometimes when I try this code:

CString strPrompt = _T("");
strPrompt.FormatMessage(IDS_TPL_SWAP_CONFIRMATION,
    m_pEntry->GetMeetingDateAsString(),
    (LPCTSTR)strExistingName,
    (LPCTSTR)strExistingDescription,
    pReplacement->GetMeetingDateAsString(),
    (LPCTSTR)getter(pReplacement),
    (LPCTSTR)strReplacementDescription);

I was finding on occasion that the %1 value was being duplicated for the %4 value, even though pReplacement->GetMeetingDateAsString() was actually a different date.

In the end I have changed the code like this:

strPrompt.LoadString(IDS_TPL_SWAP_CONFIRMATION);
strPrompt.Replace(_T("%1"), m_pEntry->GetMeetingDateAsString());
strPrompt.Replace(_T("%2"), strExistingName);
strPrompt.Replace(_T("%3"), strExistingDescription);
strPrompt.Replace(_T("%4"), pReplacement->GetMeetingDateAsString());
strPrompt.Replace(_T("%5"), getter(pReplacement));
strPrompt.Replace(_T("%6"), strReplacementDescription);

This works for me guaranteed. I am using the FormatMessage syntax because my application supports over 50languages and I could not be sure that each language would want the data in the same order.

As mentioned, all data is correct is except in some instances it would re-use the first date for the fourth item in the final output. Are there any issues I need to know about with FormatString?

My workaround is working fine for me in the meeting time.

0

There are 0 best solutions below