Thanks to WickedEwok I have gotten past the automation issue. There were actually two different problems with my code:
1. I was blindly trying to instantiate a new MC object even if MC was already running, which results in the weird hang/exception described earlier. I followed his method of finding MC first, and using Marshal.GetActiveObject in that case.
2. I was instantiating MJAutomationClass instead of plain ol' MJAutomation. I still don't understand why my code compiled and ran, and I don't understand how MJAutomation is correct since it's not in the registry or object browser, but it works.
(Finally, there was a third problem with how I was attempting to stuff the playlist names into my app's list box... I was using the Text property instead of the Items property.)
Here's the corrected code:
try
{
MJAutomation aut = null;
IntPtr mcWindow = FindWindow("MJFrame", null);
if (IntPtr.Zero != mcWindow)
{
aut = (MJAutomation) Marshal.GetActiveObject("MediaJukebox Application");
}
if (null == aut)
{
// if MC is already running when we try to do the following, we'll get this exception:
// System.Runtime.InteropServices.COMException (0x80080005): Server execution failed
aut = new MJAutomation();
aut.ShowProgram(1);
}
MJPlaylistsAutomation playlists = aut.GetPlaylists();
for (int i = 0; i < playlists.GetNumberPlaylists(); i++)
{
MJPlaylistAutomation playlist = playlists.GetPlaylist(i);
Debug.WriteLine("Path: " + playlist.Path);
Debug.WriteLine("Name: " + playlist.Name);
listPlaylists.Items.Add(playlist.Name);
}
}
catch (Exception err)
{
Debug.WriteLine("butGetPlaylists_Click: " + err.ToString());
//MessageBox.Show("butGetPlaylists_Click: " + err.ToString());
}