Beispiel #1
0
void
ACE_Process_Options::inherit_environment (void)
{
  // Ensure only once execution.
  if (environment_inherited_)
    return;
  environment_inherited_ = 1;

  // Get the existing environment.
  ACE_TCHAR *existing_environment = 0;
#if defined (ACE_HAS_WCHAR) && !defined (ACE_USES_WCHAR)
  WCHAR *existing_wide_env = 0;
  ACE_Vector<char> temp_narrow_env;
  if (this->use_unicode_environment_)
    {
      existing_wide_env = ::GetEnvironmentStringsW ();
      for (WCHAR *iter = existing_wide_env; *iter; ++iter)
        {
          ACE_Wide_To_Ascii wta (iter);
          size_t len = ACE_OS::strlen (wta.char_rep ());
          size_t idx = temp_narrow_env.size ();
          temp_narrow_env.resize (idx + len + 1, 0);
          ACE_OS::strncpy (&temp_narrow_env[idx], wta.char_rep (), len);
          iter += len;
        }
      temp_narrow_env.push_back (0);
      existing_environment = &temp_narrow_env[0];
    }
  else
#endif
  existing_environment = ACE_OS::getenvstrings ();

  size_t slot = 0;

  while (existing_environment[slot] != '\0')
    {
      size_t len = ACE_OS::strlen (existing_environment + slot);

      // Add the string to our env buffer.
      if (this->setenv_i (existing_environment + slot, len) == -1)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p.\n"),
                      ACE_TEXT ("ACE_Process_Options::ACE_Process_Options")));
          break;
        }

      // Skip to the next word.
      slot += len + 1;
    }

#if defined (ACE_HAS_WCHAR) && !defined (ACE_USES_WCHAR)
  if (this->use_unicode_environment_)
    ::FreeEnvironmentStringsW (existing_wide_env);
  else
#endif
  ACE_TEXT_FreeEnvironmentStrings (existing_environment);
}
Beispiel #2
0
static void
win32_spawn_environment_process (void)
{
    PROCESS_INFORMATION process_info;
    ACE_TEXT_STARTUPINFO startup_info;
    ACE_OS::memset ((void *) &startup_info,
                    0,
                    sizeof startup_info);
    ACE_OS::memset ((void *) &process_info,
                    0,
                    sizeof process_info);
    startup_info.cb = sizeof (startup_info);
    startup_info.dwFlags = STARTF_USESTDHANDLES;

    ACE_HANDLE std_in = ACE_STDIN;
    ACE_HANDLE std_out = ACE_STDOUT;
    ACE_HANDLE std_err = ACE_STDERR;

    if (!::DuplicateHandle (::GetCurrentProcess(),
                            std_out,
                            ::GetCurrentProcess(),
                            &startup_info.hStdOutput,
                            0,
                            TRUE,
                            DUPLICATE_SAME_ACCESS))
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%p duplicate failed.\n"),
                    ACE_TEXT ("spawn_environment_process")));
        return;
    }

    if (!::DuplicateHandle (::GetCurrentProcess(),
                            std_err,
                            ::GetCurrentProcess(),
                            &startup_info.hStdError,
                            0,
                            TRUE,
                            DUPLICATE_SAME_ACCESS))
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%p duplicate failed.\n"),
                    ACE_TEXT ("spawn_environment_process")));
        return;
    }

    if (!::DuplicateHandle (::GetCurrentProcess(),
                            std_in,
                            ::GetCurrentProcess(),
                            &startup_info.hStdInput,
                            0,
                            TRUE,
                            DUPLICATE_SAME_ACCESS))
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%p duplicate failed.\n"),
                    ACE_TEXT ("spawn_environment_process")));
        return;
    }

    // Normally, this would be just GetEnvironmentStrings, but it
    // doesn't follow the same rules as the rest of the Win32 API
    ACE_TCHAR *existing_environment = ACE_OS::getenvstrings ();
    ACE_TCHAR environment[10240];
    ACE_OS::sprintf (environment,
                     ACE_TEXT("ACE_PROCESS_TEST=%s"),
                     environment_string);

    int size = 0;
    while (existing_environment[size] != '\0')
        size +=
            ACE_Utils::truncate_cast<int> (
                ACE_OS::strlen (existing_environment + size) + 1);

    ACE_OS::memcpy (environment + (ACE_OS::strlen (environment) + 1),
                    existing_environment,
                    size);

    ACE_TEXT_FreeEnvironmentStrings (existing_environment);

    ACE_TCHAR cmd_line[16];
    ACE_OS::strncpy (cmd_line, ACE_TEXT ("process -g"), sizeof (cmd_line));
    BOOL fork_result =
        ACE_TEXT_CreateProcess (ACE_TEXT ("d:\\harrison\\ACE_wrappers\\examples\\OS\\Process\\process.exe"),
                                cmd_line,
                                0, // No process attributes.
                                0, // No thread attributes.
                                TRUE, // Allow handle inheritance.
                                0, // CREATE_NEW_CONSOLE, // Create a new console window.
                                environment, // Environment.
                                //"d:\\harrison\\ACE_wrappers\\examples\\OS\\Process\\",
                                0,
                                &startup_info,
                                &process_info);

    ::CloseHandle (startup_info.hStdOutput);
    ::CloseHandle (startup_info.hStdError);

    if (fork_result == 0)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%p.\n"),
                    ACE_TEXT ("spawn_environment_process")));
    else
    {
        ::WaitForSingleObject (process_info.hProcess,
                               INFINITE);
        ACE_DEBUG ((LM_ERROR,
                    ACE_TEXT ("spawn_environment_process succeeded.\n")));
    }
}