• C

how to get URLs of Internet Explorer documents

I would like to be able to get the URLs of all documents that are open in IE. I tried by using the tcom package (Windwos com objects, http://www.vex.net/~cthuang/tcom/) of Tcl/Tk. But when I try to get an handle of an existing instance of IE I get an "operation unavailable" error message. Instead I can create a new object with tcom and use it without any problem. Any way of doing it in Windows with C would be fine for me. Thanks.
s_federiciAsked:
Who is Participating?
 
srinimsConnect With a Mentor Commented:
finaly I, solved for u,
find it usefull.

srini ms

void GetIEAddress( )
{
      #define MAX_LEN  255
      long lngStyle,lngLen;
      HWND hWnd,hWndChild;      
      char strBuffer[MAX_LEN];
      char strURL[MAX_LEN];
      char strCAPTION[MAX_LEN];
      char *mconCAPTION = " - Microsoft Internet Explorer";
      char *partFOUND;
      OSVERSIONINFO tOSInfo;
      tOSInfo.dwOSVersionInfoSize = sizeof(tOSInfo);
      GetVersionEx(&tOSInfo);

      hWnd =GetWindow(GetDesktopWindow(), GW_CHILD);
      while(hWnd!=0)
      {
            lngStyle = GetWindowLong(hWnd, GWL_STYLE);
            if(lngStyle & WS_VISIBLE)
            {
                  GetClassName(hWnd, strBuffer, MAX_LEN);
                  if(!strcmp(strBuffer,"IEFrame"))
                  {
                        GetWindowText(hWnd, strCAPTION, MAX_LEN);
                        partFOUND = strstr(strCAPTION,mconCAPTION);
                        if(partFOUND)
                        {
                              if(tOSInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
                                    hWndChild = FindWindowEx(hWnd, 0, "WorkerW",0);
                              else
                                    hWndChild = FindWindowEx(hWnd, 0, "WorkerA",0);
                              if(hWndChild>0)
                                    hWndChild = FindWindowEx(hWndChild, 0, "ReBarWindow32",0);
                              if(hWndChild>0)
                                    hWndChild = FindWindowEx(hWndChild, 0, "ComboBoxEx32",0);
                              if(hWndChild>0)
                                    hWndChild = FindWindowEx(hWndChild, 0, "ComboBox",0);
                              if(hWndChild>0)
                                    hWndChild = FindWindowEx(hWndChild, 0, "Edit",0);
                              if(hWndChild>0)
                              {
                                    lngLen = SendMessage(hWndChild, WM_GETTEXTLENGTH, 0,0);
                                    SendMessage(hWndChild, WM_GETTEXT,((lngLen + 1) > MAX_LEN)?(lngLen + 1):MAX_LEN, (long)&strURL);
                                    MessageBox(NULL,strURL,strCAPTION,0);
                              }
                        }
                  }
            }
            hWnd = GetWindow(hWnd, GW_HWNDNEXT);
      }
}
0
 
jkrCommented:
See http://support.microsoft.com/kb/q176792/ ("How to connect to a running instance of Internet Explorer") which demonstrates that. The sample code there however is VB or C++, but it is almost complete, all you need to call is 'IWebBrowser2::get_LocationURL()' instead of 'GetDocument()'.
0
 
srinimsCommented:
HI, I think IWebBrowser@ is used to create a new instance of IE, And get_LocationURL() will get the address of created instance.
For Getting the url's of already openend windows. U have do the Spy program Which can idendify all the Windows under the Destop handle, And then you have to locate the Internet Explorer application on that. After then you can find the URL of all opened IE windows.  
pls find the below details,which I got from one site.

In the Internet Explorer the current URL is displayed inside the combo box component on the Address bar. In order to "steal" the current URL from IE, you'll need to find the (control class) name of that combo box, locate it, and grab its text. Now, this seems easy, but how do you do that?
To be able to grab a text from an arbitrary window (and its child windowed controls) on the Windows Desktop, you'll need to play with Windows API calls and Windows Messages. First, or better to say last, when you find that combo box with the current URL in it, you can send a WM_GETTEXT message to that window and pick the text displayed inside. Second, or better to say first, you need to know the exact order of controls that are parents to that URL combo box. Then, you use the FindWindowEx API call in order to retrieve the handle (an integer number that uniquely identifies each control / window) of the child window. Ok, all seems easy, but again how do you know the structure of IEs controls and their parenthood?

the main IE window is called "IEFrame".To retrieve the handle to the top-level window whose class name is provided (in our case: IEFrame), you can use the FindWindow API call. When you have the IE handle you then use the FindWindowEx API, to locate that ComboBox control with the URL. Finally, as already mentioned above, you send that ComboBox the WM_GETTEXT message to obtain the text from it.
This seems easy. However, note that FindWindow API retrieves the handle to the top-level window. What this means is that if you have multiple instances of IE opened (as you probably have) the function will locate the last activated IE window

Enumerate all top-level windows using the EnumWindows callback (this gives us a Handle)
Grab the Caption (Title) of each Window
Look only for windows whose class name is "IEFrame"
Use the FindWindow provided with the Handle and the Caption to locate the correct IE window
Call FindWindowEx several times to locate the ComboBox carying the URL, as it is the child to some parent which is a child to our top-level IEFrame (this is the class name) window
Send a message to the ComboBox to retrieve the text inside it
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
srinimsCommented:
A VB sample

http://www.mvps.org/access/api/api0051.htm

U could use the same thing for windows application, or you can convert into the Vc++ code using the same api calls.

srini ms
0
 
jkrCommented:
>>HI, I think IWebBrowser@ is used to create a new instance of IE, And get_LocationURL() will get the
>>address of created instance.

Thank you, I really appreciate when people repeat what I already wrote.
0
 
s_federiciAuthor Commented:
Sorry, after deep study, I'm totally unable to implement srinims' suggestion in C. Help is needed.
0
 
srinimsCommented:
hi jkr,   I'm really sorry for my comments. I haven't read the documment given in the microsoft site. I have read out only from my local msdn, that's the problem. Any way I'm really sorry for the comments on your post.


hi federici, could u post the problem you are facing.  have u tried the code using vb given in the second link.

srini ms
0
 
srinimsCommented:
the vb code is working fine.

u can covert the same vb code in vc++ sdk code.
get the .bas file in the above link, i have given.
try to load in to one VB. and change some code as below mentioned to get the message box with address

                If hWndChild > 0 Then
                    '   Get the length of the URL in Editbox
                    lngLen = apiSendMessage(hWndChild, WM_GETTEXTLENGTH, _
                                    0, ByVal 0&)
                    strURL = Space$(lngLen + 1)
                    '   Get the URL itself
                    lngLen = apiSendMessage(hWndChild, WM_GETTEXT, _
                                            lngLen + 1, ByVal strURL)
                    MsgBox (strURL)

the strURL is the string responsible for address, the sniphet is from the Refresh method.
i have the problem in the first line related with database, just comment it out.

srini ms
0
 
srinimsCommented:
change the statement from ((lngLen + 1) > MAX_LEN)?(lngLen + 1):MAX_LEN
into
((lngLen + 1) < MAX_LEN)?(lngLen + 1):MAX_LEN
0
 
s_federiciAuthor Commented:
Thanks a lot. Sorry if I didn't test immediately, but I was out of the office. It worked very well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.