Solved

hWnd

Posted on 2000-05-14
7
843 Views
Last Modified: 2013-11-20
I would like my programm to change the url of my browser. I know I have to use:

::ShellExecute(hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL);

but I don't know how to get  hWnd  for the browser window.
What I want is my programm to go onto the browser window and perform the change of the URL.

How can I do it?

Thanks in advance.
0
Comment
Question by:javiscript
7 Comments
 
LVL 2

Expert Comment

by:JunHaan
ID: 2808466
javiscript,
Here is a possible solution :

You could use ::FindWindow() to get the handle of your browser. Although so, you'd need to know the exact title of your browser window, which is always different because the window title is dependent on the website's title too.

Lets say you have a Netscape browser up, and am in this page(which in this case, you are) with the title "MFC Programming Question - Netscape"...

You could insert this in one of your application's event handlers:

//Start of code

HWND hWnd; //This will hold the handle to the window

hWnd = ::FindWindow( NULL, "MFC Programming Question - Netscape" );
::ShellExecute( hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL );

//End of code

This could only work if you know the exact title of the website.
I am looking forward to a better solution, of course...
Hope this helps, for the mean time! :)
0
 
LVL 2

Expert Comment

by:JunHaan
ID: 2808475
javiscript,
Here is a possible solution :

You could use ::FindWindow() to get the handle of your browser. Although so, you'd need to know the exact title of your browser window, which is always different because the window title is dependent on the website's title too.

Lets say you have a Netscape browser up, and am in this page(which in this case, you are) with the title "MFC Programming Question - Netscape"...

You could insert this in one of your application's event handlers:

//Start of code

HWND hWnd; //This will hold the handle to the window

hWnd = ::FindWindow( NULL, "MFC Programming Question - Netscape" );
::ShellExecute( hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL );

//End of code

This could only work if you know the exact title of the website.
I am looking forward to a better solution, of course...
Hope this helps, for the mean time! :)
0
 
LVL 3

Expert Comment

by:_mb_
ID: 2808609
A better way would be, to find the window handle based on the instance handle, that ShellExecute() returns. Then you don't need to know the application title.

Here's the code:

HINSTANCE hInst = ::ShellExecute(hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL);

HWND winHandle;

if( (winHandle = ::FindWindow(NULL, NULL) )
{
     do{
          if( hInst == ::GetWindowLong(winHandle, GWL_INSTANCE) )
               break;
     }while( (winHandle = ::GetWindow(winHandle, GW_HWNDNEXT) );
}
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 23

Accepted Solution

by:
chensu earned 86 total points
ID: 2808647
"hwnd
Window handle to a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box."

The hWnd is not the window handle of the Web browser. You may use NULL or your window handle. In a CWnd derived class member function, use

hWnd = this->GetSafeHwnd();
0
 

Expert Comment

by:caiman23
ID: 2808695
I get an error about

GWL_INSTANCE: undeclared identifier.

What is GWL_INSTANCE?
How can I declare it?
0
 
LVL 3

Expert Comment

by:_mb_
ID: 2808817
Sorry,it must be GWL_HINSTANCE, it's an windows api constant.
By the way: I add a check, if we have the applications parent window.

if( (winHandle = ::FindWindow(NULL, NULL) )
{
     do{
          if( !GetParent(winHandle) )
          {
                if( hInst == ::GetWindowLong(winHandle, GWL_HINSTANCE) )
                     break;
          }
     }while( (winHandle = ::GetWindow(winHandle, GW_HWNDNEXT) );
}
0
 
LVL 2

Expert Comment

by:JunHaan
ID: 2809533
I would say chensu and mb is correct...
Here is a piece of code from mb and chensu's comment to try out... it works!

//Start code

HWND hWnd;
HWND winHandle;

hWnd = this->GetSafeHwnd();

HINSTANCE hInst = ::ShellExecute(hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL);

if( winHandle = ::FindWindow(NULL, NULL) )
{
   do{
      if( !::GetParent(winHandle) )
               {
                    if( (long)hInst == ::GetWindowLong(winHandle,                                                                              GWL_HINSTANCE) )
                    break;
               }
      }while( winHandle = ::GetWindow(winHandle, GW_HWNDNEXT) );
}

//End of code

                                
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now