Link to home
Start Free TrialLog in
Avatar of ubiwebdev
ubiwebdev

asked on

Open Default Browser in a hidden status

Hi,

I will need to open a web broswer in a hidden state and navigate to a URL. I tried to use
ShellExecute(NULL,"open","http://www.google.com",NULL,NULL,SW_HIDE);
However, the browser window will still be visible. (Just like if I use SW_SHOWNORMAL)

Is there anyway to hide the browser window?


Thanks
Avatar of Darrylsh
Darrylsh

Hate to sound suspicious, but what would be the legitimate use for this?  I can't think of any.  Sounds to me like you want to open a browser and direct it to a malicious site, without the user knowing!
Avatar of ubiwebdev

ASKER

Well, basically, it is full window application that needs to pull some data from our own web server to pre-populate the fields. The reason I need to open the default browser is because I will need to use cookies.

Hate to say that too, however, if you can not answer the question, please don't waste your time to put meaningless comments.
The browser appears even if you pass SW_HIDE because the flag is only informative, it is simply passed to the associated application. It is up to the application to decide how to handle it.

What you can do here that will work is to have a system wide hook (a CBT hook will be enough) where you'll hook the window creation and if it's your IE instance (identifiable by classname and window title) will set your wndproc for it. There, in the wndproc you'll have to override some displayng messages to make it hidden.

The CBTProc can look like:

LRESULT CALLBACK MyCbtProc(int nCode, WPARAM wParam, LPARAM lParam)
{
      switch(nCode)
      {
            case HCBT_CREATEWND:
                  TCHAR      szClassName[MAX_PATH];
                  HWND      hDlg = (HWND)wParam;
                        
                  GetClassName(hDlg, szClassName, sizeof(szClassName) - 1);
                  if(!lstrcmpi(szClassName, _T("#32770"))) //here the IE class name
                  {
                        GetWindowText(hDlg, szClassName, sizeof(szClassName) - 1);
                        if(!lstrcmpi(szClassName, "Message Source"))  //here the window title
                        {
                              // Save away old proc
                              //
                              SetProp(hDlg, _T("OldWndProc"), (HANDLE) GetWindowLong(hDlg, GWL_WNDPROC));
                              SetWindowLong(hDlg, GWL_WNDPROC, (DWORD)MyIEWndProc);
                              break;
                        }
                  }
            }
      }

      return CallNextHookEx(g_hCBT, nCode, wParam, lParam);
}


And the wndproc:

LRESULT CALLBACK MyIEWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
      WNDPROC      lpOldProc;
      
      lpOldProc = (WNDPROC)GetProp(hWnd, _T("OldWndProc"));

      if(msg == WM_WINDOWPOSCHANGING)
      {
            WINDOWPOS      *pWPos = (WINDOWPOS*)lParam;
            
            pWPos->flags |= SWP_HIDEWINDOW | SWP_NOACTIVATE;
            pWPos->flags &= ~SWP_NOZORDER;
            pWPos->hwndInsertAfter = g_PluginData.hOEWnd;//HWND_BOTTOM;
      }

      return CallWindowProc(lpOldProc, hWnd, msg, wParam, lParam);
}
Thanks nonubik.
However, I can not know which application will be launched. It can be IE, Firefox, Opera, etc. It all depends on user's setting. Thus, I can not guess which message is the message that I should play with.
Instead of ShellExecute or ShellExecuteEx, is there another way I can launch the default browser and hide it? Or, is there a way that I can get the hWnd after the ShellExecute and then hide it?

Thanks.
> I can not know which application will be launched
Then do not check the classname, only try to search your page title as substring in the browser's window title.

>Thus, I can not guess which message is the message that I should play with.
The WM_WINDOWPOSCHANGING code I've posted works even for modal dialog boxes ;)

> is there a way that I can get the hWnd after the ShellExecute and then hide it?
I can't think of any right now..
>> "Hate to say that too, however, if you can not answer the question, please don't waste >> your time to put meaningless comments."

It wasn't a meaningless comment; before I decide to provide help, I want to make sure I am not helping someone create a malicious program.  That would be irresponsible of me.  

Anyway, I would suggest you use a web browser control in your app, that way you can
1. keep it hidden
2. be sure of what browser it is ( IE explorer)
3. still use cookies
Thanks Darrylsh.

Actually, the current code is using the IE control. However, since these cookies are shared among multiple windows application and web application, we will need to use the default browser. Otherwise, more than one set of cookies will be created for different browser and their value will be different too.

Anyway, the only way I found so far is to search the registry to get the default browser and then use CreateProcess to launch the browser with the SW_HIDE. It worked. However, I was just wondering if there is cleaner/easier way.

ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial