Detect and Extract Barcodes - Windows C DLL

This tutorial shows how to read barcode information from a loaded image using the LEADTOOLS SDK in a Windows C/C++ API application.

Summary This tutorial covers how to use the BARCODEDATA structure in a Windows C DLL Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (19 KB)
Platform Windows C DLL Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Detect and Extract Barcodes - Windows C DLL tutorial, get familiar with the basic steps of creating a project and loading/displaying an image by reviewing the Add References and Set a License and Load, Display, and Save Images tutorials.

Create the Project and Add LEADTOOLS References

Start with a copy of the 64-bit Windows API project created in the Load, Display, and Save Images tutorial. If the project is not available, create it by following the steps in that tutorial.

To read barcodes using LEADTOOLS, add the required header and DLL files. Open the pre-compiled header file (either pch.h or stdafx.h, depending on the version of Visual Studio used) and add the following lines:

#include "C:\LEADTOOLS22\Include\Ltbar.h" 
#pragma comment (lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltbar_x.lib") // barcode support 


For a complete list of Dlls that are required for your application, refer to Files to be Included with your Application - C API.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit functionality is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Barcode Reader Code

With the project created, the references added, the license set, and the load image code added, coding can begin.

The steps below are for Visual Studio 2019; they could be different for other versions of Visual Studio.

Go to the Solution Explorer and double-click the resources file (.rc).

Add a new &Barcode drop down menu next to the File menu. In the newly-added &Barcode menu, add a &Read Barcode(s) menu item. The new item's ID should be ID_BARCODE_READBARCODE.

Go to the WndProc function, and under the switch (wmId) statement that is below the WM_COMMAND case, add a new case:

switch (wmId) 
   if (LEADBmp.Flags.Allocated) 
      MessageBox(hWnd, TEXT("Cannot read barcodes. No image loaded"), TEXT("Barcode Demo"), MB_ICONERROR); 
// keep rest of the code as is 

Add a new ReadBarcodes function, which can be placed above the WndProc function:

void ReadBarcodes(HWND hwnd) 
   if (L_BarCodeInit(BARCODES_1D) != SUCCESS) 
   pBARCODEDATA pBarCodes = NULL; 
   BARCODE1D Bar1d = { 0 }; 
   Bar1d.uStructSize = sizeof BARCODE1D; 
   Bar1d.bErrorCheck = TRUE; 
   Bar1d.nGranularity = 9; 
   Bar1d.nDirection = BARCODE_DIR_HORIZONTAL; 
   BARCODECOLOR BarColor = { 0 }; 
   BarColor.uStructSize = sizeof BARCODECOLOR; 
   BarColor.dwColorBar = RGB(0, 0, 0); 
   BarColor.dwColorSpace = RGB(255, 255, 255); 
   // Search for all 1D barcodes in the enite bitmap 
   if (SUCCESS == nRet) 
      L_INT nCount = pBarCodes->nTotalCount; 
      L_CHAR * pszMessage = new L_CHAR[256 * nCount]; 
      ZeroMemory(pszMessage, 256 * nCount * sizeof(*pszMessage)); 
      for (L_INT i = 0; i < nCount; i++) 
         L_CHAR szBuffer[256] = ""; 
         wsprintfA(szBuffer, "Barcode # %d, Data: '%s', Left %d, Top %d, Right %d, Bottom %d\n---\n", i, 
         lstrcatA(pszMessage, szBuffer); 
      MessageBoxA(hwnd, pszMessage, "Barcode(s) Found", MB_ICONINFORMATION); 
      delete[] pszMessage; 
      MessageBox(hwnd, TEXT("Could not read 1D Barcodes"), TEXT("Barcode Demo"), MB_ICONERROR); 
   if (pBarCodes) 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps are followed correctly, the application should run and enable the user to select File -> Open to load the image containing the barcode(s) data. Select Barcode -> Read Barcode(s) to have the application run barcode recognition, and to output all the relevant barcode information from the loaded image.


If there's no barcode image available for testing, use the image at this file path: C:\LEADTOOLS22\Resources\Images\barcode1.tif

Barcode symbology recognition result dialog box


This tutorial covered how to read barcode information using the LEADTOOLS SDK in a Windows API application. It also covered how to use the BARCODEDATA structure, with the L_BarCodeRead function.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.