Menu

#1 Windows implementation does not build in Unicode projects

open
nobody
None
5
2008-06-24
2008-06-24
No

Create a Unicode project in Visual Studio (or by hand). Add exec-stream.cpp to the project. Compile. You get a bunch of errors, but they're all related to two incorrect uses of TCHAR.

In MBCS projects, TCHAR is defined as char. But in Unicode projects, TCHAR is #defined as WCHAR, which is ultimately either wchar_t or unsigned short.

The first problem is in os_error_t::compose. The TCHAR* strings returned by FormatMessage are implicitly cast to char* to be added to a std::string. In Unicode projects, this is an error. The easy fix is to explicitly call FormatMessageA and use char* strings.

The second problem is in exec_stream_t::start, where the contents of a std::string are passed to CreateProcess as a char*, where a TCHAR* is expected. Again, in Unicode projects, this is an error. Again, the easy fix is to explicitly call CreateProcessA (which means you also need a STARTUPINFOA struct rather than a STARTUPINFO).

On top of that, the MSDN page for CreateProcess explicitly says: "The Unicode version of this function, CreateProcessW, will fail if this parameter is a const string." Const-casting away the constness of the string is a very bad idea here. But the "ANSI" version, CreateProcessA, has no such problem.

Discussion

  • Andrew Barnert

    Andrew Barnert - 2008-06-24

    Patch to fix both of the Unicode problems

     
  • Andrew Barnert

    Andrew Barnert - 2008-06-24

    Logged In: YES
    user_id=1473180
    Originator: YES

    File Added: diff

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.