ByteScout BarCode Generator SDK – C++ – Get Barcode To Memory Buffer

  • Home
  • /
  • Articles
  • /
  • ByteScout BarCode Generator SDK – C++ – Get Barcode To Memory Buffer

ByteScout BarCode Generator SDK – C++ – Get Barcode To Memory Buffer

BarcodeGenerationExample.cpp

#include "stdafx.h"
#include <atlsafe.h> // For ATL::CComSafeArray

#import "Bytescout.BarCode.tlb" raw_interfaces_only

using namespace Bytescout_BarCode;


int _tmain(int argc, _TCHAR* argv[])
{
	// Initialize COM.
	HRESULT hr = CoInitialize(NULL);

	// Create the interface pointer.
	IBarcodePtr pIBarcode(__uuidof(Barcode));

	// set the registration name and key
	BSTR regname = ::SysAllocString(L"DEMO");
	pIBarcode->put_RegistrationName(regname);
	::SysFreeString(regname);

	BSTR regkey = ::SysAllocString(L"DEMO");
	pIBarcode->put_RegistrationKey(regkey);
	::SysFreeString(regkey);

	// Set barcode type (symbology)
	pIBarcode->put_Symbology(SymbologyType_Code128);

	// Set barcode value
	BSTR value = ::SysAllocString(L"Abc123");
	pIBarcode->put_Value(value);
	::SysFreeString(value);

	// Get barcode image bytes.
	SAFEARRAY* pSafeArray;
	pIBarcode->GetImageBytesPNG(&pSafeArray);

	// Convert SAFEARRAY to byte array
	CComSafeArray<BYTE> safeArray;
	safeArray.Attach(pSafeArray);
	UINT count = safeArray.GetCount();
	BYTE* bytes = new BYTE[count];
	for (ULONG i = 0; i < count; i++)
		bytes&#91;i&#93; = safeArray.GetAt(i);

	// Check result by saving image bytes to file
	FILE* pFile;
	fopen_s(&pFile, "result.png", "wb");
	fwrite(bytes, 1, count, pFile);
	fclose(pFile);

	delete&#91;&#93; bytes;
	pIBarcode->Release();

	// Uninitialize COM.
	CoUninitialize();

	return 0;
}


stdafx.cpp

// stdafx.cpp : source file that includes just the standard includes
// BarcodeGenerationExample.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file


  Click here to get your Free Trial version of the SDK

Tutorials:

prev
next