Create DataMatrix with a Component Object Model (COM) Library

This tutorial illustrates the use of a COM (Component Object Model) object library with a True Type Font (DataMatrix Barcode Font), provided in ConnectCode DataMatrix package, to create a ISO/IEC 16022:2006 (ECC200) standard-compliant DataMatrix in a .NET Windows Form application.

Prerequisites

  • ConnectCode DataMatrix package is installed
  • DataMatrixCOMLibrary.dll in the Resource\DataMatrixCOMLibrary subdirectory of ConnectCode DataMtrix package.
    The DataMatrix class library has been compiled with the "Register for COM interop" Visual Studio project property, exposing a COM-callable wrapper that enables COM interaction.
  • Visual Studio 2015/2017/2019
  • Administrator Rights

Tutorial

1. Launch the Visual Studio Developer Command Prompt as Administrator from the Windows Start Menu. In the Command Prompt, use the "cd" command to go to the DataMatrix COM Library folder.

cd C:\Program Files (x86)\ConnectCodeDataMatrix\Resource\DataMatrixCOMLibrary (or ConnectCodeDataMatrixTrial if you are using the Trial package)

2. Enter the following command in the Developer Command Prompt to register the DataMatrix COMLibrary assembly for use with COM.


Regasm DataMatrixCOMLibrary.dll /tlb:DataMatrixCOMLibrary.tlb /codebase

Regasm.exe adds information about the class to the system registry so that COM clients can use the .NET Framework class transparently. The tlb option generates a type library defined within the assembly.

3. Launch Visual Studio and create a new Windows Form project by clicking on "File -> New Project", selecting a "Windows Forms App" and clicking on the "Create" button.

4. Double click on "Form1.cs" in the "Solution Explorer" and add a "Button" and a "RichTextBox" from the Visual Studio Toolbox. You should see your Windows Form like the screenshot below. We will set up the programming codes so that when we click on the button, the DataMatrix COM library will be loaded to encode an input string. The result of the encodation will be placed in the "RichTextBox" and applied with a DataMatrix barcode font.

5. Right click on the "WindowsFormApp1" project in the "Solution Explorer" and select "Add -> Existing Item". Navigate to the "C:\Program Files (x86)\ConnectCodeDataMatrix\" folder and select the "CCodeDataMatrix.ttf" font. If you are using the trial version, select the "CCodeDataMatrix_Trial.ttf" font instead.

6. In "Solution Explorer", select the "CCodeDataMatrix.ttf" font and change the "Build Action" to "Content" and "Copy to Output Directory" to "Copy Always" in the Properties pane. This will ensure that Visual Studio deploy the DataMatrix barcode font for use with the Windows Form application.

7. Double click on "Form1.cs" in the "Solution Explorer". In the designer, double click on the Button. This will generate the button1_Click function in the editor. Enter the C# programming codes as shown below:


       using System.Drawing.Text;
       .
       .
       .
       .
       private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                PrivateFontCollection pfc = new PrivateFontCollection();
                pfc.AddFontFile("CCodeDataMatrix.ttf"); //pfc.AddFontFile("CCodeDataMatrix_Trial.ttf");               
                richTextBox1.Font = new Font(pfc.Families[0], 8, FontStyle.Regular);

                Type comObjectType = Type.GetTypeFromProgID("Net.ConnectCode.DataMatrixCOMLibrary");
                dynamic theComObject = Activator.CreateInstance(comObjectType, false);

                //or
                //Guid myGuid = new Guid("B21B180F-8A9C-49D3-9583-0FBFDB62AA93");
                //Type comObjectType = Type.GetTypeFromCLSID(myGuid);
                //dynamic theComObject = Activator.CreateInstance(comObjectType, false);

                string inputData = "12345678";
                int squareRect = 0; //0 - square, 1 - rectangle
                int prefix = 0; //0-None,1-FNC1,2-05 Macro,3-06 Macro,4-Reader Programming                  
		string result1 = theComObject.Encode_DataMatrix(inputData,squareRect,prefix);
                string result = result1;
                richTextBox1.Text = result;
                System.Diagnostics.Debug.WriteLine(result1);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);

            }

The C# function above creates a DataMatrix COM object and then uses it to generate a DataMatrix barcode with the input data "12345678". The result is placed in "richTextBox1" and the final output is displayed with the CCodeDataMatrix.ttf True Type font. When you run the application, click on the "Encode with COM" button, you should see the DataMatrix barcode as shown below.

The "DataMatrixCOMApplication" folder in "C:\Program Files (x86)\ConnectCodeDataMatrix\Resource" contains the full source code of the above application. The "DataMatrixCOMLibrary.dll" can be disributed with your application with an Enterprise or Distribution License.