Oct 21, 2008

C# AddIn - Start Debugging and get the running Internet Explorer instance.

I needed to do this (actually I wanted). I was curious if you can obtain an instance of a Internet Explorer which was started in debugging session. Here is a snipet to do this:

        _applicationObject.Debugger.Go(
false);
        SHDocVw.
ShellWindowsClass windows = new ShellWindowsClass();
        
foreach (SHDocVw.InternetExplorer explorer in windows)
        {
          
if (explorer.LocationURL == string.Empty)
           {
            
// this is our guy!
             ie = explorer;
           }
        }


Note : this snipet should be added somewhere where you have access to the _applicationObject.
This is the variable which is declared automatically when you create new instance. I suggest you to put it into the exec method of the AddIn:

     public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
     {
        _applicationObject.Debugger.Go(
false);
        SHDocVw.
ShellWindowsClass windows = new ShellWindowsClass();
        
foreach (SHDocVw.InternetExplorer explorer in windows)
        {
          
if (explorer.LocationURL == string.Empty)
           {
            
// this is our guy!
             ie = explorer;
           }
        }
     }

You also need to add reference to ShDocVw (Microsoft Shell And Automation Library).
Enjoy!

2 comments:

cypressx said...

Hi, Pavel !
I'm curious, this is for educational purpose only, or it can be used in practice ?

Thanks

Павелъ Дончевъ said...

Oh, I think it can have a great value. I am currently trying to build an application to take a snapshot from your project, make it html / word / PDF and save it.

First I tried with just HTML automation (by running my project and saying - "hey this is the window you need to watch in, now go click on the links, create some screenshots and tell me when you're ready").

This worked to some degree but unfortunatelly it wasn't as powerful as I wanted.

Now, if you are automating the Visual Studio (addIn for example), you have access to CodeProject, CodeFunction, CodeClass etc. variables which are something like DOM tree of your project.

Here is the deal - you have a project with a file of roles. You want to open each page from your project and for which the current role has access, take a screen shot, do some other stuff. You can separatelly ask for an URL to the project and a path to the Roles xml. But that's it. In the other hand - if you have automated Visual Studio you will have access to the Code... things I've mentionedd above. Thus you can see what ASP controls are there and react accordingly. For example you can see that there is an asp:GridView, you can see that this asp:GridView is bound to Object Data Source, which takes the users for example. This way you can check on the front if you have the grid view rendered correctly with all users.

P.S. The things I said above are just in theory I am still thinking on how can I validate if the grid view is actually rendered. You can check the HTML for Html table, or get the ClientID and see if there is a table with that name but it may be kind of dificult to do so.

So for now I will try to only check simple asp: controls like text box, dropdown and so on and then will focus on the more complicated.

Another thing is that you can probably "see" that the text of the text box is assigned in a variable in the code behind which is of type int. So you can think that the text box is also of type int and do some boundary tests (test with character, test with negative numbers, test with very big numbers).

Now, I know I promised you to do some investigations on the asp:GridView and dynamically changing DataSource Select Method and Parameters (I hope you don't remember as I screw it up :).
I haven't forgotten but I had no time to dive in the ObjectDataSource mechanism.
That is why I will not promise you that I will do another post to show you some practical means of the Debugger Object ;).

But I may do so ... :)


Wish you all the best,
Pavel.