我有一个powershell脚本,可以在边缘浏览器上启动网页。当您从命令行运行此脚本时,它可以正常工作。
启动WebPageFromEdge.ps1
start microsoft-edge:https://www.youtube.com
从IE.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 << "errorn";
return -1;
}
WaitForSingleObject(procInfo.hProcess, INFINITE);
return 0;
}
由于IE是从相同的代码启动的,我认为createprocess API中使用的创建标志没有任何问题。有人能在这里帮我吗。
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);