コード例 #1
0
ファイル: win32_ops.hpp プロジェクト: Flusspferd/flusspferd
/** 
 * Converts an environment to a string used by CreateProcess(). 
 * 
 * Converts the environment's contents to the format used by the 
 * CreateProcess() system call. The returned char* string is 
 * allocated in dynamic memory; the caller must free it when not 
 * used any more. This is enforced by the use of a shared pointer. 
 * 
 * \return A dynamically allocated char* string that represents 
 *         the environment's content. This string is of the form 
 *         var1=value1\\0var2=value2\\0\\0. 
 */ 
inline boost::shared_array<char> environment_to_win32_strings(const environment &env) 
{ 
    boost::shared_array<char> envp; 

    if (env.empty()) 
    { 
        envp.reset(new char[2]); 
        ::ZeroMemory(envp.get(), 2); 
    } 
    else 
    { 
        std::string s; 
        for (environment::const_iterator it = env.begin(); it != env.end(); ++it) 
        { 
            s += it->first + "=" + it->second; 
            s.push_back(0); 
        } 

        envp.reset(new char[s.size() + 1]); 
#if defined(__CYGWIN__) || defined(_SCL_SECURE_NO_DEPRECATE) 
        ::memcpy(envp.get(), s.c_str(), s.size() + 1); 
#else 
        ::memcpy_s(envp.get(), s.size() + 1, s.c_str(), s.size() + 1); 
#endif 
    } 

    return envp; 
}