Personal tools
You are here: Home Members jacky OLE to MapInfo Professional
Document Actions

OLE to MapInfo Professional

by Ian Tidy last modified 2007-08-06 04:46

Sample code for using a C# Class Library to call MapInfo Professional

Calling MapInfo Professional from C# and Borland Delphi 8 is very similar. This code should work with Delphi with some minor tweaks. Borland Delphi 2005 is very different. The following instructions are based on Microsoft Visual Studio 2003.
Step 1. Create a new C# Class Library File->New->Visual C# Projects->Class Library.
using System;
using MapBasic;
using MapInfo;


namespace MI_CS_Sample
{
	/// 
	/// Sample DLL for MapInfo Professional 8
	/// 
	public class csMapInfo 
	{
		private bool bTest = false;
		public csMapInfo()
		{

			//
			// TODO: Add constructor logic here
			//
			csMIForm shForm;
			shForm = new csMIForm();
			shForm.ShowDialog();
		}

		/// 
		/// //This is a test property
		/// 
		public bool Extra
		{
			get
			{
				return true;
			}
			set
			{
			}
		}
	}
}
Step 2. Add Additional Forms to your project File->Add New Item->Windows Form
using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

// MapInfo Specific Uses
using System.Runtime.InteropServices;
using MapInfo;
using MapBasic;


namespace MI_CS_Sample
{
	/// 
	/// Summary description for csMIForm.
	/// 
	public class csMIForm : System.Windows.Forms.Form
	{
		// MapInfo OLE Connection
		private MapInfo.MapInfoApplication oleMI;

		private System.Windows.Forms.Button btnConnect;
		private System.Windows.Forms.ListBox lbMapInfo;
		/// 
		/// Required designer variable.
		/// 
		private System.ComponentModel.Container components = null;

		public csMIForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// 
		/// Clean up any resources being used.
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 
		private void InitializeComponent()
		{
			this.btnConnect = new System.Windows.Forms.Button();
			this.lbMapInfo = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// btnConnect
			// 
			this.btnConnect.Location = new System.Drawing.Point(8, 8);
			this.btnConnect.Name = "btnConnect";
			this.btnConnect.Size = new System.Drawing.Size(408, 56);
			this.btnConnect.TabIndex = 0;
			this.btnConnect.Text = "Connect to MapInfo Professional";
			this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
			// 
			// lbMapInfo
			// 
			this.lbMapInfo.Location = new System.Drawing.Point(8, 72);
			this.lbMapInfo.Name = "lbMapInfo";
			this.lbMapInfo.Size = new System.Drawing.Size(400, 121);
			this.lbMapInfo.TabIndex = 1;
			// 
			// csMIForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(424, 266);
			this.Controls.Add(this.lbMapInfo);
			this.Controls.Add(this.btnConnect);
			this.Name = "csMIForm";
			this.Text = "csMIForm";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.csMIForm_Closing);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnConnect_Click(object sender, System.EventArgs e)
		{
			string sRes = "";  // Used to store MapInfo results
			string sCmd = "";  // Used to issue commands to MapInfo

			#region Connect to MapInfo Professional
			oleMI = new MapInfo.MapInfoApplicationClass();
			try
			{
				oleMI = (MapInfo.MapInfoApplication) Marshal.GetActiveObject("MapInfo.Application");
			}
			catch 
			{
				MessageBox.Show("Unable to connect to MapInfo Professional");
				Application.Exit();
			}
			#endregion

			#region Get Stuff From MapInfo
			try
			{
				// Display a Note and Print Command
				sCmd = "OLE Connection Created";
				oleMI.Do("Print " + '\u0022' + sCmd + '\u0022');

				// Get the Number of Windows
				sCmd = "NumWindows()";
				sRes = oleMI.Eval(sCmd);
				lbMapInfo.Items.Add("Open Windows = " + sRes);

                // Get the Number of Tables
				sCmd = "NumTables()";
				sRes = oleMI.Eval(sCmd);
				lbMapInfo.Items.Add("Open Tables = " + sRes);
			}
			catch
			{
				MessageBox.Show("An Error Has Occured.");
				Application.Exit();
			}
			#endregion
	
			#region Close Connection to MapInfo
			oleMI.RunMenuCommand(MapBasic.Constants.M_TOOLS_SELECTOR);
			oleMI = null;
			#endregion
		}

		private void csMIForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Application.Exit();
		}
	}
}

Step 3. Add the Interop.MapInfo.DLL and MapBasic.CS files as references Project->Add References->Projects->Browse (browse to Interop.MapInfo.DLL)->Select->OK and File->Add Existing Item->(Browse to MapBasic.CS)->Open

Key Parts

using System.Runtime.InteropServices; Provides COM interoperability.
using MapInfo; Allows you to use functions in the Interop.MapInfo.DLL.
using MapBasic; Allows you to use constants defined in the MapBasic.CS.
private MapInfo.MapInfoApplication oleMI; Creates a variable "oleMI" with OLE properties and methods exposed by the Interoperability DLL.
oleMI = new MapInfo.MapInfoApplicationClass(); Initializes the OLE variable.
oleMI = (MapInfo.MapInfoApplication) Marshal.GetActiveObject("MapInfo.Application"); Creates the actual OLE connection to a running instance of MapInfo Professional.
From this point if you want to run a menu command, you use .RunMenuCommand
oleMI.RunMenuCommand(MapBasic.Constants.M_TOOLS_SELECTOR);
Return a value back to your DLL, you use .Eval. A string is returned which you must test and cast to what you require.
// Get the Number of Windows
sCmd = "NumWindows()";
sRes = oleMI.Eval(sCmd);
lbMapInfo.Items.Add("Open Windows = " + sRes);
Just do something (execute a SQL Query) .Do.
// Display a Print Command
sCmd = "OLE Connection Created";
oleMI.Do("Print " + '\u0022' + sCmd + '\u0022');
oleMI = null; Don't forget to close your OLE connection when you have finished.
OK, this was the simple bit. This will build a managed C# DLL, the only problem is that MapInfo Professional cannot use it. To be able to call the DLL you must create a bridge DLL. But this code shows you how to establish an OLE call to MapInfo Professional.

Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: