Click here to Skip to main content
Click here to Skip to main content
Go to top

Late Bound ActiveX Loading [TamilFM]

, 26 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Late Bound ActiveX loading [TamilFM]
Sample Image - TamilFM_Screen_Shot.jpg

Introduction

This project was inspired by SafeCOMWrapper.

There are several problems using ActiveX from .NET:

  • You cannot guarantee ActiveX references are finalized.
  • When you "Add Reference..." to an ActiveX library, the reference is version specific. So, if you add a reference to the MPlayer11 ActiveX library, it does not work properly when deployed to MPlayer10.
  • The only solution to version independent ActiveX uses Late Bound operations but you miss all the features of a strongly typed language.

Let's solve all these problems by using COMWrapper and AxHost classes. To learn more about the late bound COM usage in .NET, refer to COMWrapper.

Background

This project was highly inspired and influenced by SafeCOMWrapper.

Please refer to this project before proceeding further.

Introducing AxHost and Using for Late Bound ActiveX

AxHost class wraps ActiveX controls and exposes them as fully featured Windows Forms controls. AxHost class was provided by Forms namespace:

public class AxVLCPlayer : AxHost
    {
        /// <span class="code-SummaryComment"><summary></span>
        /// vlc ActiveX control default interface
        /// <span class="code-SummaryComment"></summary></span>
        IVLCCore ocx; 

        public AxVLCPlayer(string strClaid)
            : base(strClaid)
        {

        } 
        /// <span class="code-SummaryComment"><summary></span>
        /// must be overridden to initialize the ocx field
        /// <span class="code-SummaryComment"></summary></span>
        protected override void AttachInterfaces()
        {
            try
            {
                this.ocx = (IVLCCore)COMWrapper.Wrap(this.GetOcx(), typeof(IVLCCore));
            }
            catch (System.Exception)
            {
            }
        } 
}

To load the ActiveX control in C#, we must create a new class from the AxHost class. From the new class, we must call the base (AxHost) class's one parameter constructor; the constructor argument would be the typelib guid of the ActiveX control which we want to load.

The new class will look like the one shown above.

The new class must override the AttachInterfaces() method to access its default interface. The below statement will attach the ActiveX control default interface in the late bound method.

this.ocx = (IVLCCore)COMWrapper.Wrap(this.GetOcx(), typeof(IVLCCore));

The IVLCCore interface must be declared manually:

    public interface IVLCCore : IDisposable
    {
        bool AutoPlay
        {
            get;
            set;
        }

        string MRL
        {
            get;
            set;
        }
        bool Playing
        {
            get;
        }
        int Volume
        {
            get;
            set;
        }
        void addTarget(string uri, object options, VLCPlaylistMode mode, int position);
        void playlistClear();
        void play();
        void stop();
    }

And the interface methods are exposed as shown below:

