Registering a .NET Plug-in

banner art

Previous Next

Registering a .NET Plug-in

Windows Media Services plug-ins are COM objects. You can create a COM object in C# or Visual Basic .NET by using the GuidAttribute class to associate a GUID with the object that implements your plug-in. The GuidAttribute class is in the System.Runtime.InteropServices namespace.

Visual Basic .NET Example

Imports Microsoft.WindowsMediaServices.Interop
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

<GuidAttribute("7DF709B2-3585-4201-BC50-4820ABE2BF18")> _
Public Class VBEventTest
    Implements IWMSBasicPlugin
    Implements IWMSEventNotificationPlugin

C# Example

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

namespace CSEventTest
{

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

When you build your plug-in, .NET creates an assembly with a .dll file name extension. If you use the Regasm.exe utility to register type information from the assembly, the following entries are added to the system registry.

HKEY_CLASSES_ROOT\progid
   (default) = progId

HKEY_CLASSES_ROOT\progid\CLSID
   (default) = clsid

HKEY_CLASSES_ROOT\CLSID\{clsid}
   (default) = progid

HKEY_CLASSES_ROOT\CLSID\{clsid}\InProcServer32
   (default) = %systemroot%\mscoree.dll
   Assembly = assembly name and version information
   Class = class name
   ThreadingModel = Both
   RuntimeVersion = version_of_the_runtime

HKEY_CLASSES_ROOT\CLSID\{clsid}\Implemented Categories\{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}

HKEY_CLASSES_ROOT\CLSID\{clsid}\ProgId
   (default) = progid

However, to enable Windows Media Services to recognize and use your plug-in, you must provide additional registry information. You can use the ComRegisterFunctionAttribute class to identify a registration function for Regasm.exe to call during the registration process. The following example creates subkeys in the HKEY_CLASSES_ROOT and HKEY_LOCAL_MACHINE keys to add registry information needed by the server.

Visual Basic .NET Example

<ComRegisterFunctionAttribute()> _
Shared Sub RegisterFunction(ByVal t As Type)
  Try
    Dim regHKLM As RegistryKey
    regHKLM = Registry.LocalMachine
    regHKLM = regHKLM.CreateSubKey("SOFTWARE\\Microsoft\\Windows Media\\Server\\RegisteredPlugins\\Event Notification and Authorization\\{7DF709B2-3585-4201-BC50-4820ABE2BF18}")
    regHKLM.SetValue(Nothing, "Sample VBEvent Notification")

    Dim regHKCR As RegistryKey
    regHKCR = Registry.ClassesRoot
    regHKCR = regHKCR.CreateSubKey("CLSID\\{7DF709B2-3585-4201-BC50-4820ABE2BF18}\\Properties")
    regHKCR.SetValue("Name", "Sample VBEvent 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 e As Exception
    MsgBox(e.Message, MsgBoxStyle.OKOnly)
  End Try
End Sub

C# Example

[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", "Event plug-in");
  }
  catch(Exception error)
  {
    MessageBox.Show(error.Message,
              "Inside RegisterFunction(). Cannot Register",
              MessageBoxButtons.OK,
              MessageBoxIcon.Error);
  }
}

It is recommended that you use the ComUnRegisterFuntionAttribute class to identify a registration function for Regasm.exe to call when an end user removes registry entries. This is illustrated by the following examples.

Visual Basic .NET Example

<ComUnregisterFunction()> _
Shared Sub UnRegisterFunction(ByVal t As Type)
  Try

    Dim regHKLM As RegistryKey
    regHKLM = Registry.LocalMachine
    regHKLM.DeleteSubKey("SOFTWARE\\Microsoft\\Windows Media\\Server\\RegisteredPlugins\\Event Notification and Authorization\\{7DF709B2-3585-4201-BC50-4820ABE2BF18}")

    Dim regHKCR As RegistryKey
    regHKCR = Registry.ClassesRoot
    regHKCR.DeleteSubKeyTree("CLSID\\{7DF709B2-3585-4201-BC50-4820ABE2BF18}")
    regHKCR.DeleteSubKeyTree("VBEventTest.VBEventPlugin")

  Catch e As Exception
    MsgBox(e.Message, MsgBoxStyle.OKOnly)
  End Try
End Sub

C# Example

[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);
  }
}

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.

To register your plug-in on a computer other than that on which it was built, copy the assembly to the <%systemroot%>/system32/windows media/server folder on that computer, and run the Regasm.exe utility to register it.

    regasm CSEventTest.dll

See Also

Previous Next