现在的位置: 首页 > 综合 > 正文

CreateProcess函数调用python文件

2013年03月06日 ⁄ 综合 ⁄ 共 900字 ⁄ 字号 评论关闭
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

PROCESS_INFORMATION* _handle;

/* exec由绝对路径和参数构成 */
int Excute(string& exec)
{
	
	STARTUPINFOA si;
	PROCESS_INFORMATION* pi = new PROCESS_INFORMATION;

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


	// Start the child process. 
	if( !CreateProcessA( NULL,   // No module name (use command line)
		(LPSTR)exec.c_str(),        // 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 
		(LPSTARTUPINFOA)&si,            // Pointer to STARTUPINFO structure
		pi )           // Pointer to PROCESS_INFORMATION structure
		) 
	{
		return 1;
	}
	_handle = pi;

	return 0;
}

int main()
{
	string exe = "C:\\Python27\\python.exe D:\\Projects\\Test\\test-set\\test-1\\test_1.py";
	if(Excute(exe) != 0)
	{
		cout<<GetLastError()<<endl;
	}
	else
	{
		cout<<"succeed"<<endl;
	}
	
	getchar();
	return 0;
}

抱歉!评论已关闭.