/// <span class="code-SummaryComment"><summary></span>
/// enable autoplay mode
/// <span class="code-SummaryComment"></summary></span>
public bool AutoPlay
{
    get
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("AutoPlay", AxHost.ActiveXInvokeKind.PropertyGet);
        return ocx.AutoPlay;
    }
    set
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("AutoPlay", AxHost.ActiveXInvokeKind.PropertySet);
        ocx.AutoPlay = value;
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// get/set mrl links
/// <span class="code-SummaryComment"></summary></span>
public string MRL
{
    get
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("MRL", AxHost.ActiveXInvokeKind.PropertyGet);
        return ocx.MRL;
    }
    set
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("MRL", AxHost.ActiveXInvokeKind.PropertySet);
        ocx.MRL = value;
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// check whether the player is playing its content or not
/// <span class="code-SummaryComment"></summary></span>
public bool Playing
{
    get
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("Playing", AxHost.ActiveXInvokeKind.PropertyGet);
        return ocx.Playing;
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// get/set volume
/// <span class="code-SummaryComment"></summary></span>
public int Volume
{
    get
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("Volume", AxHost.ActiveXInvokeKind.PropertyGet);
        return ocx.Volume;
    }
    set
    {
        if (ocx == null)
            throw new AxHost.InvalidActiveXStateException
            ("Volume", AxHost.ActiveXInvokeKind.PropertySet);
        ocx.Volume = value;
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// add target to play list
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="uri">URL</param></span>
/// <span class="code-SummaryComment"><param name="options">most cases null</param></span>
/// <span class="code-SummaryComment"><param name="mode">VLCPlaylistMode</param></span>
/// <span class="code-SummaryComment"><param name="position">in play list</param></span>
public void addTarget
    (string uri, object options, VLCPlaylistMode mode, int position)
{
    if (ocx == null)
        throw new AxHost.InvalidActiveXStateException
        ("addTarget", AxHost.ActiveXInvokeKind.MethodInvoke);
    ocx.addTarget(uri, options, mode, position);
}

/// <span class="code-SummaryComment"><summary></span>
/// clear play list
/// <span class="code-SummaryComment"></summary></span>
public void playlistClear()
{
    if (ocx == null)
        throw new AxHost.InvalidActiveXStateException
        ("playlistClear", AxHost.ActiveXInvokeKind.MethodInvoke);
    ocx.playlistClear();
}

/// <span class="code-SummaryComment"><summary></span>
/// start/resume play
/// <span class="code-SummaryComment"></summary></span>
public void play()
{
    if (ocx == null)
        throw new AxHost.InvalidActiveXStateException
        ("play", AxHost.ActiveXInvokeKind.MethodInvoke);
    ocx.play();
}

/// <span class="code-SummaryComment"><summary></span>
/// stop playing
/// <span class="code-SummaryComment"></summary></span>
public void stop()
{
    if (ocx == null)
        throw new AxHost.InvalidActiveXStateException
        ("stop", AxHost.ActiveXInvokeKind.MethodInvoke);
    ocx.stop();
}

Finally our ActiveX wrapper class is ready. The next thing is just to create the object for this wrapper class and use it as other .NET class objects.

///Initialize the vlc media player
vlcPlayer = new AxVLCPlayer(getClsidFromProgId("VideoLAN.VLCPlugin.1"));
            ((System.ComponentModel.ISupportInitialize)(vlcPlayer)).BeginInit();

 vlcPlayer.Enabled = true;
 vlcPlayer.Location = new System.Drawing.Point(0, 9);
 vlcPlayer.Name = "vlcPlayer";
 vlcPlayer.Size = new System.Drawing.Size(55, 50);
                  ((System.ComponentModel.ISupportInitialize)(vlcPlayer)).EndInit();

///workaround for vlc player size problem
vlcPlayer.Location = new System.Drawing.Point(0, 9);
vlcPlayer.Size = new System.Drawing.Size(55, 50);
vlcPlayer.AutoPlay = true;

That’s all folks. Our ActiveX class is ready.

Conclusion

I'd like to thank my brother (RathaKrishnan) for doing some quick testing for me. Of course there might be a few bugs left for all I know. But I am hoping on getting some quality feedback and suggestions.

History

  • 26th October, 2006: Initial version 2.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kannan K
Web Developer
India India
Kannan K living in india, he likes cricket as like all indians,loves to code in C#,VC++ languages.

Comments and Discussions

 
GeneralEmbed the player in a WEB page to play an aac+ streaming PinmemberJulio Villarroel14-Jul-09 9:23 
Generalon addTarget PinmemberChandrasekharanp21-Feb-08 23:08 
QuestionDispose Pinmembermailhacker5-Apr-07 21:14 
QuestionVisio problem Pinmemberivan.bolcina27-Feb-07 14:16 
QuestionHow to show OCX version Pinmembergxdata31-Oct-06 4:59 
AnswerRe: How to show OCX version PinmemberKannan K2-Nov-06 19:43 
GeneralRe: What about event handling Pinmemberesub10-Nov-08 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 27 Oct 2006
Article Copyright 2006 by Kannan K
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid