我有一个 powershell 脚本可以在边缘浏览器上启动网页。当您从命令行运行此脚本时,它工作正常。
launchWebPageFromEdge.ps1
start microsoft-edge:https://www.youtube.com
launchWebPageFromIE.ps1
$url = 'https://www.youtube.com/'
$IE = new-object -com internetexplorer.application
$IE.navigate2($url)
$IE.visible = $true
我的任务是从 Windows C++ 控制台应用程序启动它。我有以下相同的代码。使用 CreateProcess API 调用时,脚本未启动浏览器。我有另一个 powershell 脚本可以在 IE 中启动网页。它工作正常。
int main()
{
std::string cmdExc = "powershell.exe -ExecutionPolicy Bypass -file \"C:\\launchWebPageFromEdge.ps1\"";
STARTUPINFO startInfo;
PROCESS_INFORMATION procInfo;
if (!CreateProcess(NULL,
const_cast<char *>(cmdExc.c_str()), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to TRUE
REALTIME_PRIORITY_CLASS | CREATE_NO_WINDOW, // creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startInfo, // Pointer to STARTUPINFO structure
&procInfo) // Pointer to PROCESS_INFORMATION structure
)
{
std::cout << "error\n";
return -1;
}
WaitForSingleObject(procInfo.hProcess, INFINITE);
return 0;
}
由于IE是从相同的代码启动,我不认为与CreateProcess的API使用创建旗帜的任何问题。那么有人可以在这里帮助我吗。
答案 0 :(得分:0)
STARTUPINFO
结构未初始化。它不是输出结构,因此在那里传递垃圾可能会导致 CreateProcess
失败。您可以像这样轻松修复它:
STARTUPINFO startInfo { sizeof(STARTUPINFO) };
请注意,您应该关闭 procInfo
中返回的 hProcess
和 hThread
句柄。
但是您根本不需要 PowerShell 来打开 Edge。
使用 ShellExecuteEx
可以轻松实现相同的目的:
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); // initialize COM at the start of the program
SHELLEXECUTEINFOA ex{sizeof(SHELLEXECUTEINFOA)};
ex.lpFile = "microsoft-edge:https://www.youtube.com";
ShellExecuteExA(&ex);