Frage

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?

War es hilfreich?

Lösung

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));
   } 
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top