使用ShellExecuteEx调用Python可执行文件(exe)的过程中,传递中文路径时会产生乱码的问题,主要原因为该函数传递的参数为LPCWSTR,Python中无法处理。后改用ShellExecuteExA传递LPCSTR格式的参数,Python能够正常处理其中文路径。具体用法如下:
try
{
SHELLEXECUTEINFOA sei = { 0 };
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.hwnd = NULL;
sei.lpVerb = "open";
sei.lpFile = LPCSTR(exePath);
sei.lpParameters = LPCSTR(params);
sei.nShow = SW_HIDE;
sei.hInstApp = NULL;
printf("sei.lpFile: %s\n", sei.lpFile);
printf("sei.lpParameters: %s\n", sei.lpParameters);
//使用ShellExecuteExA解决向Python传中文时出现乱码的问题
if (ShellExecuteExA(&sei))//执行脚本
{
if (sei.hProcess)//以 SEE_MASK_NOCLOSEPROCESS 方式执行,执行成功会返回进程句柄hProcess
WaitForSingleObject(sei.hProcess, INFINITE);//等待执行完毕
else
return -1;
}
else
{
return -1;
}
}
catch (const std::exception& e)
{
printf("[Main process] Error: ", e.what());
LOG(INFO) << e.what();
return -2;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)