title: How to add a path with spaces as an arguments to CreateProcess batch file?
body: <p>I am trying to pass an argument with a space in i to a batch file I run via CreateProcess(). How do I specify that the entire object is an argument?</p>
  
  <pre><code>std::wstring args = TEXT("/C \"C:\\setup.bat\" C:\\TEST TEST");
  </code></pre>
  
  <p>In the example above, my batch file reads the first argument as C:\TEST.</p>
  
  <p>And, this does not work (batch file exits immediately and does not run):</p>
  
  <pre><code>std::wstring args = TEXT("/C \"C:\\setup.bat\" \"C:\\TEST TEST\"");
  </code></pre>
  
  <p>Here is the entire code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #define WINDOWS_LEAN_AND_MEAN
  #include &lt;Windows.h&gt;
  
  #include &lt;strsafe.h&gt;
  #include &lt;string&gt;
  #include &lt;UserEnv.h&gt;
  #include &lt;vector&gt;
  
  #define BUFSIZE 4096
  
  #pragma comment(lib, "userenv.lib")
  
  std::wstring GetEnvString()
  {
      wchar_t* env = GetEnvironmentStrings();
      if (!env)
      {
          abort();
      }
  
      const wchar_t* var = env;
      size_t total_len = 0;
      size_t len;
  
      while ((len = wcslen(var)) &gt; 0)
      {
          total_len += len + 1;
          var += len + 1;
      }
  
      std::wstring result(env, total_len);
      FreeEnvironmentStrings(env);
      return result;
  }
  int main()
  {
      LPVOID env;
  
      if (!CreateEnvironmentBlock(&amp;env, NULL, FALSE))
      {
          std::cout &lt;&lt; "FAILURE" &lt;&lt; std::endl;
          system("PAUSE");
          abort();
      }
  
      PROCESS_INFORMATION pi;
      memset(&amp;pi, 0, sizeof(pi));
  
      STARTUPINFO si;
      memset(&amp;si, 0, sizeof(si));
      si.cb = sizeof(si);
  
      std::wstring program = TEXT("C:\\Windows\\System32\\cmd.exe");
      std::wstring args = TEXT("/C");
  
      args.append(TEXT(" \"C:\\setup.bat\""));
      args.append(TEXT(" C:\TEST TEST"));
  
      std::vector&lt;wchar_t&gt; buf(args.begin(), args.end());
      buf.push_back(0);
  
      if (!CreateProcess(program.c_str(), buf.data(), NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, env, NULL, &amp;si, &amp;pi))
      {
          std::cout &lt;&lt; "FAILURE" &lt;&lt; std::endl;
          system("pause");
          abort();
      }
  
      WaitForSingleObject(pi.hProcess, INFINITE);
      CloseHandle(pi.hProcess);
      CloseHandle(pi.hThread);
      DestroyEnvironmentBlock(env);
  
  
      if (!CreateEnvironmentBlock(&amp;env, NULL, FALSE))
      {
          std::cout &lt;&lt; "FAILURE" &lt;&lt; std::endl;
          abort();
      }
  
      return 0;
  }
  </code></pre>