FormatMessage "Access violation reading location (ucrtbased.dll)" on specific languages

youki 1,021 Reputation points
2023-10-13T11:52:30.25+00:00

I wanted to show an error message in several languages but if I do not add the main languages (like English [US], Spanish [Spain], German [Germany]) it throws the error.

https://renenyffenegger.ch/notes/Windows/development/Internationalization/language

I tested it with adding the language code directly as parameter and also with the MAKELANGID makro.

Is it because the other languages are not supported (like Spanish [Chile], German [Luxembourg)?

(I am setting the language by SetThreadUILanguage and it always automatically takes the for example German neutral string table for all german "languages". I thought I could do the same with the error messages: I set any german language and would get the german error message by FormatMessage.)

PS: I also installed the corresponding language packages. Unfortunately, it does not work with the sub lang id.

DWORD errorCode = GetLastError(); 	
LANGID  langId = GetThreadUILanguage(); 	
LPWSTR errorMsg = nullptr; 	
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errorCode, 1031, (LPWSTR)&errorMsg, 0, nullptr); 	// Get error message. 
Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. youki 1,021 Reputation points
    2023-10-15T23:45:11.2966667+00:00

    OK, thank you. I've done a lot of testing and research and I think it's best to create your own error message + the error code. If the mui is present, the message is added from FormatMessage. At least it seems to me that it's best if you want to have a number of languages. There are probably cases where the user can't figure it out and neither can the admin because he can't get any further without the error code because FormatMessage is in a foreign language.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.