Subscribing to events in external applications

From MediaMonkey Wiki
Jump to navigation Jump to search

Subscribing to events

In MediaMonkey 4.0 and above (see bug tracking entry), external applications can subscribe to MediaMonkey events in the same way as they register to other events.


In older versions however, subscribing to events does not work with external applications. See:

A solution is presented in:

Walkthrough

This solution is based on making your assembly ComVisible, registering it and letting an auto-script in MediaMonkey create your application object. This can be accomplished by creating a class along the lines under.

  • First build and run your program, then call the RegisterCOM() Method to register your application with COM.

(continued after C#-code)

using System.Reflection;
using System.Runtime.InteropServices;
using SongsDB;

namespace SomeNamespace {
  [ComVisible(true)] // this overrides [assembly: ComVisible(false)] (is set by project options dialog) so registry don't get bloated with unneeded types
  public class EventTest {
    public SDBApplication SDB { get; private set; }

    public static void RegisterCOM() {
      RegistrationServices rs = new RegistrationServices();
      rs.RegisterAssembly(Assembly.GetExecutingAssembly(),
        AssemblyRegistrationFlags.SetCodeBase);
    }
    public static void UnRegisterCOM() {
      RegistrationServices rs = new RegistrationServices();
      rs.UnregisterAssembly(Assembly.GetExecutingAssembly());
    }
    public void Init(SDBApplication mm) {
      this.SDB = mm;
      this.SDB.set_Objects(Assembly.GetExecutingAssembly().GetName().Name, this);
      this.SDB.OnPlay += () => { System.Windows.MessageBox.Show("OnPlay event fired!"); };
    }
  }
}
  • Add an autoscript-to mediamonkey (ie. a file in ...\MediaMonkey\Scripts\Auto) named SomeScript.vbs that starts your application and runs the Init function. It's contents should be like this:
Sub OnStartup
   Set o = CreateObject("SomeNamespace.EventTest")
   o.Init(SDB)
End Sub
  • When you now start MediaMonkey it will connect to your application and event firing will work.