dLSoft  barcode & labelling software     

Generating barcodes in C/C++

The best method to include barcodes in C/C++ projects is to use barcode images created as metafile images or enhanced metafile images. These provide the highest resolution image, and so the most accurate barcode. The dBarcode DLLs are dynamic link libraries that create high resolution metafile images (among others) for most major barcode types. A wide range of function calls is provided to allow the develop to retrieve a barcode image. Similar facilities are available in the dBarcode Developer kit, based on Active DLLs which also provide Active-X functionality and include the ability to create both image and font-based barcodes.

Using the call BarCodem() and BarCodemx() (for enhanced metafiles) will allow handles to these metafile images to be obtained directly, while BarDraw() and BarDrawEnh() all the images to be draw directly to a device at a specified location.

Some developers may prefer (or be forced) to obtain the images from the clipboard (using BarCodec()) or from a disk file (BarCodef()). Others may be unable to use metafile images and be forced to use either monochrome bitmaps or device independent bitmaps, at the current default resolutions using calls to SetDpi, BarCodeb or BarCoded respectively, or at a specified target resolution in dpi using BarCodeb3 or BarCoded3 respectively. Barcode images may be save to disc in BMP, GIF, PCX, PNG or WMF formats.

The dBarcode DLL function calls are divided broadly into two classes. The Group 1 calls to the DLL require a Barcode structure to be filled prior to the call. The structure holds information about the barcode type, size, colour and data, etc.

Group 2 calls do not require the Barcode structure (unless a structure is being retrieved from the built-in dialog box functions), but do require the dialog box to be used to set barcode properties. The built in dialog box functions provide a simple way for a developer to allow the end user access to setting all barcode properties.

Barcode properties dialog box      Click to enlarge

 

Popular Library calls for creating barcodes in C/C++

BarDraw

int WINAPI BarDraw(HDC hDC, LPRECT lpRectIn, LPBAR bc, LPLOGFONT lpLf);

Draws the vector (metafile) barcode specified by the barcode structure bc and the human readable font lpLf in the device context hDC in the rectangle represented by lpRectIn (specified in HIMETRIC units).
Note that the top left of the lpRectIn is where the barcode is drawn. Its size is determined by the data provided in bc, and the bottom right of lpRectIn is adjusted accordingly.
The return value is 0 if successful, or an error code in the event of failure.

BarDrawEnh

int WINAPI BarDrawEnh(HDC hDC, LPRECT lpRectIn, LPBAR bc, LPLOGFONT lpLf);

Draws the vector (Enhanced metafile) barcode specified by the barcode structure bc and the human readable font lpLf in the device context hDC in the rectangle represented by lpRectIn (specified in HIMETRIC units).
Note that the top left of the lpRectIn is where the barcode is drawn. Its size is determined by the data provided in bc, and the bottom right of lpRectIn is adjusted accordingly.
The return value is 0 if successful, or an error code in the event of failure.

BarDrawBitmap

int WINAPI BarDrawBitmap(HDC hDC, LPRECT lpRectIn, LPBAR bc, LPLOGFONT lpLf, BOOL bmono);

Draws a bitmap barcode image specified by the barcode structure bc and the human readable font lpLf in the device context hDC in the rectangle represented by lpRectIn (specified in HIMETRIC units).
If bmono is false the bitmap is drawn as a full colour DIB, while if bmono is true the bitmap is a monochrome.
Note that the top left of the lpRectIn is where the barcode is drawn. Its size is determined by the data provided in bc, and the bottom right of lpRectIn is adjusted accordingly.
The return value is 0 if successful, or an error code in the event of failure.

BarSaveImage

int WINAPI BarSaveImage(LPBAR bc, LPLOGFONT lpLf, LPTSTR szFileName, int ires);

Saves the barcode image specified by the barcode structure bc and the human readable font lpLf into the file specified by the string szFileName and with a resolution specified by the value of ires (in pixels per inch).
The type of image saved is determined by the extension given to the filename, which may be:
BMP – 1 bpp Bitmap (monochrome)
BMP - 8 bpp Bitmap (colour) [created by using bm8 as the extension passed to the library]
DIB – Device Independent (colour) bitmap
EPS – Encapsulated PostScript
GIF – GIF
PCX – Paintbrush file
PNG – Portable Network Graphic
WMF – Windows Metafile
(The image types may be interrogated using the Barfinf function – see below)
The return value is 0 if successful, or an error code in the event of failure.

Group 1 calls

Group 1 Library calls generate barcode images from a Barcode structure filled before the call.

Barcodem - returns the handle to a metafile image
Barcodemx - returns the handle to an enhanced metafile image.
Barcodeb - returns the handle to a bitmap image
Barcoded - returns the handle to a device independent bitmap image.
Barcodec - produces barcode image on the clipboard for Visual Basic
Barcodef - saves the image of the barcode to disk
Barfinf - specifies the graphics formats in which barcode images may be saved to disk.
BarcodeW - creates a barcode image on the Windows clipboard using a reduced (and limited) set of data.
Barcodeh - display the barcode image within a device context.
BarCodep - generates only the pattern string (no graphic image)
BarCodeb3 - returns a handle to a bitmap image for a device of specified resolution
BarCoded3 - returns a handle to a device independent bitmap for a device of specified resolution

Group 2 Library calls

Group 2 calls enable the programmer to generate barcodes using a dialog box to provide the barcode properties rather than the Barcode structure.

BarAsk - displays a pop-up dialog requesting barcode information. When OK is pressed all barcode properties except the data are stored in memory and saved as defaults for future use.
BarAskEx - displays a pop-up dialog requesting barcode information. When OK is pressed all barcode properties except the data are stored in memory and used as defaults until the calling program is closed.
BarCdm - generates a metafile image from a data string using the current default barcode properties.
BarCdmx - generates a enhanced metafile image from a data string using the current default barcode properties.
BarCdd - generates a bitmap image from a data string using the current default barcode properties.
BarCdh2 - draws a high-resolution image generated from the string parameter using the current default barcode properties into the device context with the handle hDC.
BarCdc - generates a enhanced metafile image from a data string using the current default barcode properties and places the image on the clipboard.
BarCdcb - generates a bitmap (BMP) image from a data string using the current default barcode properties and places the image on the clipboard.
BarCdcd - generates a Device Independent Bitmap (DIB) image from a data string using the current default barcode properties and places the image on the clipboard.
BarCdp - generates a barcode pattern string from a data string using the current default barcode properties.
BarCds – generates a barcode image from a data string using the current default barcode properties and saves the image to a named disk file.
BarCdrd - copies the current default barcode properties into a Barcode structure.
BarCdin – sets the default barcode properties to the values passed as a Barcode structure and saves the defaults for future use.
BarCdinEx – sets the default barcode properties to the values passed as a Barcode structure; these values will be lost when the calling program is closed.