Opening a URL in windows explorer without COM Object

518 Views Asked by At

The problem

I'm trying to open a URL in internet explorer. Regularly I would use the Navigate method of the Internet Explorer com object. However that isn't available in the programming language I am using (MapBasic).

My next method would be to use the Shell function to execute a command in the command line. For example:

explorer "http://yahoo.com"

Will navigate to http://www.yahoo.com. However when it comes to longer links, e.g.

https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8

Command prompt actually navigates to:

https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbn

Which is a significantly shorter URL.

To solve this issue I am looking into using the Win32API, but I can't for the life of me figure out how I could run this code with Win32API... Perhaps using Shell32.dll's ShellExecute? Or would it be better to use Kernel32.dll's CreateProcess?

Whichever is used, it would be really helpful if someone could supply me with some example code of how to open the above link with IE using the Win32 APIs in VB/C#. I can easily port the code to MapBasic from there!

(Alternatively if there is a method to open the link through shell, I would love to know how!)

Thanks

2

There are 2 best solutions below

0
On

As Hans Passant replied:

It is 259 characters long. Magic number. Don't use Explorer.exe, use a browser like iexplore.exe

This is exactly the solution I needed. Using iexplorer.exe instead:

"C:\Program Files\Internet Explorer\iexplore.exe" "https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8"

Opens the URL fine.


Similarly one can also use the following command:

start iexplore "https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8"

Which should be less system dependant.

1
On

You can just "execute" it using Process.Start which is under System.Diagnostics name space.