C quick start for the Windows Barcode Scanner SDK
Build a lightweight barcode scanner in C for Windows.
Install prerequisites
Install a C toolchain with CMake (for example, Visual Studio Build Tools and CMake) and ensure cmake is available in your terminal.
Project layout
Create a minimal project:
mkdir barcode-quickstart-c
cd barcode-quickstart-c
ni CMakeLists.txt -ItemType File
ni main.c -ItemType File
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ScanbotSDK.h>
// 👉 Replace this with your Scanbot SDK license key
static const char *SCANBOTSDK_LICENSE_KEY = "<SCANBOTSDK-LICENSE-KEY>";
/** Load an image from a file path using Scanbot SDK API. */
static scanbotsdk_error_code_t load_image_from_path(const char *path, scanbotsdk_image_t **out_image) {
scanbotsdk_path_image_load_options_t *options = NULL;
scanbotsdk_error_code_t ec = scanbotsdk_path_image_load_options_create_with_defaults(&options);
if (ec != SCANBOTSDK_OK) return ec;
ec = scanbotsdk_image_create_from_path(path, options, out_image);
scanbotsdk_path_image_load_options_free(options);
return ec;
}
/** Run the barcode scanner and print results. */
static scanbotsdk_error_code_t run_barcode_scanner(scanbotsdk_image_t *image) {
scanbotsdk_error_code_t ec = SCANBOTSDK_OK;
scanbotsdk_barcode_scanner_configuration_t *config = NULL;
scanbotsdk_barcode_scanner_t *scanner = NULL;
scanbotsdk_barcode_scanner_result_t *result = NULL;
// Create scanner config with defaults
ec = scanbotsdk_barcode_scanner_configuration_create_with_defaults(&config);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "config_create: %d\n", ec); goto cleanup; }
// Create scanner instance
ec = scanbotsdk_barcode_scanner_create(config, &scanner);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "scanner_create: %d\n", ec); goto cleanup; }
// Run scanner
ec = scanbotsdk_barcode_scanner_run(scanner, image, &result);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "scanner_run: %d\n", ec); goto cleanup; }
bool success = false;
scanbotsdk_barcode_scanner_result_get_success(result, &success);
if (!success) {
printf("No barcodes found.\n");
goto cleanup;
}
size_t count = 0;
ec = scanbotsdk_barcode_scanner_result_get_barcodes_size(result, &count);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "get_barcodes_size: %d\n", ec); goto cleanup; }
scanbotsdk_barcode_item_t **barcodes = calloc(count, sizeof(*barcodes));
if (!barcodes) { fprintf(stderr, "calloc failed\n"); ec = SCANBOTSDK_ERROR_OUT_OF_MEMORY; goto cleanup; }
ec = scanbotsdk_barcode_scanner_result_get_barcodes(result, barcodes, count);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "get_barcodes: %d\n", ec); goto cleanup; }
for (size_t i = 0; i < count; i++) {
scanbotsdk_u8string_ref_t text_ref = {0};
scanbotsdk_barcode_item_get_text(barcodes[i], &text_ref);
const char *text = scanbotsdk_u8string_ref_assume_cstring(text_ref);
printf(" %zu) %s\n", i + 1, text ? text : "(null)");
}
free(barcodes);
cleanup:
scanbotsdk_barcode_scanner_result_free(result);
scanbotsdk_barcode_scanner_free(scanner);
scanbotsdk_barcode_scanner_configuration_free(config);
return ec;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <image_path>\n", argv[0]);
return 1;
}
const char *file_path = argv[1];
// Init SDK with license
scanbotsdk_init_params_t params = (scanbotsdk_init_params_t){0};
params.license_key = (char *)SCANBOTSDK_LICENSE_KEY;
params.writeable_path = ".";
scanbotsdk_error_code_t ec = scanbotsdk_initialize(¶ms);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "initialize: %d\n", ec); goto cleanup; }
// Load image and scan
scanbotsdk_image_t *image = NULL;
ec = load_image_from_path(file_path, &image);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "load_image_from_path: %d\n", ec); goto cleanup; }
ec = run_barcode_scanner(image);
if (ec != SCANBOTSDK_OK) { fprintf(stderr, "run_barcode_scanner: %d\n", ec); }
scanbotsdk_image_free(image);
cleanup:
return (ec == SCANBOTSDK_OK) ? 0 : 1;
}
CMakeLists
cmake_minimum_required(VERSION 3.16)
project(scanbot_barcode_quickstart C)
set(CMAKE_C_STANDARD 11)
if(NOT SCANBOT_SDK_VERSION)
message(FATAL_ERROR "You must set -DSCANBOT_SDK_VERSION=<version>")
endif()
if(NOT SCANBOTSDK_ARCHITECTURE MATCHES "^(x64|x86)$")
message(FATAL_ERROR "Set -DSCANBOTSDK_ARCHITECTURE=x64 or -DSCANBOTSDK_ARCHITECTURE=x86")
endif()
set(SCANBOTSDK_DIR "${CMAKE_CURRENT_BINARY_DIR}/scanbotsdk" CACHE PATH "Scanbot SDK directory")
find_program(TAR_PATH tar REQUIRED)
if(NOT EXISTS "${SCANBOTSDK_DIR}/include" OR NOT EXISTS "${SCANBOTSDK_DIR}/lib")
set(TARBALL "scanbotsdk-${SCANBOT_SDK_VERSION}-windows-${SCANBOTSDK_ARCHITECTURE}.tar.gz")
set(URL "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv${SCANBOT_SDK_VERSION}/${TARBALL}")
file(DOWNLOAD "${URL}" "${CMAKE_CURRENT_BINARY_DIR}/${TARBALL}" STATUS DOWNLOAD_STATUS SHOW_PROGRESS)
list(GET DOWNLOAD_STATUS 0 DOWNLOAD_CODE)
if(NOT DOWNLOAD_CODE EQUAL 0)
message(FATAL_ERROR "Failed to download ${URL}")
endif()
file(MAKE_DIRECTORY "${SCANBOTSDK_DIR}")
execute_process(
COMMAND ${TAR_PATH} -xzf "${TARBALL}" -C "${SCANBOTSDK_DIR}" --strip-components=1
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
RESULT_VARIABLE TAR_RES
)
if(NOT TAR_RES EQUAL 0)
message(FATAL_ERROR "Failed to extract ${TARBALL}")
endif()
endif()
find_path(ScanbotSDK_INCLUDE_DIRS
NAMES ScanbotSDK.h
HINTS ${SCANBOTSDK_DIR}/include
)
set(ScanbotSDK_DLL "${SCANBOTSDK_DIR}/lib/scanbotsdk.dll")
set(ScanbotSDK_IMPORT_LIBRARY "${SCANBOTSDK_DIR}/lib/scanbotsdk.lib")
if(NOT EXISTS "${ScanbotSDK_DLL}" OR NOT EXISTS "${ScanbotSDK_IMPORT_LIBRARY}" OR NOT ScanbotSDK_INCLUDE_DIRS)
message(FATAL_ERROR "Could not find Scanbot SDK inside ${SCANBOTSDK_DIR}")
endif()
add_library(scanbotsdk SHARED IMPORTED)
set_target_properties(scanbotsdk PROPERTIES
IMPORTED_LOCATION "${ScanbotSDK_DLL}"
IMPORTED_IMPLIB "${ScanbotSDK_IMPORT_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${ScanbotSDK_INCLUDE_DIRS}"
)
add_executable(barcode-quickstart main.c)
target_link_libraries(barcode-quickstart scanbotsdk)
add_custom_command(TARGET barcode-quickstart POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${ScanbotSDK_DLL}"
"$<TARGET_FILE_DIR:barcode-quickstart>"
)
Build
mkdir build
cd build
Replace <SCANBOT_SDK_VERSION> with the version you want to install. Pick the tab that matches your target architecture.
- x64 (windows-x64)
- x86 (windows-x86)
cmake -A x64 -DSCANBOT_SDK_VERSION=<SCANBOT_SDK_VERSION> -DSCANBOTSDK_ARCHITECTURE=x64 ..
cmake --build . --config Release
cmake -A Win32 -DSCANBOT_SDK_VERSION=<SCANBOT_SDK_VERSION> -DSCANBOTSDK_ARCHITECTURE=x86 ..
cmake --build . --config Release
Run
Run the app with the path to an image file containing barcodes:
.\Release\barcode-quickstart.exe C:\path\to\image_with_barcodes.jpg
🚀 That's it! You've built a minimal barcode scanner in C on Windows.
💡 In this quick start guide, we use the SDK's default scanner settings. However, you can freely customize the configuration, supported barcode types, and performance options.
Get in touch
If you need further information or are interested in licensing the Scanbot SDK, please get in touch with our solution experts.
Scanbot SDK is part of the Apryse SDK product family
A mobile scan is just the start. With Apryse SDKs, you can expand mobile workflows into full cross‑platform document processing. Whether you need to edit PDFs, add secure digital signatures, or use a fast, customizable document viewer and editor, Apryse gives you the tools to build powerful features quickly.
Learn more
