【问题标题】:Create Process default browser创建进程默认浏览器
【发布时间】:2016-01-30 10:37:12
【问题描述】:

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

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

【问题讨论】:

  • CreateProcess() 工作方式不同。你不能用这个函数来做。

标签: c++ visual-c++


【解决方案1】:

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

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

【讨论】:

    【解决方案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;
    }
    

    【讨论】:

    • 你真是太棒了。好吧,但是我遇到了一个问题,它作为标签打开而不是作为新窗口打开,有什么解决方案吗?
    • 在这种情况下,您需要做一些工作。因为您需要为每个浏览器添加不同的命令行参数。使用 AssocQueryString() 返回解决方案。检查默认浏览器。如果那是 chrome,您应该为 CreateProcess() 的第二个参数添加 --new-window。如果那是 Firefox,请添加 -new-instance。或许可以加IE等...
    • if (!CreateProcessA("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", // 没有模块名(使用命令行)"--new - window ", ........ 给我 ERROR CreateProcess failed: (2)
    • 这可能是一个题外话,但我添加了它。如果对您有帮助,请投票,谢谢。
    • 非常感谢。那么你能告诉我如何更改标题吗?
    【解决方案3】:

    这是您想要的另一个答案。只要您希望它作为新窗口打开。可以为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;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 2014-08-10
      • 2012-07-19
      • 2012-01-29
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多