c++ - Python subprocess directing data to standard intput -
i have c++ program on windows reads characters in standard input. want write python script opens c++ program , have script write programs standard input.
i can create subprocess in python , read standard out. however, program fails receive standard in python script. program uses readconsole() read standard in , repeatedly returns error code 6 (invalid handle) though getstdhandle() returns without error.
here programs code:
char buffer[gide_buffer_size]; handle hconsole_c = getstdhandle(std_input_handle); dword chars_read = 0; if(hconsole_c == invalid_handle_value ) { gide_printf(log_err,"error: invalid_handle_value stdout: %d.", getlasterror()); fflush(stdout); keyboard_handler_running = false; main_thread_running = false; } else if( hconsole_c == null) { gide_printf(log_err,"error: unable handle standard output."); fflush(stdout); keyboard_handler_running = false; main_thread_running = false; } gide_printf(log_debug,"keyboard_listener thread started."); sleep(500); //sleep give time come up. print_menu(); memset(buffer, 0, sizeof(buffer)); //reads characters console after enter pressed. //enter key adds cr , lf adds 2 chars output. while(keyboard_handler_running) { if( readconsole( hconsole_c, buffer, sizeof(buffer), &chars_read, null ) == 0) { gide_printf(log_err,"error: reading console failed: %d.", getlasterror()); errorhandler("blah"); continue; } gide_printf(log_debug,"read %d chars console.", chars_read); . . . . here python script:
import time import subprocess subprocess import popen, pipe, stdout print '0' proc = subprocess.popen('program.exe', stdout=none, stdin=subprocess.pipe, stderr=subprocess.pipe, shell=false) time.sleep(2) print '1' proc.stdin.write('xtyasmdmdjmdhjmdmjdmjd\n') time.sleep(2) print '2' proc.stdin.close() proc.stdout.close() proc.kill() msdn mentions following: although readconsole can used console input buffer handle, readfile can used other handles (such files or pipes). readconsole fails if used standard handle has been redirected other console handle. http://msdn.microsoft.com/en-us/library/windows/desktop/ms684958%28v=vs.85%29.aspx
i"m wondering if has it.
if has suggestions on how go doing this, or better way with python, let me know.
thanks.
i figured out workaround gives insight nature of problem. i'm working in visual studio using windows console project. created new "blank project" , instead of using windows api of readconsole() used c++ api of std::cin.getline(). fixes problem.
once again, isn't solution as workaround. indicates problem seems windows api or visual studio project settings.
Comments
Post a Comment