Using C# to Create a Plug-in

banner art

Previous Next

Using C# to Create a Plug-in

To create a plug-in using C# and Microsoft Visual Studio® .NET, complete the following steps:

  1. Start Microsoft Visual Studio .NET and, on the File menu, click New and then click Project.

  2. In the New Project dialog box, click Visual C# Projects and click the Class Library template. Give the project a name, specify a location for your project, and click OK.

  3. Click Add Reference on the Project menu. Click the .NET tab in the Add Reference dialog box and select the Microsoft.WindowsMediaServices component. Click OK.

  4. Open the Class1.cs file and add the following namespaces.

    using Microsoft.WindowsMediaServices.Interop;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    

    Microsoft.WindowsMediaServices.Interop is the namespace generated from the Windows Media Services type library. The System.Runtime.InteropServices namespace contains attributes needed to generate and register a COM object. The Microsoft.Win32 namespace contains functions that can be used to create specialized registration entries.

  5. Declare a class to represent your plug-in object and inherit from the IWMSBasicPlugin object. You must also inherit from the specialized interfaces needed by the type of plug-in you are creating. For example, an event notification plug-in must inherit from the IWMSEventNotificationPlugin object. This is illustrated by the following example.

    public class CSEventPlugin : IWMSBasicPlugin,
    IWMSEventNotificationPlugin
    {
    

  6. Use the GuidAttribute class in the System.Runtime.InteropServices namespace to add an explicit GUID to the class that represents your plug-in. You can drop Attribute from the name when using a .NET class that ends with Attribute. You can use the GuidGen.exe utility that ships with Visual Studio .NET to generate a GUID.

    [Guid("E1171A34-1F13-46d9-AA8A-63F47E92207C")]
    public class CSEventPlugin : IWMSBasicPlugin, IWMSEventNotificationPlugin
    {
    

  7. Override the IWMSBasicPlugin methods. Your implementation of these methods depends upon the nature of your plug-in. The following example illustrates the minimal implementation.

    void IWMSBasicPlugin.InitializePlugin( IWMSContext ServerContext,
    WMSNamedValues NamedValues,
    IWMSClassObject ClassFactory)
    {
    }

void IWMSBasicPlugin.ShutdownPlugin() { }

void IWMSBasicPlugin.EnablePlugin( ref int lFlags, ref int lHeartbeatPeriod) { }

void IWMSBasicPlugin.DisablePlugin() { }

object IWMSBasicPlugin.GetCustomAdminInterface() { return 0; }

void IWMSBasicPlugin.OnHeartbeat() { }

  1. Override the methods required by the specific type of plug-in you are creating. For example, if you are creating an event notification plug-in, you must override methods in the IWMSEventNotificationPlugin interface. This is illustrated by the following example.

    // Overrides the IWMSEventNotificationPlugin.OnEvent method. This
    // implementation displays a message box when a client either
    // connects or disconnects.
    public void OnEvent(
    ref WMS_EVENT Event,
    IWMSContext UserCtx,
    IWMSContext PresentationCtx,
    IWMSCommandContext CommandCtx )
    {
    try
    {
    switch (Event.Type)
    {
    case WMS_EVENT_TYPE.WMS_EVENT_CONNECT:
    MessageBox.Show("Client connected",
    "Event Plug-in",
    MessageBoxButtons.OK);
    break;

    case WMS_EVENT_TYPE.WMS_EVENT_DISCONNECT: MessageBox.Show("Client disconnected", "Event Plug-in", MessageBoxButtons.OK); break; } } catch( Exception e) { MessageBox.Show(e.Message, "Event Plug-in Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

// Overrides the IWMSEventNotificationPlugin.GetHandledEvents method. public object GetHandledEvents() { int[] iHandledEvents = new int[2]; iHandledEvents[0] = (int) WMS_EVENT_TYPE.WMS_EVENT_CONNECT; iHandledEvents[1] = (int) WMS_EVENT_TYPE.WMS_EVENT_DISCONNECT; return(iHandledEvents); }

  1. Create functions that modify the registration process so that Windows Media Services can find your plug-in. You can use the Regasm.exe utility that ships with Visual Studio .NET to register an assembly, but Windows Media Services requires more information about your plug-in than it can find in the registry settings created by Regasm. However, Regasm can call additional methods implemented by your plug-in to supply extra information to the registry. You can use the ComRegisterFunctionAttribute and the ComUnregisterFunctionAttribute classes to identify the appropriate methods to call, and you can use the Registry and RegistryKey classes in the Microsoft.Win32 namespace to create and modify registry keys. This is illustrated for an event notification plug-in by the following example. For more information about registry settings, see Registering Plug-ins.

    [ComRegisterFunctionAttribute]
    public static void RegisterFunction(Type t)
    {
    try
    {
    RegistryKey regHKLM = Registry.LocalMachine;
    regHKLM = regHKLM.CreateSubKey("SOFTWARE\Microsoft\Windows
    Media\Server\RegisteredPlugins\Event Notification
    and Authorization\
    {E1171A34-1F13-46d9-AA8A-63F47E92207C}");
    regHKLM.SetValue(null, "Sample CSEvent Notification");

    RegistryKey regHKCR = Registry.ClassesRoot; regHKCR = regHKCR.CreateSubKey("CLSID\ {E1171A34-1F13-46d9-AA8A-63F47E92207C}\ Properties"); regHKCR.SetValue("Name", "Sample CSEvent Notification"); regHKCR.SetValue("Author", "XYZ Corporation"); regHKCR.SetValue("CopyRight", "Copyright 2002 . All rights reserved"); regHKCR.SetValue("Description", "Enables you to trap the connect and disconnect events"); } catch(Exception error) { MessageBox.Show(error.Message, "Inside RegisterFunction(). Cannot Register", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

[ComUnregisterFunctionAttribute] public static void UnRegisterFunction(Type t) { try { RegistryKey regHKLM = Registry.LocalMachine; regHKLM.DeleteSubKey("SOFTWARE\Microsoft\Windows Media\Server\RegisteredPlugins\Event Notification and Authorization\{E1171A34-1F13-46d9-AA8A-63F47E92207C}");

RegistryKey regHKCR = Registry.ClassesRoot;
regHKCR.DeleteSubKeyTree("CLSID\\{E1171A34-1F13-46d9-AA8A-
                         63F47E92207C}");
regHKCR.DeleteSubKeyTree("CSEventTest.CSEventPlugin");

} catch(Exception error) { MessageBox.Show(error.Message, "Cannot delete a subkey.", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

  1. Build your plug-in. You can register the assembly manually or by using Visual Studio. To register the assembly manually, copy it to <%systemroot%>/system32/windows media/server, and run the Regasm.exe utility to register it and create a type library.

        regasm CSEventTest.dll /tlb
    

    To register the assembly by using Visual Studio, in the Solution Explorer, right-click the name of the assembly and click Properties. In the property pages dialog box, click Configuration Properties and click Build. Change the Register for COM Interop property to true. This process automatically configures the system registry.

    Refresh the server so that it can recognize your plug-in.

    For more information about registering your plug-in, see Registering a .NET Plug-in. You must refresh the server before it can recognize your plug-in.

See Also

Previous Next