Python文件读取无法在Windows子进程

时间:2015-04-24 08:46:42

标签: python c++ process child-process

我有一个简单的python脚本,它读取文件的行,并按预期工作:

fn = "c:\\tempfile.txt"
f = open(fn, 'r')
print "f", f
lines = f.readlines()
print lines

但是,当我使用C ++的Windows CreateProcess()调用此脚本时,它不起作用。

print lines命令导致:

[]

根据第一个print语句,文件似乎在两种情况下都“打开”。关于这里出了什么问题的任何想法?

下面是我用来从C ++启动进程的代码。我从Stackoverflow或MSDN

获取它
#ifndef WINPROC_H_
#define WINPROC_H_

#include <stdio.h>

#include <string>
#include <vector>
#include <windows.h>
void WinProc(std::string argv) {
    std::vector<char> str(argv.begin(), argv.end());
    str.push_back('\0');

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        &str[0],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}
#endif  // WINPROC_H_

0 个答案:

没有答案