Question : Loading any ActiveX.ocx in VB.NET Form

Hi there,

I have a problem where I need to load an ActiveX control (VB6) inside a VB.NET form.
I can do this using the code (shown below).

The VB.NET application needs to be able to load any registered ActiveX ocx control at runtime.
The application doesn’t know what the ActiveX controls are just that they all will have the same procedures and properties.

The code below works. It displays any ActiveX control nicely. But the problem I have is that using this code I cannot use any of the ocx’s procedures or properties.
I‘ve searched high and low on the internet for a solution, but can’t find anything on the subject.

The reason behind wanting to do this is that we have so many ocx controls that perform so many different tasks, we cannot rebuild them in .NET.
So it’s a definite requirement that the application needs to be able to load any of our legacy VB6 ActiveX ocx controls.

I figure now that I’m going about this the wrong way?

Public Class Form1
    Private m_axCtrl As AxGenericControl

    Private ProgID As String = "GenericControlVB6.GenControl"

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim ControlType As Type = Type.GetTypeFromProgID(ProgID)
        Dim m_axCtrl As New AxGenericControl(ControlType.GUID.ToString)

        CType(m_axCtrl, System.ComponentModel.ISupportInitialize).BeginInit()

        SuspendLayout()

        m_axCtrl.Left = 0
        m_axCtrl.Top = 0
        m_axCtrl.Name = "OCX1"

        pnlScreen.Controls.Add(m_axCtrl)

        CType(m_axCtrl, System.ComponentModel.ISupportInitialize).EndInit()

        ResumeLayout(False)
    End Sub
End Class

Public Class AxGenericControl
    Inherits AxHost

    Public Sub New(ByVal CLSID As String)
        MyBase.New(CLSID)
    End Sub
End Class

Thanks in advance
Karl.

Answer : Loading any ActiveX.ocx in VB.NET Form

Ok, here's some ideas that I have discovered in the past, but never had a reason to use (the .NET framework is full of all sorts of new things to discover):

Using the Marshal class from the System.Runtime.InteropServices namespace:

HOW TO: Write a Custom Marshaler for COM Interoperation by Using Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;307713

GetComInterfaceForObject

It looks like you create a managed interface that describes the same members as the unmanaged interface, so that you can make references to the properties through the managed interface.

Bob
Random Solutions  
 
programming4us programming4us