Integrating Mapinfo and .NET C#

1k views Asked by At

I'm developing an app in MapBasic that calls methods in a .NET assembly library

This is how I Declare the method in my MapBasice enviroment.

Declare Method showMainWindow Class "mynamespace.AldebaranInterface" Lib "Aldebaran.dll" ()

Sub ShowWindow
    Call showMainWindow()
End Sub

Sub formEventHandler
    'Reacting to some button click. I need to call this Sub from somewhere in mynamespace.AldebaranInterface
End Sub

What I need is to callback some Sub or Funcion in my MapBasic application from my .NET C# code. Let's say execute some portion of MapBasic code when the user clicks some button that is in my .NET form.

Is there a way to do that? If so. How can I achieve it? Any Help would be highly appreciated.

1

There are 1 answers

1
Kevin Malone On

I'm not sure what is your .dll is supposed to do, I guess it's a form, since you need your mapbasic code to react to button click.

In order to send command to mapbasic you need to create instance of Mapinfo’s COM object. Here is the link on how to do it. I personally use second approach.

So after you create class:

public class Mapinfo
{
   private object mapinfoinstance;
   public Mapinfo(object mapinfo)
   {
     this. mapinfoinstance = mapinfo;
   }

   public static Mapinfo CreateInstance()
   {
        Type mapinfotype = Type.GetTypeFromProgID("Mapinfo.Application");
        object instance = Activator.CreateInstance(mapinfotype);
        return new Mapinfo(instance);
    }

    public void Do(string command)
    {
          parameter[0] = command;
          mapinfoType.InvokeMember("Do",
                    BindingFlags.InvokeMethod,
                    null, instance, parameter);
     }

     public string Eval(string command)
     {
         parameter[0] = command;
         return (string)mapinfoType.InvokeMember("Eval", BindingFlags.InvokeMethod,
                             null,instance,parameter);
      }
}

, you need to add button clicked event:

appMapInfo = Mapinfo.CreateInstance();

//It's good idea to pass this path string from your MapBasic code
//as method parameter in your .dll 
string appMapInfoFilePath = @"D:\YourMBXPath\YourMBX.MBX";

//argumet you want to pass to MapBasic code
string argForMapBasic = ...;    

string runCommand;
string subCommand;

subCommand = "dim i_chan_num as integer i_chan_num = DDEInitiate(\"\"MapInfo\"\",\"\"" + appMapInfoFilePath + "\"\")";
subCommand = subCommand + " DDEExecute i_chan_num, \"\"" + argForMapBasic + "\"\"  DDETerminate i_chan_num";
runCommand = String.Format("Run Command \"{0}\"", subCommand);
appMapInfo.Do(runCommand);

Now on MapBasic side, you should create Sub RemoteMsgHandler (MapBasic Reference: A reserved procedure name, called when a remote application sends an execute message.)

Sub RemoteMsgHandler 

    Dim command as String 

    'command - string passed from your C# code (string argForMapBasic)
    command = CommandInfo(CMD_INFO_MSG) 

    'pass a command to your procedure
    Call yourProcedure(command)

End Sub