C/C++ Sample Code
The following example demonstrates the principal calls with the Unicode API. It checks each barcode-processing step and releases all allocated resources. Complete application samples are installed with TBarCode SDK.
#define _UNICODE
#include <windows.h>
#include <wchar.h>
#include "tbarcode.h"
// Windows DLL builds do not require BCInitLibrary().
// Static-library builds must call BCInitLibrary() before using the API.
ERRCODE GenerateBarcode(HDC hPrinterDC, LONG xPos, LONG yPos)
{
ERRCODE eCode = ErrOk;
t_BarCode* pBC = NULL;
LPBYTE imageBuffer = NULL;
const wchar_t* demo = L"12345678";
// Apply the product license.
eCode = BCLicenseMe(L"LicenseeName", eLicKindDeveloper, 1,
L"LicenseKey", eLicProd2D);
if (eCode != ErrOk)
goto cleanup;
// Allocate the barcode structure.
eCode = BCAlloc(&pBC);
if (eCode != ErrOk)
goto cleanup;
// Select the symbology and configure the human-readable text.
eCode = BCSetBCType(pBC, eBC_Code128);
if (eCode != ErrOk)
goto cleanup;
eCode = BCSetFontHeight(pBC, 14);
if (eCode != ErrOk)
goto cleanup;
// Set the barcode data after all properties that affect its interpretation.
eCode = BCSetText(pBC, demo, (LONG)wcslen(demo));
if (eCode != ErrOk)
goto cleanup;
// Optionally validate the data before creating the barcode.
eCode = BCCheck(pBC);
if (eCode != ErrOk)
goto cleanup;
// Create the internal barcode representation.
eCode = BCCreate(pBC);
if (eCode != ErrOk)
goto cleanup;
// Draw to a Windows device context. Hi-Metric units are 0.01 mm.
{
RECT rect = {0, 3000, 5000, 0}; // 50 x 30 mm
INT savedDC = SaveDC(hPrinterDC);
SetMapMode(hPrinterDC, MM_HIMETRIC);
OffsetRect(&rect, xPos, yPos);
eCode = BCDraw(pBC, hPrinterDC, &rect);
RestoreDC(hPrinterDC, savedDC);
}
if (eCode != ErrOk)
goto cleanup;
// Save to an EPS file. Vector-output dimensions use 0.001 mm.
eCode = BCSaveImage(pBC, L"barcode.eps", eIMEps,
50000, 30000, 0, 0);
if (eCode != ErrOk)
goto cleanup;
// Save a JPG to a memory buffer. Bitmap dimensions use pixels.
eCode = BCSaveImageToBuffer(pBC, &imageBuffer, eIMJpg,
(LONG)(2 * BCGetCountModules(pBC)),
100, 96, 96);
cleanup:
if (imageBuffer != NULL)
GlobalFree((HGLOBAL)imageBuffer);
if (pBC != NULL)
BCFree(pBC);
return eCode;
}
// Windows DLL builds do not require BCDeInitLibrary().
// Static-library builds must call BCDeInitLibrary() before the application exits.