Chapter 7. Developing Application with DataBar Fontware

DataBar Fontware simplifies the programming by breaking DataBar barcode creation into two distinct processes - encoding and rendering. The rendering is to format the barcode string from the encoding process with one of our font.

The encoding result is a string. If it is a stacked barcode (DataBar Expanded Stacked and DataBar Stacked Omnidirectional), the string contains two or more rows separated by line endings. On Windows, the line feed string consists of two control characters, expressed as \r\n in C language, and vbCrLf in Visual Basic. In ASCII values, they are the control character 10 followed by character 13.

DataBar Fontware includes two DLLs for integrating your applications with our software. One is a Crystal Reports extension UFL, and we have discussed it in Chapter 6, Using DataBar Fonts in Crystal Reports. This chapter addresses DataBarFontEncoder.dll, a Windows native DLL that exposes eight encoding functions. Each encoder function takes a string input and returns a string that becomes a barcode after being formatted with a DataBar font.

DataBarFontEncoder.dll is a regular Windows DLL, and does not require any registration. We recommend that you install the DLL in your own application's directory, which is usually searched first.

7.1. Encoder Functions

The table below lists the functions exported from this encoder DLL.

Table 7.1. Encoder Functions (MoroviaDataBarEncoder.dll)

Function NameBarcode FormatComment
int Code128_Uni(const char* DataToEncode, char* buffer, unsigned int maxSize)Code128Returns the barcode string that becomes Code128 barcode for data DataToEncode after being formated with a Morovia DataBar Font.
int EAN128_Uni(const char* DataToEncode, char* buffer, unsigned int maxSize)GS1-128Returns the barcode string that becomes GS1-128 (UCC/EAN-128) barcode for data DataToEncodeafter being formated with a Morovia DataBar Font.
int DataBar14(const char* DataToEncode, char* buffer, unsigned int maxSize)GS1 DataBar, GS1 DataBar Truncated[a]Returns the barcode string that becomes DataBar-14 barcode for data DataToEncodeafter being formated with a Morovia DataBar Font.
int DataBarLimited(const char* DataToEncode, char* buffer, unsigned int maxSize)GS1 DataBar LimitedReturns the barcode string that becomes DataBar Limited barcode for data DataToEncodeafter being formated with a Morovia DataBar Font.
int DataBarStacked(const char* DataToEncode, char* buffer, unsigned int maxSize)GS1 DataBar StackedReturns the barcode string that becomes DataBar Stacked barcode for data DataToEncodeafter being formated with a Morovia DataBar Font.
int DataBarStackedOmni(const char* DataToEncode, char* buffer, unsigned int maxSize)GS1 DataBar Stacked OmnidirectionalReturns the barcode string that becomes DataBar Stacked Omnidirectional barcode for data DataToEncodeafter being formated with a Morovia DataBar Font.
int DataBarExpanded(const char* DataToEncode, unsigned int SegmentsPerRow, char* buffer, unsigned int maxSize)DataBar Expanded, DataBar Expanded OmnidirectionalReturns the barcode string that becomes DataBar Expanded or DataBar Expanded Stacked barcode for data DataToEncode after being formated with a Morovia DataBar Font. DataBar Expanded is a subset of DataBar Expanded Stacked. Set SymbolsPerRow to 0 or 22 creates DataBar Expanded barcodes.
int getMod10CheckDigit(const char* data)N/AThis function calculates modulo 10 check digit based on the input data, and returns check digit value. This function can be used to calculate check digit for UPC-A, EAN-13 and GTIN numbers.

[a] To create a DataBar Truncated Barcode, use the same function but format the result with font MRV DataBar 13X.


These are the functions directly exported from the DLL. We provide wrapper modules for some programming environments, such as Visual Basic 6 and .Net. The interfaces exposed from these wrapper modules are different from the ones listed in this table.

Note

Some programing environments require the function name declared to exactly match the one in the DLL - that is, the function name is case-sensitive.

7.1.1. Parameters

DataToEncode

A pointer to a C string that specifies the data to be encoded. For EAN128_Uni and DataBarExpanded, the input must be structured GS1-128 data. For more information, see Section 2.4, “Input Format”.

buffer

A pointer to a variable that receives the encoded result (barcode string). The size of the storage area is denoted by maxSize.

maxSize

The maximum size, in bytes, that buffer can store.

SegmentsPerRow

This parameter specifies the number of segments in DataBar Expanded barcodes created. See Section 2.4.4, “DataBar Expanded” for more information.

7.1.2. Return Value

Every encoder function returns the number of bytes (excluding the terminating NUL character) of the encoding results if successful.

If the input is invalid, it returns -1.

If the buffer is too small to hold the result, the function returns 0.

getMod10CheckDigit returns the check digit value (0-9).

7.2. C/C++

To use a DLL in a C/CC++ project, it is recommended that you load the DLL dynamically into the process, obtain address of the function, and invoke the function through that address. After calling the DLL function, your program unloads the DLL. In this way, it is not necessary to link your application with an import library.

The example below illustrates using run-time dynamic linking when calling the encoder DLL.

