3

I used this function to open new tab in Chrome and active it:

ShellExecuteA(0,0,"chrome.exe","http://google.com  --incognito",0,SW_SHOWMAXIMIZED);

but Chrome only open new tab but it doesnt active window.
(I call this function from global keyboard-hook of an application with no user interface, if user press specified key).

How I can fix it?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
NoName
  • 7,940
  • 13
  • 56
  • 108
  • http://stackoverflow.com/questions/557166/bring-to-front-for-windows-xp-command-shell Does this help? – Adrian Panasiuk May 29 '13 at 01:39
  • Generally, you can't bring arbitrary windows to front, so there is no easy way to do it. See "Remarks" section here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx – n0rd Jun 03 '13 at 17:28

2 Answers2

2

Looks like a bug in chrome.exe. I could repro with your ShellExecute call from a simple console app, if a regular (non-incognito) chrome.exe session was running and no incognito session was running. In other words, if a new incognito chrome session needed to be spawned, the regular session did not appear to correctly propagate the ShowWindow flags to the spawned incognito process. Another factor was that the activation failure also required the regular chrome session to be active before the test app ran. If any other app was active (say notepad.exe), then activation of the incognito session succeeded. The same behavior occurs with ShellExecuteEx and CreateProcess. Observing in Process Explorer (from sysinternals), it's clear that chrome.exe is forking the child process as necessary and then terminating itself. This makes it difficult to intercept an hProcess or processId in order to ultimately call SetActiveWindow.

Scott Jones
  • 2,880
  • 13
  • 19
  • `SetActiveWindow` can only work with windows from your own process. You can't use it with other process' windows. – n0rd Jun 03 '13 at 19:17
  • @n0rd Good clarification - it could be called via DLL injection, though. Or SetForegroundWindow could be used. My point had more to do with the difficulty of even obtaining an HWND (via hProcess or processId) in the first place. – Scott Jones Jun 03 '13 at 21:05
0

Its not possible. You have to make Google Chrome as default browser and than try this:

(only tested on WinXP using IE6)

first a function, that finds the default app for any File extension:**

enter code here

#include<Registry.hpp>

AnsiString GetDefaultApp(AnsiString ext)
  {
   TRegistry* reg = new(TRegistry);
   reg->RootKey = HKEY_CURRENT_USER;
   if(!reg->OpenKeyReadOnly("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\."+ext+"\\OpenWithList"))
      return(NULL);
   try
      {
      AnsiString MRUList = reg->ReadString("MRUList");
      AnsiString ret = reg->ReadString(AnsiString(char(MRUList[1])));
      return(ret);
      }
   catch(...)
      {
      return(NULL);
      }
   }

now the code to launch the default app for html files and giving the URL as parameter:**

#include<shellapi>
void OpenURL(AnsiString URL)
   {
   AnsiString app = GetDefaultApp("html");
   if(app == NULL)
      return;
   ShellExecute(NULL,"open",app.c_str(),URL.c_str(),NULL,SW_SHOWDEFAULT);
   }

Now you can open a URL in a new Browser using this command:

OpenURL("http://www.AlgorithMan.de/");