I'm using a COM Wrapper to interact with Windows Media Player.
The it is using an AxHost to somehow wrap the player, for me it's all just magic under the hood^^
The AxHost.AttachInterfaces looks like this
protected override void AttachInterfaces()
{
try
{
//Get the IOleObject for Windows Media Player.
IOleObject oleObject = this.GetOcx() as IOleObject;
//Set the Client Site for the WMP control.
oleObject.SetClientSite(this as IOleClientSite);
Player = this.GetOcx() as WMPLib.WindowsMediaPlayer;
...
Everything is working find as long as I host this AxHost in a Windows Forms control. But I can't hook up the events in a constructor.
This for example doesn't work:
public WMPMediaRating()
{
var remote = new WMPRemote.RemotedWindowsMediaPlayer();
_WMP = remote.Player;
_WMP.MediaChange += new _WMPOCXEvents_MediaChangeEventHandler(_WMP_MediaChange);
}
remote.Player is always null and the program crashes with a NullReferencesException.
The code in AttachInterfaces() is somehow only executed after the Form has been drawn, or after everything else is done.
I tried calling AttachInterfaces() by hand, but that didn't work either because GetOcx() returns nothing.
So how can I instantiate my AxHost-inherited control without Windows Forms, to use it for example in a console application?
FYI: nobody stops you from using a hidden window in your console application.
You'll not be able to host the media player in a non-windows application - it requires hosting. If you want to play some music you can use the Media Graphs to create a graph that renders (plays) your music file - it'll not require any extra hosting.