#include <windows.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
  HINSTANCE hInstance = ::LoadLibrary("MoroviaDataBarFontEncoder.dll");
  int errorCode = 0;
  if (!hInstance) {
    fprintf(stderr, "Can not load MoroviaDataBarFontEncocer.dll.\n");
    errorCode = 100;
    goto LABEL_EXIT;
  }

  typedef int (__stdcall*ENCODE_FUNC_TYPE3)(const char*, char*, unsigned int);
  typedef int (__stdcall*ENCODE_FUNC_TYPE4)(const char*, unsigned int, 
          char*, unsigned int);

  ENCODE_FUNC_TYPE3 pFuncDataBar14 = (ENCODE_FUNC_TYPE3)::GetProcAddress(hInstance, 
                                            "DataBar14");
  if (!pFuncDataBar14) {
    fprintf(stderr, "Invalid DLL.");
    errorCode = 101;
    goto LABEL_EXIT;
  }

  const char* input = "0123456789123";
  char buffer[512];
  int rc = (*pFuncDataBar14)(input, buffer, 512);
  if ( rc > 0 ) {
    fprintf(stdout, "DataBar-14 barcode string for data (%s) is %s.\n",
      input, buffer);
  } else {
    fprintf(stderr, "Invalid data");
    errorCode = 101;
    goto LABEL_EXIT;
  }

  ENCODE_FUNC_TYPE4 pFuncDataBarExp = 
              (ENCODE_FUNC_TYPE4)::GetProcAddress(hInstance, "DataBarExpanded");
  if (!pFuncDataBarExp) {
    fprintf(stderr, "Invalid DLL.");
    errorCode = 101;
    goto LABEL_EXIT;
  }
  input = "(01)1234567890(15)990210";
  rc = (*pFuncDataBarExp)(input, 4, buffer, 512);
  if ( rc > 0 ) {
    fprintf(stdout, "DataBar Expanded barcode string for data (%s) is %s.\n",
      input, buffer);
  } else {
    fprintf(stderr, "Invalid data");
    errorCode = 101;
    goto LABEL_EXIT;
  }

LABEL_EXIT:
  ::FreeLibrary(hInstance);
  return errorCode;
} 

The code above first calls Win32 API LoadLibrary to load the DLL into the process memory. For improving readability, the code defines two function pointer types: ENCODE_FUNC_TYPE3, which takes three arguments, and ENCODE_FUNC_TYPE4, which takes four arguments. The address of the DLL exported function is then retrieved by calling GetProcAddress and casted to an corresponding function pointer instance. Subsequently the encoder function is invoked through this function pointer.

7.3. Visual Basic 6

You can use the same Morovia.DataBarFontDLL.bas module from the one in Office examples. Copy the file into your project directory, and add it to the project. For the function prototypes, see Table 5.1, “List of VBA Functions (Morovia.DataBarFontDLL.bas)”.

When printing GS1 DataBar Stacked Omnidirectional and GS1 DataBar Expanded Stacked barcodes, you need to call Split function to divide barcode string into an array of lines. Then print the lines one by one. Visual Basic does not handle line returns automatically.

 'To print the barcode, we recommend using the built-in Printer object
 'Do not print to a picture box and transfer it to printer. That results
 'in a low quality barcode.
 
 'You can change it to other values to adjust the barcode size.
 Printer.FontSize = 12
 Printer.FontName = "MRV DataBar 34X"
 
 'We use inch as measurement unit here.
 Printer.ScaleMode = vbInches
 
 'We want to print the barcode at point (1.5 inch, 3 inch)
 'Note - printer usually has a margin 0.25" at four directions.
 'You may consider this fact when you set the printer cursor
 Dim StartX, StartY As Double
 StartX = 1.5
 StartY = 3#
 Printer.CurrentX = StartX
 Printer.CurrentY = StartY
 
 'The Priner always go back to the left edge of the page after
 'executing a print statement. Unless we print the barcode to
 'the left ledge of the page,
 'we have to parse the barcode string into lines, and print
 'them one by one.
 'Fortunately, VB provides a nice helper function for this purpose.
 Dim separator As String
 Dim lines() As String
 separator = vbCrLf
 lines() = Split(lblBarcodeString.Caption, separator)
 
 'Now we do the actual print line by line
 Dim i As Integer
 
 For i = 0 To UBound(lines)
   Printer.Print lines(i)
   'Note: the Printer always go back to the beginning of the next line
   'after executing Print. We need to set CurrentX every time after
   'printing a line
   Printer.CurrentX = StartX
 Next
 
 'EndDoc must be called to actual print
 Printer.EndDoc 

7.4. Perl

In Perl, you can use Win32::API module, the Perl Win32 API Import Facility to call a Windows DLL function. The code below demonstrates how you can call a DLL function from a Perl script:

#!/usr/bin/perl -w
use strict;
use Win32::API;

my $DataBarStacked = Win32::API->new('MoroviaDataBarFontEncoder', 
                'DataBarStacked',
                'PPN', 'I');
my $DataToEncode = "1234567890123";
my $Buffer = " " x 512;  # allocate a 512-byte buffer

$DataBarStacked->Call($DataToEncode, $Buffer, 512);
printf("Barcode String is %s\n", $Buffer); 

7.5. .Net Platform (All Versions)

For .Net programmers we prepared a wrapper class called DataBarFontEncoder. Add file Morovia.DataBarFontEncoder.cs into your project, and you can call the methods as if they are local. Note that the prototypes of the methods are different from the one listed in Table 7.1, “Encoder Functions (MoroviaDataBarEncoder.dll)”. They are very similiar to the VBA functions in Table 5.1, “List of VBA Functions (Morovia.DataBarFontDLL.bas)”.

In the following code snippet, DataBarStacked method is invoked to get the barcode string for DataBar Stacked barcode with data 1234567890123 encoded. The second part of the code gets the full string (with check digit) of a GTIN-14 number.

DataBarFontEncoder encoder = new DataBarFontEncoder();
string DataToEncode = @"1234567890123";
string result = encoder.DataBarStacked(DataToEncode);
Console.WriteLine("BarcodeString for {0} is {1}",
  DataToEncode,
  result);

result = encoder.Mod10(DataToEncode);
Console.WriteLine("Full GTIN number (with check digit) is {0}",
  result); 

7.6. Other Programming Environments

Most programming environments support calling native Windows DLL directly. You usually declare the functions exported from the DLL first, then use them as if they are local functions.