Main Content

Integrate Magic Square into a COM Application


This example uses a simple MATLAB® file that takes a single input and creates a magic square of that size. It then builds a COM component using this MATLAB file as a class method. Finally, the example shows the integration of this component into a standalone Microsoft® Visual Basic® application. The application accepts the magic square size as input and displays the matrix in a ListView control box.


ListView is a Windows® Form control that displays a list of items with icons. You can use a list view to create a user interface like the right pane of Windows Explorer. See the MSDN Library for more information about Windows Form controls.

Creating the MATLAB File

To get started, create the MATLAB file mymagic.m containing the following code:

function y = mymagic(x); 
y = magic(x);

Using the Library Compiler App to Create and Build the Project

  1. While in MATLAB, open the Library Compiler app.

  2. Select Generic COM Component as the application type.

  3. Add mymagic.m to the list of exported functions.

    mymagic.m is located in the MagicDemoComp folder.

  4. Click the Package button.

Creating the Microsoft Visual Basic Project


This procedure assumes that you are using Microsoft Visual Basic 6.0.

  1. Start Visual Basic.

  2. In the New Project dialog box, select Installed > Templates > Other Languages > Visual Basic > Windows Form Application as the project type and click Open. This creates a new Visual Basic project with a blank form.

  3. From the main menu, select Project > References to open the Project References dialog box.

  4. Select magicdemo 1.0 Type Library from the list of available components and click OK.

  5. Returning to the Visual Basic main menu, select Project > Add Component... to open the Add New Item dialog box.

Creating the User Interface

After you create the project, add a series of controls to the blank form to create a form with the following settings.

Control TypeControl NamePropertiesPurpose



Caption = Magic Squares Demo

Groups controls



Caption = Magic Square Size

Labels the magic square edit box.




Accepts input of magic square size.



Caption = Create

When pressed, creates a new magic square with current size.



GridLines = True

LabelEdit = lvwManual

View = lvwReport

Displays the magic square.

When the form and controls are complete, add the following code to the form. This code references the control and variable names listed above. If you have given different names for any of the controls or any variable, change this code to reflect those differences.

Public Class magicvb
    Private sizeMatrix As Double 'Holds current matrix size
    Private theMagic As magicdemo.magicdemoclass 'magic object instance 

    Private Sub magicvb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'This function is called when the form is loaded.
        'Creates a new magic class instance.
        On Error GoTo Handle_Error
        theMagic = New magicdemo.magicdemoclass
        sizeMatrix = 0
        Exit Sub
    End Sub

    Private Sub ShowMatrix(matrixMagic As Object)
        'This function populates the ListView with the contents of 
        'y. y is assumed to contain a 2D array.
        Dim szSquare As Long
        Dim indxRow As Long
        Dim indxCol As Long
        Dim nLen As Long
        On Error GoTo Handle_Error
        'Get array size
        If IsArray(matrixMagic) Then
            szSquare = UBound(matrixMagic, 1)
            szSquare = 1
        End If
        For cIndx = 1 To szSquare
        lstMagic.View = View.Details
        For indxRow = 1 To szSquare
            Dim item As New ListViewItem(CStr(indxRow))
            For indxCol = 1 To szSquare
                item.SubItems.Add(Format(matrixMagic(indxRow, indxCol)))

        Exit Sub

    End Sub

    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        'This function is called when the Create button is pressed.
        'Calls the mymagic method, and displays the magic square.
        Dim matrixMagic As Object
        If sizeMatrix <= 0 Or theMagic Is Nothing Then Exit Sub
        On Error GoTo Handle_Error
        Call theMagic.mymagic(1, matrixMagic, sizeMatrix)
        Call ShowMatrix(matrixMagic)
        Exit Sub
    End Sub

    Private Sub edtSize_TextChanged(sender As Object, e As EventArgs) Handles edtSize.TextChanged
        'This function is called when ever the contents of the
        'Text box change. Sets the current value of Size.
        On Error Resume Next
        sizeMatrix = CDbl(edtSize.Text)
        If Err.Number > 0 Then
            sizeMatrix = 0
        End If
    End Sub

End Class

Creating the Executable in Microsoft Visual Basic

After the code is complete, create the standalone executable magic.exe:

  1. Reopen the project by selecting File > Save Project from the main menu. Accept the default name for the main form and enter magic.vbp for the project name.

  2. Return to the File menu. Select File > Make magic.exe to create the finished product.

Testing the Application

You can run the magic.exe executable as you would any other program. When the main dialog box opens, enter a positive number in the input box and click Create. A magic square of the input size appears.

The ListView control automatically implements scrolling if the magic square is larger than 4-by-4.