Search code examples
c++windowsstring-conversion

How to drop control characters when using WideCharToMultibyte


I'm working on a Windows UI Automation client that interfaces with some legacy code. At the point where the legacy code is called I have to convert the LPWSTR to a char *, which works in most cases, but sometimes the input strings contain control characters (such as the invisible LTR control character), and WideCharToMultibyte always maps those characters to '?'.

Is it possible to drop those characters? Is there another function better suited to this purpose? Any help would be greatly appreciated!


Solution

  • Doesn't look like there's a single function that accomplishes this, so I'm using Mark Random's solution from the comments.

    Let lpDefaultChar point to a character that will never be in your string, such as 0x01, then remove those characters from the output. – Mark Ransom