Export (0) Print
Expand All

© 2004 Microsoft Corporation. All rights reserved.

Figure 4 Sample .NET Client Program
Private Sub button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button1.Click

    ' Create RCW, which in turn creates COM object

    Dim foo As New ComServerUsedByDotNet.Class1()

    ' Make call to RCW, which forwards it to COM object and
    ' returns results in .NET format

    label1().Text = foo.GetTimeFromCom(checkBox1().Checked)

    ' RCW becomes garbage when it goes out of scope.
    ' COM object released at next garbage collection

End Sub

Private Sub button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button2.Click

    ' Create RCW, which in turn creates COM object

    Dim foo As New ComServerUsedByDotNet.Class1()

    ' Make call to RCW, which forwards it to COM object and
    ' returns results in .NET format

    label1().Text = foo.GetTimeFromCom(checkBox1().Checked)

    ' Force immediate release of RCW's internal COM object

    System.Runtime.InteropServices.Marshal.ReleaseComObject(foo)
End Sub
Figure 6 Late-binding Client
Private Sub button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button1.Click

    ' Get system type name based on prog ID

    Dim MyType As System.Type
    MyType = Type.GetTypeFromProgID(textBox1().Text)

    ' Use an activator to create object of that type

    Dim MyObj As Object
    MyObj = Activator.CreateInstance(MyType)

    ' Assemble array of parameters to pass to COM object

    Dim prms() As Object = {checkBox1().Checked}

    ' Call method on object by its name

    label2().Text = MyType.InvokeMember("GetTimeFromCom", _
    Reflection.BindingFlags.InvokeMethod, _
    Nothing, MyObj, prms).ToString()

End Sub
Figure 8 Registry Entries Made by RegAsm.exe

Figure 8 Registry Entries Made by RegAsm.exe
Figure 9 COM Error Handled by a .NET Client

Error-generating Code from Sample COM Server
Public Sub CauseAnError()
    Err.Raise vbObjectError + 999, "ComServerUsedByDotNet", _
    "Sample error raised by CauseAnError"
End Sub
Error-handling Code from Sample .NET Client
Private Sub button4_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button4.Click

    ' Create RCW, which in turn creates COM object

    Dim foo As New ComServerUsedByDotNet.Class1()

    Try

        ' Call a function that we know will throw an exception

        foo.CauseAnError()

    Catch ex As Exception

        ' Catch and handle exception

        Messagebox.Show("Caught .NET exception, source: " + ex.Source + _
                        " message: " + ex.Message)
    End Try
End Sub
Figure 10 .NET Error Handled by a COM Client

Error-generating Code from Sample .NET Server
Public Sub CauseAnError()
    Dim MyException As New Exception("From .NET Sample Server")
    Throw MyException
End Sub
Error-handling Code from Sample COM Client
Private Sub Command2_Click()
    On Error GoTo myEnd
    
    Dim foo As New Class1
    Call foo.CauseAnError
    Exit Sub

myEnd:
    MsgBox "Client trapped error, source: " _
            + Err.Source + " Description: " + Err.Description
End Sub
Figure 12 Sample Code of .NET Control
Public Class Control1

' Our control inherits from the CLR base class UserControl

    Inherits System.Windows.Forms.UserControl
    
    ' Our control exposes a public property called "ShowSeconds"

    Private myShowSeconds As Boolean
    
    Public Property ShowSeconds() As Boolean
        
        Get
            Return myShowSeconds
        End Get
        
        Set
            myShowSeconds = Value
        End Set
    End Property
    
' We override the OnPaint handler to do our own painting.
    
    Protected Overrides Sub OnPaint(ByVal e As _
        System.Windows.Forms.PaintEventArgs)

    ' code omitted...

    End Sub
End Class
Figure 14 .NET Registration as an ActiveX Control
Public Shared Sub <System.Runtime.InteropServices.ComRegisterFunction()> _
    AxRegister(ByVal regKey As String)
    Dim foo As New Control1()
    ActiveXRegister(foo.GetType)
    End Sub
    
Public Shared Sub <System.Runtime.InteropServices.ComUnregisterFunction()> _
    AxUnregister(ByVal regkey As String)
    Dim foo As New Control1()
    ActiveXUnregister(foo.GetType)
End Sub
Figure 18 Methods and Properties in the .NET Object Browser

Figure 18 Methods and Properties in the .NET Object Browser
Show:
© 2015 Microsoft