Retrieving current URL

I have a running instance of IE and I want to know what is the current URL that this IE is displaying. This is a stand alone IE (not WebBrowser control).
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First, read this:

With that information, you can get the URL using the:


yelinyAuthor Commented:
Thank you jhance.
I followed the instructions in this kb article, but it doesn't compile.
This is why I don't find your answer the perfect one.
I will appreaciate your help to solve the compilation error.
I get a message saying:
Cannot open type library file: 'mshtml.dll': Error loading type library/DLL.

I added the #import statments in the application's view header file.

Do you have any clue?

>I followed the instructions in this kb
>article, but it doesn't compile.

It's not a complete application in the article.  It just shows the key points on how to get the pointer to a running IE object.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

yelinyAuthor Commented:
I know this is not a complete code.
This is why I said that I followed the instructions.
I added these parts of code in the classes that this article says to put it in.
I thought maybe you tried this in the past and you know why doen't it compile.
Thank you for your help.
With your permision I'll leave the question open for now in order to let others read it and response.
You can get an instance of IE this way.

IWebBrowserApp*      webApp;

HRESULT hr = CoCreateInstance( __uuidof( InternetExplorer ), NULL, CLSCTX_SERVER, IID_IWebBrowserApp, (void**)&webApp);

webApp->put_Visible( VARIANT_TRUE );
webApp->get_LocationURL( &url );

You might have to include EXDISP.H if it is not already included.

But this creates a new instance of IE.  

When creating IE via COM you don't have any options ( that I know of ) to tell COM that you want a current running IE.  It is up to IE ClassFactory to either give you a old one or create a new one.  If you know any COM way to get the old one or if you can configure IE to give you the old one you are done.

Gunnar Valur

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

Your technique will create _an_ instance of IE, but the question was getting the interface to a running instance of IE.  Not the same problem.
yelinyAuthor Commented:
I couldn't compile the code that was proposed as a solution.
yelinyAuthor Commented:
I couldn't compile the code that was proposed as a solution.
yelinyAuthor Commented:
gvg's solution was good enough for me (at the moment).
If you'll post it as an answer you'll earn the points.
Thank you both for the effort.
You don't have to have gvg post an answer.  EE has added the "Accept Comment as Answer" feature some time ago.  Just click on that link next to his comment that you wish to accept.

BTW, I still think that his answer (while a correct statement) doesn't answer your question.  But hey, they're your points....
yelinyAuthor Commented:
First jhance thanks for the information. I did use this feature (Accept comment as answer).
Although gvg's answer wasn't the best one, he brought me to a point where I can go on with my study of this subject. I think this is why EE gave us the option to grade an answer.
Thanks and good luck.

Since you were a good sport and gave me points I thought I should figure this out.

I used the article and it works for me.
If you have IE 5 be sure to only.

#import <shdocvw.dll>
#import <mshtml.tlb>

SHDocVw::IShellWindowsPtr m_spSHWinds;       

CString strCount;
long nCount = m_spSHWinds->GetCount();

IDispatchPtr spDisp;
for (long i = 0; i < nCount; i++)
  _variant_t va(i, VT_I4);
  spDisp = m_spSHWinds->Item(va);

  SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
  if (spBrowser != NULL)
//  m_ctlListLoc.AddString(spBrowser->GetLocationName());

    MSHTML::IHTMLDocument2Ptr spDoc(spBrowser->GetDocument());
    if (spDoc != NULL)
//    m_ctlListTitle.AddString(spDoc->Gettitle());

If you have more questions feel free to ask
My point was that is you don't have to import <mshtml.dll> if you can import the mshtml.tlb since it has the same class information.

Only one more comment.

In IE 4 the Type Library was linked into the MSHTML.DLL but in IE 5 they split to DLL into DLL functionality ( the DLL ) and into Type Library ( the TLB )

Gunnar Valur
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.