Follow us on Twitter

Using Late Binding to call MapPoint from C#

Share:

There are a number of articles on the MapForums website about using MapPoint from a variety of different languages. Most if not all of these articles use early binding to reference the MapPoint COM objects, because this is usually the easiest way of working with COM and it tends to be safer due to compiler checking. Early binding is also slightly faster due to all the references being hard-coded. However there are times when late binding might be more appropriate. This article shows you how to call MapPoint from C# using late binding. Late binding is also known as “dynamic invocation”.

What is late binding?

Early and late binding refer to how the code refers to the MapPoint COM objects. Early binding is performed at compile time and is essentially ‘hard-coded’. E.g.

The compiler knows how myApp is defined, and can check that the Version property exists. To do this, you must have the type library file for the COM objects that are being used.

However, you might not have this type library, or you might want to support multiple type libraries. In this case, you have to write code that checks the interface of the COM object, and makes the call at run time. The above line becomes:

This is obviously a lot longer and more complicated! However, note that the property name is defined as string. This string could be changed at run time. The return type could also be changed at run time without forcing an inappropriate cast to occur.

So why would you want to use late binding, if it is so much work? Here are some possible reasons:

  • A type library might not be available at compile time
  • Easier to support multiple versions — e.g. You can check the version number before invoking a new method. Early binding would fail if you compiled with a type library that used a new method but you called an earlier version of the interface
  • Useful for generic programs that can interface to a wide range of object types (e.g. debuggers)

Sample code that uses late binding

So let’s see an example! This tongue-in-cheek example plans a road route through London for Gordon Brown’s eventual trip to Buckingham Palace when he dissolves Parliament (Footnote: When this was originally written, Gordon Brown’s poll ratings were still fairly high).

To use late binding, you will need to add namespace references for System.Reflection and System.Runtime.InteropServices:

No MapPoint object definitions are available, so our MapPoint objects are all defined as generic objects:

Parameters are passed to MapPoint methods using arrays of objects. This sample only needs to pass one or two parameters at a time, so define these next. Note that these arrays can be re-used – we do not have to define new arrays for each and every method call.

Let’s create a MapPoint instance. In order to create an object instance, we need a type. We obtain the type from a COM name by using Type.GetTypeFromProgID:

mp_app now contains a reference to the main MapPoint Application object. Properties and methods are invoked using the InvokeMember() method on the object’s type. A BindingFlags value specifies the operation to invoke. We can read properties like so:

Similarly, we can write properties:

A parameter has to be passed when writing a property. We do this by setting a one element array to the value that we need to pass. More parameters could be passed (eg. to a method) by passing a longer array.

Next we find two locations:

In both cases I have assumed that the first result is good. In a real application, you would check resultsQuality before reading the results collection (object fr).

Next we fetch a reference to the Route object and the Waypoints collection:

In order to set the waypoints, we must pass two parameters to the waypoints object. This is performed using a two element array instead of the previous one element array. As an aside, it should be noted that all option parameters should be set. This is a good general rule for both early and late binding calls to COM from .NET.

Next we calculate the route, and fetch any results that we might require:

This is what MapPoint now looks like:

MapPoint screenshot of the route created using late binding

C# and .NET do a good job of garbage collection and cleaning up after COM. All you need to do is to set the object references to null, or to let them pass out of scope. You should still call the Application.Quit method to finally close MapPoint:

And that is it! Late binding is more complicated, and it lacks the safety net of compile-time error detection; but it can be very useful if you need to support multiple conflicting versions of MapPoint in .NET.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">