用python代码creatprocess函数_C++使用CreateProcess函数调用python脚本(调用gdal_polygonize.py将栅格TIF转矢量SHP)...

这个例子是调用gdal_polygonize.py将Tif格式的文件转换为shape格式的文件。

#include

#include

#include

using namespace std;

PROCESS_INFORMATION* _handle;

/* exec由绝对路径和参数构成 */

int RunPythonScript(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 pyexe = "D:\\Python27\\python.exe";

string pyscript = "D:\\bin2\\gdal_polygonize.py";

string tiffile = "D:\\120104.tif";

string param = "-f";

string drive = "ESRI SHAPEFILE";

string shapefile = "D:\\1.shp";

string commond = pyexe + " " + "\"" + pyscript + "\"" + " " + "\"" + tiffile + "\"" + " " + param + " " + "\"" + drive + "\"" + " " + "\"" + shapefile + "\"";

//string exe = "D:\\Python27\\python.exe \"D:\\bin2\\gdal_polygonize.py\" \"D:\\120104.tif\" -f \"ESRI SHAPEFILE\" \"D:\\1.shp\"";

if (RunPythonScript(commond) != 0)

{

cout << GetLastError() << endl;

}

else

{

cout << "succeed convert Tif file to shape file." << endl;

}

getchar();

return 0;

}

运行截图:

shape file:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值