|
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:
using SHDocVw; using AxSHDocVw;
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 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: |
©2004 Michael Bolin » bolinfest@gmail.com | www.bolinfest.com |