Share via


Using Visual Basic .NET to Create a Plug-in

banner art

Previous Next

Using Visual Basic .NET to Create a Plug-in

To create a plug-in using Visual Basic 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 Basic 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.vb file and add the following namespaces.

    Imports System
    Imports System.Runtime.InteropServices
    Imports Microsoft.WindowsMediaServices.Interop
    Imports 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 VBEventTest
    Implements IWMSBasicPlugin
    Implements 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 use the GuidGen.exe utility that ships with Visual Studio .NET to generate a GUID.

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

  7. Use the Implements keyword to override the IWMSBasicPlugin methods. Your implementation of these methods depends upon the nature of your plug-in. The following example illustrates the minimal implementation.

        Public Sub EnablePlugin( _
    ByRef plFlags As Integer, _
    ByRef plHeartbeatPeriod As Integer) _
    Implements IWMSBasicPlugin.EnablePlugin

    ' Set the heartbeat period to zero.
    plFlags = 0
    plHeartbeatPeriod = 0
    

    End Sub

    Public Sub DisablePlugin() _ Implements IWMSBasicPlugin.DisablePlugin

    End Sub

    Public Sub InitializePlugin( _ ByVal pServerContext As IWMSContext, _ ByVal pNamedValues As WMSNamedValues, _ ByVal pClassFactory As IWMSClassObject) _ Implements IWMSBasicPlugin.InitializePlugin

    End Sub

    Public Sub OnHeartBeat() _ Implements IWMSBasicPlugin.OnHeartbeat

    End Sub

    Public Sub ShutDownPlugin() _ Implements IWMSBasicPlugin.ShutdownPlugin

    End Sub

    Public Function GetCustomAdminInterface() As Object _ Implements IWMSBasicPlugin.GetCustomAdminInterface Return 0 End Function

  8. 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 Function GetHandledEvents() As Object _ Implements IWMSEventNotificationPlugin.GetHandledEvents

' Capture the connect and disconnect events.
Dim iHandledEvents(2) As Integer
iHandledEvents(0) = WMS_EVENT_TYPE.WMS_EVENT_CONNECT
iHandledEvents(1) = WMS_EVENT_TYPE.WMS_EVENT_DISCONNECT

Return iHandledEvents

End Function

Public Sub OnEvent( _ ByRef pEvent As WMS_EVENT, _ ByVal pUserCtx As IWMSContext, _ ByVal pPresentationContext As IWMSContext, _ ByVal pCommandCtx As IWMSCommandContext) _ Implements IWMSEventNotificationPlugin.OnEvent

Try
    Select Case pEvent.Type
        Case WMS_EVENT_TYPE.WMS_EVENT_CONNECT
            MsgBox("Client connected", _
                   MsgBoxStyle.OKOnly, _
                   "VB Sample Event Plug-in")

        Case WMS_EVENT_TYPE.WMS_EVENT_DISCONNECT
            MsgBox("Client disconnected", _
                   MsgBoxStyle.OKOnly, _
                   "VB Sample Event Plug-in")
        End Select

Catch e As Exception
    MsgBox(e.ToString, MsgBoxStyle.OKOnly, "OnEvent Error")

End Try

End Sub

  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()> _
    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

<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

  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 VBEventTest.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. Select the Register for COM Interop check box. 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