创建流程默认浏览器

时间:2016-01-30 10:37:13

标签: c++ visual-c++

我目前正在使用ShellExecute" open"在用户的浏览器中打开一个URL,但在Win7和Vista中遇到一些麻烦,因为该程序作为一项服务运行升级。

我想获取线程ID,因此ShellExecute无法获取线程ID,因此我开始使用" CreateProcess"但是我没有看到任何关于打开默认浏览器的帮助,无论它来自CreateProcess。

3 个答案:

答案 0 :(得分:2)

您可以使用Shell API的其他功能来确定哪个浏览器是默认浏览器。

例如,您可以使用AssocQueryString向shell询问与.html文件关联的可执行文件名,并通过CreateProcess启动它。

答案 1 :(得分:2)

您可以在此处使用默认浏览器打开网址。

STARTUPINFOA si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CStringA command_line;
command_line.Format("cmd.exe /c start \"link\" \"%s\"", "http://www.google.com");

if (!CreateProcessA(NULL,     // No module name (use command line)
     command_line.GetBuffer(),
     NULL,           // Process handle not inheritable
     NULL,           // Thread handle not inhberitable
     FALSE,          // Set handle inheritance to FALSE
     NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,              // No creation flags
     NULL,           // Use parent's environment block
     NULL,           // Use parent's starting directory 
     &si,            // Pointer to STARTUPINFO structure
     &pi)           // Pointer to PROCESS_INFORMATION structure
     )
{
     TRACE("CreateProcess failed (%d).\n", GetLastError());
     return;
}

答案 2 :(得分:1)

这是你想要的另一个答案。只要你想要它打开一个新的窗口。您可以为IE,Opera等扩展它......

DWORD size = MAX_PATH;
char buff[MAX_PATH];

int err = AssocQueryStringA(ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_EXECUTABLE, ".html", NULL, buff, &size);
STARTUPINFOA si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CStringA command_line;
CStringA target_url( "http://google.com/" );


if( strcmp( "chrome.exe", PathFindFileNameA(buff)) == 0 )
    command_line.Format("%s --new-window %s", buff, target_url);
else if( strcmp( "firefox.exe", PathFindFileNameA(buff)) == 0 )
    command_line.Format("%s -new-instance %s", buff, target_url);
else 
    command_line.Format("%s %s", buff, target_url);


if (!CreateProcessA(NULL,     // No module name (use command line)
    command_line.GetBuffer(),
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inhberitable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi)           // Pointer to PROCESS_INFORMATION structure
)
{
    //... error handling
    return;
}