1

我正在 MapBasic 中开发一个调用 .NET 程序集库中的方法的应用程序

这就是我在 MapBasice 环境中声明该方法的方式。

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

我需要的是从我的 .NET C# 代码中回调我的 MapBasic 应用程序中的一些 Sub 或 Funcion。假设当用户单击我的 .NET 表单中的某个按钮时执行 MapBasic 代码的某些部分。

有没有办法做到这一点?如果是这样。我怎样才能实现它?任何帮助将不胜感激。

4

1 回答 1

0

我不确定你的 .dll 应该做什么,我猜它是一个表单,因为你需要你的 mapbasic 代码来响应按钮点击。

为了向 mapbasic 发送命令,您需要创建 Mapinfo 的 COM 对象的实例。 是有关如何执行此操作的链接。我个人使用第二种方法。

所以在你创建类之后:

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);
      }
}

,你需要添加按钮点击事件:

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);

现在在 MapBasic 方面,您应该创建 Sub RemoteMsgHandler(MapBasic 参考:保留的过程名称,当远程应用程序发送执行消息时调用。

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 
于 2018-11-27T13:15:10.750 回答