Jos*_*mon -2 c++ winapi system createprocess
我创建了一个代码,使用Windows窗体应用程序来构造一个gui.我在我的代码中使用系统命令来调用外部.exe.但是,这种方法创建了一个命令行终端.我发现我可以在这里用CreateProcess函数替换系统.我该如何使用这个功能?我应该指定哪些参数才能运行?我现在的代码是:
string run_template = "a.exe -i " + s1 + " -r 10 -f image2 filename%03d.jpg";
system(run_template.c_str());
Run Code Online (Sandbox Code Playgroud)
编辑:
#include <tchar.h>
string workPath = "";
string args = "-i " + s1 + " -r 10 -f image2 vid/frames/filename%03d.jpg";
HINSTANCE hRet = ShellExecute(NULL, _T("open"), _T("a.exe"), _T(args.c_str()), _T(workPath.c_str()), SW_HIDE);
DWORD errNum = GetLastError();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
1>c\projects\first_api\first_api\Form1.h(229): error C2065: 'Largs' : undeclared identifier
1>c:\projects\first_api\first_api\Form1.h(229): error C2228: left of '.c_str' must have class/struct/union
1> type is ''unknown-type''
1>c:\projects\first_api\first_api\Form1.h(229): error C2065: 'LworkPath' : undeclared identifier
1>c:\projects\first_api\first_api\Form1.h(229): error C2228: left of '.c_str' must have class/struct/union
Run Code Online (Sandbox Code Playgroud)
EDIT2:
string run_template = "a.exe -i " + s1 + " -r 1 -f image2 /filename%03d.jpg";
//system(run_template.c_str());
STARTUPINFOA si = {sizeof(STARTUPINFOA), 0};
PROCESS_INFORMATION pi = {0};
if (CreateProcessA(NULL, const_cast<char*>(run_template.c_str()), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码时,命令提示符仍然存在.
string run_template = "a.exe -i " + s1 + " -r 10 -f image2 filename%03d.jpg";
STARTUPINFOA si = {sizeof(STARTUPINFOA), 0};
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi = {0};
vector<char> cmdline(run_template.begin(), run_template.end());
if (CreateProcessA(NULL, &cmdline[0], NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1375 次 |
| 最近记录: |