PullRank Home a tool to enable you to log the Google PageRank of a URL
Development History
The Code
Since I was new to C#, I decided to start with a "Hello World"-caliber example that would launch an Internet Explorer browser and load a particular URL. From some previous work experience, I knew that there was an Internet Explorer object somewhere in the .NET library, so I used Google to search for Internet Explorer site:msdn.microsoft.com which took me to the InternetExplorer Object API. There I found a method called Navigate that looked promising:
object.Navigate( _
    url As String, _
    [Flags As Variant,] _
    [TargetFrameName As Variant,] _
    [PostData As Variant,] _
    [Headers As Variant])
However, the syntax looked a little strange as As Variant did not seem like it was valid C# code. Apparently, this documentation was for Visual Basic. Yet I knew that the .NET platform was aimed at allowing for the interoperability of C#, J#, C++, and VB, so I supposed that the Navigate method would have the same name in C# and that the last four parameters would be optional. To test my hypothesis, I created a file named src/pullrank/Test.cs with the following code:
public class Test {
  public static void Main(string[] args) {
    InternetExplorer ie = new InternetExplorer();
    ie.Visible = true;
    ie.Navigate("http://web.mit.edu/");
  }
}
As I suspected, this first attempt did not work. If this were Java, I would expect to have to import the package that contained the InternetExplorer object. The question was what "package" did InternetExplorer belong to and how could I include the "library" on the classpath? And even once I did that, what would the command be to compile the thing?

Fortunately, I found a blog post with all of the answers I needed. Some of the key points were:

  • running aximp c:\WINNT\system32\shdocvw.dll to generate the "interop dlls" SHDocVw.dll and AxSHDocVw.dll (I placed both of these dlls in my lib\ directory)
  • "importing" these dlls by adding the following lines to Test.cs:
using SHDocVw;
using AxSHDocVw;
  • using all 5 parameters available for the method, filling the unused ones with null references:
object o = null;
ie.Navigate("http://web.mit.edu/", ref o, ref o, ref o, ref o);
I also read the output from running csc /? to see what the available C# compiler commands were, and was able to come up with the following command for compilation:
csc.exe /r:lib\SHDocVw.dll,lib\AxSHDocVw.dll /out:.\debug\pullrank.exe
/recurse:src\*
When I was finished, Test.cs looked like this:
namespace pullrank {

  using System;
  using SHDocVw;
  using AxSHDocVw;

  public class Test {
    public static void Main(string[] args) {
      InternetExplorer ie = new InternetExplorer();
      ie.Visible = true;
      object o = null;
      ie.Navigate("http://web.mit.edu/", ref o, ref o, ref o, ref o);
    }
  }

}
I was able to compile this new version of Test.cs using the command above. When I tried running pullrank.exe in the debug/ directory, it crashed on me. However, when I added the two dlls to the debug/ directory, everything worked. I think that the trick is that the dlls have to be listed in the %PATH% for this to work, just like Java jars have to be in your %CLASSPATH%. Either way, the important thing is that I was now able to launch an IE browser and load a particular URL with code by running an executable I had written in C#!

Aside: As I mentioned before, I'm mainly a Java programmer. Since C# parallels Java in many ways, I was extremely disappointed that I could not find a C# equivalent to Java's easily navigable API. For example, when I look at the InternetExplorer documentation, it isn't clear what namespace it belongs to, what other objects it extends, etc. Perhaps the problem is that this is the VB documentation instead of the C# documentation, but even after repeated Google searches, I could find no such C# documentation! By comparision, the Java API is clearly organized hierarchically with hyperlinked cross-references. Do I have to be an MSDN subscriber or something to enjoy such luxuries for C#?


References:
  1. InternetExplorer Object API
  2. Helpful Post by Dumky: "Hosting a web browser component in a C# winform"
  3. Java 1.4.2 API


©2004 Michael Bolin » bolinfest@gmail.com www.bolinfest.com