title: CreateProcess( ) doesn't create a new window with CREATE_NEW_CONSOLE flag - C/C++
body: <p>I've been trying to create a process with CreateProcess() using the Windows API  of course. I haven't been able to create a new console for some reason even after scouring the web. </p>
  
  <p><strong>Reasearch I've Done :</strong></p>
  
  <p>I used the MSDN example code as a base for the parameters I should use in the function :</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx</a></p>
  
  <p>I read the following MSDN article for information on how you should create new console windows :</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528%28v=vs.85%29.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528%28v=vs.85%29.aspx</a></p>
  
  <p>I also read a similar SO problem to mine about someone having the same problem :</p>
  
  <p><a href="https://stackoverflow.com/questions/14958276/createprocess-does-not-create-additional-console-windows-under-windows-7">CreateProcess does not create additional console windows under Windows 7?</a></p>
  
  <p><strong>Results :</strong></p>
  
  <p>I've written the code I will post below with all the requirements needed to create a new console, but it doesn't behave as expected. I've spent a long time trying to find the answer on my own, but the articles above were the only relevant ones I could find through google. What happens is that the process is created, but it is inside my C program's console. I want to be able to create the process without it inherting my program's console. </p>
  
  <p>There are also other discrepancies as well. If I print lots of characters in my do-while loop without a Sleep() to slow it down, TerminateProcess() will fail with Access Denied and the program will crash when I press the escape key. This is also not desired behavior.</p>
  
  <p>Here is the C program that I have right now :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define WIN32_LEAN_AND_MEAN
  #include &lt;process.h&gt;
  #include &lt;windows.h&gt;
  
  #define IS_PRESSED( vk ) ( GetAsyncKeyState( vk ) &amp; 0x8000 )
  
  typedef struct process
  {
      PROCESS_INFORMATION p_info;
      STARTUPINFO s_info;
  } process;
  
  void win_error( char * message, int is_exit )
  {
  
      char buffer[BUFSIZ] = { 0 };
      DWORD error_code = GetLastError( );
  
      FormatMessage
      (
          FORMAT_MESSAGE_FROM_SYSTEM,
          NULL,
          error_code,
          MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
          ( LPTSTR ) buffer,
          BUFSIZ,
          NULL
      );
  
      MessageBox( NULL, buffer, message, MB_ICONWARNING | MB_OK );
  
      if ( is_exit ) exit( error_code );
  
      return;
  }
  
  int create_process( process * p, const char * exe_path, const char * cmd_line_args )
  {
      p-&gt;s_info.cb = sizeof( STARTUPINFO );
      p-&gt;s_info.dwFlags |= CREATE_NEW_CONSOLE;
  
      return CreateProcess(
                              exe_path,
                              ( LPSTR )cmd_line_args,
                              NULL,
                              NULL,
                              FALSE,
                              0,
                              NULL,
                              NULL,
                              &amp;p-&gt;s_info,
                              &amp;p-&gt;p_info
                          );
  }
  
  int main( )
  {
      process p = { { 0 }, { 0 } };
  
      srand( time( NULL ) );
  
      if ( !create_process( &amp;p, "J:\\C programs and compiliers\\C\\WindowsTest\\bin\\Debug\\matrix.bat", NULL ) )
          win_error( "CreateProcess", 1 );
  
      CloseHandle( p.p_info.hThread );
  
      do
      {
          if ( IS_PRESSED( VK_ESCAPE ) )
              if ( !TerminateProcess( p.p_info.hProcess, 0 ) )
                  win_error( "TerminateProcess", 0 );
  
          Sleep( 50 );
  
      } while ( WaitForSingleObject( p.p_info.hProcess, 0 ) != WAIT_OBJECT_0 );
  
      CloseHandle( p.p_info.hProcess );
  
      return 0;
  }
  </code></pre>
  
  <p>Here is the Batch program I'm calling :</p>
  
  <pre><code>@echo off
  setlocal enabledelayedexpansion
  
  :start
  echo Hello PSAPI on Windows...
  pause &gt;nul
  exit
  </code></pre>
  
  <p>I'm expecting someone will know how to mess with processes more than I do. This is my first time using the CreateProcess() function. Yes, I am aware of ShellExecute(). I am also aware that my Batch file isn't a matrix, but I wanted to start simple.</p>