title: How do I pass an std::string environment block to CreateProcess?
body: <p>I'm currently trying to use <a href="http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682425%28v=vs.85%29.aspx" rel="nofollow">CreateProcess </a> with the Path, Arguments and Environment Variables. My variables are stored in strings.</p>
  
  <p>In the below example filePath and cmdArgs work fine, but I cannot get the envVars to work.</p>
  
  <pre><code>std::string filePath = "C:\\test\\DummyApp.exe";
  std::string cmdArgs  = "Arg1 Arg2 Arg3";
  std::string envVars  = "first=test\0second=jam\0";  // One
  
  //LPTSTR testStr = "first=test\0second=jam\0";      // Two
  
  CreateProcess(
     LPTSTR(filePath.c_str()),           //path and application name
     LPTSTR(cmdArgs.c_str()),            // Command line
     NULL,                               // Process handle not inheritable
     NULL,                               // Thread handle not inheritable
     TRUE,                               // Set handle inheritance
     0,                                  // Creation flags
     LPTSTR(envVars.c_str()),            // environment block
     //testStr                      //this line works
     NULL,                               // Use parent's starting directory 
     &amp;si,                                // Pointer to STARTUPINFO structure
     &amp;pi )                               // Pointer to PROCESS_INFORMATION structure
  </code></pre>
  
  <p>) </p>
  
  <p>When I run this code the error that comes back is "error 87: The parameter is incorrect".</p>
  
  <p>What I don't understand is that if I comment out the line labeled "one" and replace it with the line labeled "two" (and make the matching swap in the function call) then it works correctly.</p>