Jonny Dens
Jonny Dens

Reputation: 49

Passing boost::filesystem::path to boost::process::child causes exception on Windows

Example code below causes exception on Windows:

#include <boost/filesystem.hpp>
#include <boost/process.hpp>

using namespace boost::filesystem;
using namespace boost::process;

int main()
{
  child child(exe = search_path("app", { current_path() / "app_dir" }),
              args = { "--help" });

  child.wait();

  return 0;
}

But this one is not:

#include <boost/filesystem.hpp>
#include <boost/process.hpp>

using namespace boost::filesystem;
using namespace boost::process;

int main()
{
  child child(search_path("app", { current_path() / "app_dir" }).string(),
              "--help");

  child.wait();

  return 0;
}

This is the exception message:

boost codecvt to wchar_t: error

I found some information about codecvt in Boost documentation:

Since windows does not use UTF-8 it is sometimes unavoidable to use the wchar_t version of the WinApi. To keep this library consistent it provides wchar_t support on posix also.

Since the posix api is purely char every wchar_t based type will be converted into char.

Windows on the other hand is more selective; the default is to use char, but if any parameter requires wchar_t, everything will be converted to wchar_t. This also includes boost::filesystem::path. Additionally, if the system does not provide the char api (as is the case with Windows CE) everything will also be converted.

But I think that I am using child process properly in both cases. Can you tell me what I am doing wrong?

Upvotes: 2

Views: 744

Answers (1)

hipower
hipower

Reputation: 31

I just had this nasty behaviour on Win32 crop up when the user has a non-ASCII username (regardless of the path of the parent process).

Only safe solution I could find is to always pass the child path and all flags as wchar_t strings.

Upvotes: 0

Related Questions