3

I have a plugin for a third-party application that calls a C#.NET method and I need to get the instance from the calling application.

_MapInfoApplication = (MapInfo.MapInfoApplication) System.Runtime.InteropServices.Marshal.GetActiveObject("Mapinfo.application");

Gives me an active instance from the application, but in the case there is two open instances of the application I can't know if I got the right one, is there way to determine who called?

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48
Ulrik
  • 546
  • 1
  • 5
  • 21

1 Answers1

1

Use the System.Runtime.InteropServices.Marshal.GetObjectForIUnknown method, passing an object pointer using the IDispatchID from MapInfo as a parameter:

public static void MINote(int MIWindowID, string Message)
{
   System.IntPtr MIDispatchPtr = new IntPtr(MIWindowID);
   DMapInfo MIConnection = (DMapInfo)Marshal.GetObjectForIUnknown(MIDispatchPtr);
   MIConnection.Do(String.Format("Note \"Note from CSharp: {0}\"",Message));
   DMBApplications Applications = (DMBApplications) MIConnection.MBApplications;
   foreach (DMapBasicApplication mbApp in Applications) 
   {
      MIConnection.Do(String.Format("Note \"MB App. running in this MapInfo instance: {0}\"", mbApp.Name));
   } 
}
chridam
  • 100,957
  • 23
  • 236
  • 235