CreateProcess () does not create a new window with the flag CREATE_NEW_CONSOLE - C / C ++

I am trying to create a process using CreateProcess () using, of course, the Windows API. For some reason, I could not even create a new console even after cleaning the network.

Repeat Search:

I used the MSDN code example as a base for the parameters that I should use in the function:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx

I read the following MSDN article for information on how to create new console windows:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528%28v=vs.85%29.aspx

I also read a similar SO problem to find out about someone having the same problem:

Does CreateProcess not create additional console windows under Windows 7?

Results:

I wrote the code, which I posted below, with all the requirements necessary to create a new console, but does not behave as expected. For a long time I tried to find the answer myself, but the above articles were the only relevant ones that I could find through Google. It happens that a process is created, but it is inside my C program console. I want to be able to create a process without inheriting my program console.

There are other discrepancies. If I print a lot of characters in my do-while loop without Sleep () to slow it down, TerminateProcess () will fail with access and will fail when you press the escape key. This is also an undesirable behavior.

Here is the C program that I have right now:

#include <stdio.h> #include <time.h> #include <stdlib.h> #define WIN32_LEAN_AND_MEAN #include <process.h> #include <windows.h> #define IS_PRESSED( vk ) ( GetAsyncKeyState( vk ) & 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->s_info.cb = sizeof( STARTUPINFO ); p->s_info.dwFlags |= CREATE_NEW_CONSOLE; return CreateProcess( exe_path, ( LPSTR )cmd_line_args, NULL, NULL, FALSE, 0, NULL, NULL, &p->s_info, &p->p_info ); } int main( ) { process p = { { 0 }, { 0 } }; srand( time( NULL ) ); if ( !create_process( &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; } 

Here is the batch program that I call:

 @echo off setlocal enabledelayedexpansion :start echo Hello PSAPI on Windows... pause >nul exit 

I expect someone to know how to handle processes more than I do. This is my first time using the CreateProcess () function. Yes, I know ShellExecute (). I also know that my batch file is not a matrix, but I wanted to start simply.

+6
source share
1 answer

CREATE_NEW_CONSOLE is a flag of CreateProcess() , not STARTUPINFO . You put the flag in the wrong place. Try instead:

 int create_process( process * p, const char * exe_path, const char * cmd_line_args ) { ... return CreateProcessA( exe_path, cmd_line_args, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, // <-- here NULL, NULL, &p->s_info, &p->p_info ); } 

Also keep in mind that STARTUPINFOEX can be passed to CreateProcess() , so your create_process() function should not force p->s_info.cb , this should be the responsibility of the caller depending on whether there is STARTUPINFO or STARTUPINFOEX .

+10
source

All Articles