spacer1
spacer2 1_1 1_2
2_1
 Subscribe
 The MP2K Update!
 
 
 
 Magazine
Front Cover
What's New
Articles
News
Sample Data
Gallery
Advertise
About
 Features
MapPoint 2013
Press Releases
MapPoint Forums
Companies
Link to MP2Kmag
Wish List
MapPoint Trial
Authors
 Earlier Content
Past News Items
Past What's New Announcements
 Sponsors
 Order

MapPoint 2013

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
SVG Tutorials
MapPoint

Map Visitors

  ARTICLES  


Calling MapPoint from C# with Late Binding

Richard Marsden shows how to call MapPoint from C# using late binding or dynamic invocation. Late binding has a number of advantages in certain scenarios including being about to support multiple type libraries.

There are a number of articles on MP2K Magazine 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'. Eg.

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

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 which 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 runtime 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 - eg. 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 (eg. 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: Gordon Brown’s popularity ratings currently make George Bush look like a popular President).

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:

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 compiler errors; but it can be very useful if you need to support multiple versions of MapPoint in .NET.

Discuss this story in the forum.

Author: Richard Marsden
Email: enquiries(AT)winwaed.com
URL: http://www.winwaed.com
Richard Marsden is the proprietor of Winwaed Software Technology, LLC which provides software consulting and development services, specializing in both MapPoint and online mapping applications. He operates the Mapping-Tools.com Website for MapPoint Tools and Utilities, and recently launched the GeoWeb Guru a community website for developers of the geospatial web. In 2008, Richard was awarded Virtual Earth MVP status by Microsoft.

Prior to Winwaed, Richard worked as a software developer working on seismic processing algorithms for the oil exploration industry. He holds geology and geophysics degrees from the University of Cambridge (Churchill College), and the University of Durham; and an interdisciplinary MBA from the University of Dallas.



Google
 
MP2Kmag Internet


 Recent Discussion
 Resources
Browse GIS books and periodicals
Find a MapPoint Partner or Consultant
Real Estate Columbia MO Homes for Sale


Want Your Site To Appear Here?

   © 1999-2012 MP2K. Questions and comments to: website@mp2kmag.com
  Microsoft and MapPoint 2002/2004/2006/2009/2010/2011/2013 are either trademarks or registered trademarks of Microsoft.