Creating an instance of a Mapinfo COM object in .NET – Part Three

In part three of the series Creating a instance of a Mapinfo COM object in .NET, I’m going to be talking about creating an instance of Mapinfo’s COM object using Activator.CreateInstace but also allowing you to have strong typed access to Mapinfo’s members. This approach unlike the approach outlined in part two, will allow for your application to be Mapinfo version independent without having to use reflection to get access to Do and Eval

If you haven’t read part one and two I would recommend reading them first as it will give you an understanding of where this post is heading.

Step 1: Adding a referance to Mapinfo.

As this method requires us to get access to some of the interfaces that Mapinfo provides, we will need to add a reference to Mapinfo like we did in first step in part one.

To save on re-explaining the whole process here again, I will wait while you head over to part one and follow the process outlined in step 1. Make sure that you come back here after you have completed step 1.

Step 2: Creating the instance

Now that you are back, we can continue.

The process in this step is similar to the process outlined in Part two step 1, however things will differ a little bit.

If you have a look at the Interop.Mapinfo.dll in the object browser of visual studio you will notice that the class called MapinfoApplicationClass, implments a interface called DMapinfo.




















Does that MapinfoApplicationClass class look familiar? It should as it is what we used to create the Mapinfo instance in part one of this series, but we don’t need that class here so we will just forget about it. What we do need is that interface called DMapinfo as it provides all the methods that we need to interact with Mapinfo.

If we have the code that we used in part two step 1 for creating the instance of Mapinfo.

Type mapinfotype = Type.GetTypeFromProgID("Mapinfo.Application");
object instance = Activator.CreateInstance(mapinfotype);

now this code is great and all but we run into the problem that we had in part two where C# doesn’t support late binding so we have to use reflection which just feels dirty. There has to be a better way.

What object is Activator.CreateInstace returning in the above code anyway?

Turns out that the object that it returns also implements the interface DMapinfo, this is good for us as it allows us to cast the object returned from Activator.CreateInstance() to the type DMapinfo. If we go and add the right casting to the code it should now look like the following.

Type mapinfotype = Type.GetTypeFromProgID("Mapinfo.Application");
DMapInfo instance = (DMapInfo)Activator.CreateInstance(mapinfotype);

Cool, so we have created a instance of Mapinfo and casted it to the type DMapinfo now we should be able to do something useful with it.

Step 3: Using the object.

Because we have casted our object to the interface called DMapinfo, we can now get strongly typed access to Mapinfo’s Do and Eval method. No more reflection needed :).

Some sample code:

Type mapinfotype = Type.GetTypeFromProgID("Mapinfo.Application");
DMapInfo instance = (DMapInfo)Activator.CreateInstance(mapinfotype);

instance.Do("Print 1234567");
string value = instance.Eval("NumTables()");

More information on DMapinfo.

If we have a look at the GUID on the top of the DMapinfo interface you will notice that it looks something like this:

public interface DMapInfo

This GUID for DMapinfo, which unlike the GUID for the MapinfoApplicationClass is the same for every Mapinfo version.
Because it is the same we can create Mapinfo using Activator.CreateInstance() which will return a COM object, cast it DMapinfo and by making calls against the interface we don’t have to worry about what version of Mapinfo the client is running.

Summing up: Pros and Cons

In this post I have outlined how you can create an instance of Mapinfo using Activator.CreateInstance() and cast the return object to the interface called DMapinfo. This technique allows us to have Mapinfo version independence while still maintaining our type safety and compiler support.


  • Allows Mapinfo version independence.
  • Strong Typed
  • Cleaner then having to use reflection.
  • Faster method calling then using reflection.


  • No real cons

9 thoughts on “Creating an instance of a Mapinfo COM object in .NET – Part Three

  1. there seems to be some empty / missing bitmaps? Not graphic along with “Pros” and “Cons”. Also in step 2 after “DMapinfo.” there is a large empty space.

    1. Missing bitmaps hmm that’s strange, everything looks good on my end. I’ll try a different computer and see if I can see what you mean.

      What do you mean by “Not graphic along with “Pros” and “Cons””?

      There should be a picture of the VS object explorer in that big empty space after DMapinfo in step 2.

      is anyone else having problems with missing bitmaps?

      1. Using IE7 it all looks better. With IE6 there was no list of pros and cons and the VS object explorer bitmap was missing.

    1. Yes and no.

      Yes part yes you can attach to a running instance.
      No part no you can’t control which instance you attach to.

      This is the C# code you can use to get a copy of an already running instance of Mapinfo.

      MapInfoApplication mapinfo = (MapInfoApplication)Marshal.GetActiveObject(“Mapinfo.Application”);
      mapinfo.Do(“Print \”Hello World\””);

      This will work, however be aware as this will only return the first running instance of Mapinfo. If you have more then one running it will always return the first one that was launched.

  2. ок that’s cool but i still need help.

    I want to run Net app from MapInfo (like push the button) and connect MapinfoWrapper in code to current MapInfo instance???

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s