Jump to content

Recommended Posts

Posted (edited)

Using BarCodeGenerator.dll in AutoIt: A Comprehensive Guide

This guide introduces you to the BarCodeGenerator.dll—a dynamic link library designed for generating various types of barcodes and QR codes. You will learn about the structure of its exported functions, the necessary parameters, and how you can integrate and call these functions from an AutoIt script. The article also includes example code to illustrate both the in-memory image handling (using GDIPlus) as well as file-based output.

 

1. Exported Functions Overview

The DLL exports two primary functions:

a) CreateBarcode

  • Purpose: Generates a barcode or QR code based on the provided text and other configuration parameters. Depending on the parameters, the function either returns an HBITMAP (when no file path is provided and when the output format is an image such as PNG, BMP, JPG, or TGA) or returns a text result with configuration details (for console output, SVG, or when output is generated to a file).

  • Exported Signature (C/C++ Prototype):

     
    DLL_API void* __stdcall CreateBarcode(
        const wchar_t* textContent,
        const wchar_t* outputType,
        const wchar_t* eccStr,
        const wchar_t* qrColorHex,
        const wchar_t* bgColorHex,
        const wchar_t* outputFormat,
        const wchar_t* filePath,
        int sizeParam,
        int scale,
        int borderModules
    );

     

     
  • Parameters:

    • textContent: The string content that you want to encode. For example, "Hello World".

    • outputType: A string that defines the type of barcode/QR code. Accepted values include "qr", "code128", "code39", and "ean128".

    • eccStr: Specifies the error correction level (primarily used for QR codes). Example values are "low", "medium", "quartile", and "high".

    • qrColorHex and bgColorHex: These parameters represent the barcode (foreground) and background colors in hexadecimal notation. For example, passing "000000" for black and an empty string for the default background.

    • outputFormat: Sets the desired output format. Valid options include "bmp", "png", "jpg", "jpeg", "tga", "svg", and "console".

    • filePath: When provided with a valid path, the DLL saves the generated file to disk. If this parameter is an empty string, the function instead returns an HBITMAP (if the output is an image) that can be handled via GDIPlus in AutoIt.

    • sizeParam: Indicates the desired overall size for the generated image.

    • scale: The scale factor. If set to 0 (or less), the scale is determined automatically based on size and border parameters.

    • borderModules: Determines the number of border modules (padding) around the generated symbol.

  • Return Value: The function returns a pointer to a structure—BarcodeResult. This structure contains:

    • A fixed-length wide-character array (wchar_t info[4096]) that includes the configuration details along with the generation result.

    • A pointer (hBitmap) that will hold a valid HBITMAP if the output is generated in memory (i.e., when no file path is provided).

b) GetBarcodeInfo

  • Purpose: This function returns static information about the DLL, including version and author details.

  • Exported Signature (C/C++ Prototype):

     
    DLL_API wchar_t* __stdcall GetBarcodeInfo();

     

     
  • Return Value: A pointer to a wide-character string containing DLL information (for example, version, author, etc.).

 

2. Structure Definition in the Header File

For smooth integration with AutoIt, the DLL uses a predefined structure for the return value of CreateBarcode. Below is an example of the header file content (named BarCodeGenerator.h😞

#ifndef BARCODEGENERATOR_H
#define BARCODEGENERATOR_H

#ifdef _WIN32
    #ifdef DLL_EXPORTS
        #define DLL_API __declspec(dllexport)
    #else
        #define DLL_API __declspec(dllimport)
    #endif
#else
    #define DLL_API
#endif

// Define the size of the buffer to store info details.
#define INFO_BUFFER_SIZE 4096

#ifdef __cplusplus
extern "C" {
#endif

// Structure returned from CreateBarcode
typedef struct _BarcodeResult {
    wchar_t info[INFO_BUFFER_SIZE];
#ifdef _WIN32
    void* hBitmap;  // HBITMAP handle on Windows platforms
#else
    void* hBitmap;
#endif
} BarcodeResult;

// Exported function to create a barcode/QR code.
DLL_API void* __stdcall CreateBarcode(
    const wchar_t* textContent,
    const wchar_t* outputType,
    const wchar_t* eccStr,
    const wchar_t* qrColorHex,
    const wchar_t* bgColorHex,
    const wchar_t* outputFormat,
    const wchar_t* filePath,
    int sizeParam,
    int scale,
    int borderModules
);

// Exported function to get general renderer information.
DLL_API wchar_t* __stdcall GetBarcodeInfo();

#ifdef __cplusplus
}
#endif

#endif // BARCODEGENERATOR_H

 

 

This header file clearly defines the exported functions and makes it easier to write AutoIt scripts to call these functions via the DllCall mechanism.

 

3. Using the DLL in AutoIt

When integrating this DLL in AutoIt, you must create a structure that matches BarcodeResult.

Typically, you'll use DllStructCreate() with a format string such as "wchar[4096];ptr hBitmap".

Example AutoIt Workflow:

  1. Call CreateBarcode to generate a QR code in memory.

    • Pass an empty string as the filePath parameter so that the DLL returns an HBITMAP for in-memory processing.

    • Use GDIPlus (via the GDIPlus UDF) to convert the HBITMAP to a PNG image and save it to disk.

  2. Call CreateBarcode to output directly to a file.

    • Provide a valid filePath (e.g., "C:\Temp\qr_output.png") so that the DLL writes the file to the disk and also returns a text report with the configuration and operation details.

  3. Call GetBarcodeInfo to retrieve additional information about the DLL.

Below is a sample AutoIt code snippet that demonstrates how to call these functions:

#RequireAdmin

;~ #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
;~ #AutoIt3Wrapper_UseX64=y
;~ #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;~ #pragma compile(x64, true)

; For test
FileDelete(@ScriptDir & "\output_qr.png")
FileDelete(@ScriptDir & "\output_code128.png")

; Path to the DLL (adjust according to the actual location of the DLL on your system)
Global Const $g_sDllPath = @AutoItX64 ? @ScriptDir & "\BarCodeGenerator_x64.dll" : @ScriptDir & "\BarCodeGenerator_x86.dll"
; Path to the DLL (update to the correct path)
#include <GDIPlus.au3>

; Define the BarcodeResult structure as follows:
; - wchar[4096]: a text string containing configuration and result information (fixed at 4096 characters)
; - ptr hBitmap: the hBitmap pointer returned when using in-memory image output
Local $tBarcodeResultDef = "wchar[4096];ptr hBitmap"

ConsoleWrite("; EG 1 -----------------------------" & @CRLF)
; -----------------------------
; PART 1: Call CreateBarcode to generate a QR code in-memory
; (Since FilePath is "" the function returns an HBITMAP, and the PNG image will be saved using _GDIPlus)
Local $aRet = DllCall($g_sDllPath, "ptr", "CreateBarcode", _
        "wstr", "Hello World", _       ; textContent
        "wstr", "qr", _                ; outputType
        "wstr", "medium", _            ; eccStr
        "wstr", "000000", _            ; qrColorHex
        "wstr", "", _                  ; bgColorHex (empty means default)
        "wstr", "png", _               ; outputFormat
        "wstr", "", _                  ; filePath = empty => create image in memory
        "int", 128, _                  ; sizeParam
        "int", 0, _                    ; scale
        "int", 1)                      ; borderModules

If Not IsArray($aRet) Then
    ConsoleWrite("CreateBarcode call failed." & @CRLF)
Else
    ; $aRet[0] is the pointer to the BarcodeResult structure.
    Local $pResult = $aRet[0]
    ; Map the returned pointer to the BarcodeResult structure.
    Local $sBarcodeResult = DllStructCreate($tBarcodeResultDef, $pResult)
    ; Retrieve the configuration and status message string.
    Local $sInfo = DllStructGetData($sBarcodeResult, 1)
    ConsoleWrite("In-memory QR generation result info:" & @CRLF & $sInfo & @CRLF)

    ; Get the HBITMAP
    Local $hBmp = DllStructGetData($sBarcodeResult, 2)
    If @error Then
        ConsoleWrite("! Error in DllStructGetData, Code: " & @error & @CRLF)
    EndIf

    ; Use _GDIPlus to save this HBITMAP to a PNG file.
    _GDIPlus_Startup()

    ; Convert HBITMAP to a GDI+ Bitmap object
    Local $hBmpGp = _GDIPlus_BitmapCreateFromHBITMAP($hBmp)
    If $hBmpGp = 0 Then
        ConsoleWrite("Failed to convert HBITMAP to GDI+ Bitmap." & @CRLF)
    Else
        ; Save the image to a file
        If _GDIPlus_ImageSaveToFile($hBmpGp, @ScriptDir & "\output_qr.png") Then
            ConsoleWrite("Saved in-memory QR code to file " & @ScriptDir & "\output_qr.png" & @CRLF)
            If FileExists(@ScriptDir & "\output_qr.png") Then ShellExecute(@ScriptDir & "\output_qr.png"); show qr
        Else
            ConsoleWrite("! Failed to save the image to file." & @CRLF)
        EndIf
        _GDIPlus_ImageDispose($hBmpGp)
    EndIf
    _GDIPlus_Shutdown()
EndIf

ConsoleWrite("; EG 2 -----------------------------" & @CRLF)
; -----------------------------
; PART 2: Call CreateBarcode to output the result directly to a file.
; Here, since filePath is provided, the DLL will write the file to disk.
Local $aRet2 = DllCall($g_sDllPath, "ptr", "CreateBarcode", _
        "wstr", "Hello World", _
        "wstr", "code128", _
        "wstr", "medium", _
        "wstr", "000000", _
        "wstr", "", _
        "wstr", "png", _
        "wstr", @ScriptDir & "\output_code128.png", _ ; Direct output to file
        "int", 128, _
        "int", 0, _
        "int", 4)

If Not IsArray($aRet2) Then
    ConsoleWrite("! File output CreateBarcode call failed." & @CRLF)
Else
    Local $pResult2 = $aRet2[0]
    Local $sBarcodeResult2 = DllStructCreate($tBarcodeResultDef, $pResult2)
    Local $sInfo2 = DllStructGetData($sBarcodeResult2, 1)
    If FileExists(@ScriptDir & "\output_code128.png") Then ShellExecute(@ScriptDir & "\output_code128.png") ; show bar code 128
    ConsoleWrite("File output QR generation result info:" & @CRLF & $sInfo2 & @CRLF)
EndIf

ConsoleWrite("; Test 3 -----------------------------" & @CRLF)
; -----------------------------
; PART 3: Call GetBarcodeInfo to retrieve additional information about the author, version, etc.
Local $aRet3 = DllCall($g_sDllPath, "wstr", "GetBarcodeInfo")
If Not IsArray($aRet3) Then
    ConsoleWrite("! GetBarcodeInfo call failed." & @CRLF)
Else
    MsgBox(64 + 262144, "About ", $aRet3[0] & @CRLF)
    ;MsgBox(64 + 262144, "GetBarcodeInfo: ", CorrectFromCP1252ToUTF8($aRet3[0]) & @CRLF)
EndIf


; Function to convert strings misinterpreted as CP1252 to proper UTF-8
Func CorrectFromCP1252ToUTF8($sMisinterpreted)
    ; Convert the string (encoded as CP1252) to binary.
    Local $aBinary = StringToBinary($sMisinterpreted, 0)
    ; Create an ADODB.Stream object to perform encoding conversion.
    Local $oStream = ObjCreate("ADODB.Stream")
    If Not IsObj($oStream) Then
        MsgBox(16, "Error", "Unable to create ADODB.Stream object.")
        Return $sMisinterpreted
    EndIf
    ; Write the binary data to the stream.
    $oStream.Type = 1    ; 1: Binary
    $oStream.Open
    $oStream.Write($aBinary)
    ; Reset stream position and switch to text mode.
    $oStream.Position = 0
    $oStream.Type = 2    ; 2: Text
    $oStream.Charset = "utf-8"   ; Read data as UTF-8.
    Local $sCorrect = $oStream.ReadText()
    $oStream.Close()
    Return $sCorrect
EndFunc   ;==>CorrectFromCP1252ToUTF8

 

 

Explanation of the AutoIt Code:

  • Structure Initialization: The structure for the result (BarcodeResult) is defined in AutoIt using DllStructCreate with "wchar[4096];ptr hBitmap". This matches the DLL’s definition.

  • Function Calls:

    • For the first call to CreateBarcode, an empty filePath tells the DLL to generate the image in memory. The returned HBITMAP is then processed via _GDIPlus for conversion and saving.

    • For the second call, a valid file path is provided so the DLL writes the image file directly to disk while also returning a text-based log of the configuration and status.

    • Finally, the static information is retrieved using GetBarcodeInfo.

  • Output Display: Instead of using message boxes, all feedback and information are displayed using ConsoleWrite().

 

4. Conclusion

In this guide, we have covered the following:

  • The structure and purpose of the exported functions from BarCodeGenerator.dll.

  • Detailed descriptions of all necessary parameters—including text content, barcode/QR type, error correction level, colors, output format, and file handling.

  • How to set up and call these functions from an AutoIt script using the DLL call mechanism.

  • An example AutoIt code that demonstrates both in-memory image retrieval (with _GDIPlus) and direct file output, and how to retrieve additional DLL information.

By following this guide, you should be able to effectively integrate the barcode generation capabilities of the DLL into your AutoIt projects and further customize the functionality to suit your requirements.

Happy coding!

_________________________________________

For Vietnamese:

Spoiler

Hướng Dẫn Sử Dụng BarCodeGenerator.dll với AutoIt

Mục tiêu: Bài viết này nhằm giúp bạn hiểu cấu trúc của các hàm export trong DLL BarCodeGenerator.dll và các tham số bắt buộc để sử dụng từ AutoIt. Bạn sẽ được tìm hiểu cách gọi hàm CreateBarcode để tạo mã QR hoặc mã vạch, cũng như cách lấy dữ liệu trả về (bao gồm thông tin cấu hình và HBITMAP cho trường hợp tạo mã trực tiếp trong bộ nhớ). Cuối cùng, chúng ta cũng sẽ xem cách gọi hàm GetBarcodeInfo để lấy thông tin chung của DLL.

1. Cấu Trúc Các Hàm Export

Hàm CreateBarcode

  • Mục đích: Hàm CreateBarcode tạo ra mã vạch hoặc mã QR dựa trên nội dung được cung cấp cùng với các tham số cấu hình như kích thước, tỷ lệ (scale), màu sắc, định dạng xuất ra và đường dẫn file (nếu có).

  • Chữ ký hàm (Prototype):

    DLL_API void* __stdcall CreateBarcode(
        const wchar_t* textContent,
        const wchar_t* outputType,
        const wchar_t* eccStr,
        const wchar_t* qrColorHex,
        const wchar_t* bgColorHex,
        const wchar_t* outputFormat,
        const wchar_t* filePath,
        int sizeParam,
        int scale,
        int borderModules
    );

    Các Tham Số Chi Tiết:

    • textContent: Nội dung cần mã hóa, ví dụ "Hello World". Đây là tham số bắt buộc.

    • outputType: Loại mã cần tạo. Các giá trị hợp lệ bao gồm:

      • "qr": Mã QR

      • "code128": Mã Code 128

      • "code39": Mã Code 39

      • "ean128": Mã EAN 128

    • eccStr: Mức độ sửa lỗi dùng cho mã QR. Các lựa chọn có thể là "low", "medium", "quartile", hoặc "high".

    • qrColorHexbgColorHex: Mã màu dạng hex cho phần mã (foreground) và nền (background). Ví dụ "000000" cho màu đen; nếu để rỗng, hệ thống sẽ chọn màu mặc định.

    • outputFormat: Định dạng đầu ra. Các lựa chọn bao gồm:

      • Đầu ra ảnh: "bmp", "png", "jpg", "jpeg", "tga".

      • Đầu ra dạng các file văn bản: "svg", "console".

    • filePath: Đường dẫn xuất file. Nếu tham số này không rỗng, DLL sẽ ghi file vào ổ đĩa (với việc kiểm tra và tạo cây thư mục nếu chưa tồn tại). Nếu là chuỗi rỗng và đầu ra thuộc ảnh (ví dụ: PNG, BMP, ...), DLL sẽ trả về ảnh dưới dạng HBITMAP để xử lý trực tiếp (ví dụ dùng _GDIPlus trong AutoIt).

    • sizeParam: Kích thước tổng thể (pixel) của ảnh tạo ra.

    • scale: Tỷ lệ phóng đại. Nếu giá trị này ≤ 0, DLL tự động tính toán dựa trên sizeParam.

    • borderModules: Số module viền (padding) xung quanh biểu tượng mã vạch/QR.

  • Giá Trị Trả Về: Hàm trả về một con trỏ đến cấu trúc BarcodeResult gồm các thành phần sau:

    • Một mảng ký tự kiểu wchar_t info[4096] chứa thông tin chi tiết về cấu hình và kết quả của quá trình tạo mã.

    • Một con trỏ hBitmap chứa handle HBITMAP (chỉ hợp lệ cho trường hợp tạo ảnh trực tiếp trong bộ nhớ).

Hàm GetBarcodeInfo

  • Mục Đích: Lấy thông tin tĩnh về DLL, bao gồm version, tác giả 

  • Chữ ký hàm (Prototype):

    DLL_API wchar_t* __stdcall GetBarcodeInfo();
  • Giá Trị Trả Về: Trả về một con trỏ đến chuỗi ký tự chứa thông tin của DLL.

2. Cấu Trúc File Tiêu Đề (Header File) BarCodeGenerator.h

Để dễ dàng tích hợp với AutoIt, file header định nghĩa cấu trúc trả về và các hàm export. Dưới đây là nội dung file tiêu đề đã được chỉnh sửa:

#ifndef BARCODEGENERATOR_H
#define BARCODEGENERATOR_H

#ifdef _WIN32
    #ifdef DLL_EXPORTS
        #define DLL_API __declspec(dllexport)
    #else
        #define DLL_API __declspec(dllimport)
    #endif
#else
    #define DLL_API
#endif

// Định nghĩa kích thước của bộ đệm chứa thông tin.
#define INFO_BUFFER_SIZE 4096

#ifdef __cplusplus
extern "C" {
#endif

// Cấu trúc kết quả trả về từ hàm CreateBarcode.
// Thuộc tính 'info' chứa chuỗi cấu hình & kết quả. 'hBitmap' chứa handle ảnh khi cần.
typedef struct _BarcodeResult {
    wchar_t info[INFO_BUFFER_SIZE];
#ifdef _WIN32
    void* hBitmap;  // Handle của HBITMAP trên Windows
#else
    void* hBitmap;
#endif
} BarcodeResult;

// Hàm tạo mã vạch / QR.
DLL_API void* __stdcall CreateBarcode(
    const wchar_t* textContent,
    const wchar_t* outputType,
    const wchar_t* eccStr,
    const wchar_t* qrColorHex,
    const wchar_t* bgColorHex,
    const wchar_t* outputFormat,
    const wchar_t* filePath,
    int sizeParam,
    int scale,
    int borderModules
);

// Hàm lấy thông tin chung của DLL (version, tác giả,...)
DLL_API wchar_t* __stdcall GetBarcodeInfo();

#ifdef __cplusplus
}
#endif

#endif // BARCODEGENERATOR_H

 

3. Ví Dụ Sử Dụng DLL trong AutoIt

Để sử dụng DLL này trong AutoIt, bạn cần định nghĩa cấu trúc BarcodeResult bằng cách sử dụng DllStructCreate() với định dạng "wchar[4096];ptr hBitmap". Sau đó, bạn có thể gọi hàm CreateBarcodeGetBarcodeInfo theo trình tự sau.

a) Tạo mã QR “in-memory” và xử lý ảnh bằng _GDIPlus

Khi truyền chuỗi rỗng vào tham số filePath, DLL sẽ trả về HBITMAP chứa ảnh được tạo. Ta sẽ sử dụng _GDIPlus (qua UDF _GDIPlus.au3) để chuyển HBITMAP thành file PNG.

b) Tạo mã QR thông qua xuất file trực tiếp

Khi truyền tên/đường dẫn file (ví dụ: "C:\Temp\qr_output.png"), DLL sẽ tự động kiểm tra và tạo cây thư mục nếu cần, ghi file ra đĩa và trả về kết quả dưới dạng chuỗi cấu hình thông qua thuộc tính info.

c) Lấy thông tin bổ sung của DLL

Hàm GetBarcodeInfo cung cấp các thông tin như version và tác giả.

Ví Dụ Code AutoIt

Dưới đây là ví dụ mã AutoIt minh họa cho các trường hợp trên (tất cả thông báo hiển thị qua ConsoleWrite😞

#RequireAdmin
;~ #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
;~ #AutoIt3Wrapper_UseX64=y
;~ #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;~ #pragma compile(x64, true)


FileDelete(@ScriptDir & "\output_qr.png")
FileDelete(@ScriptDir & "\output_code128.png")

; Đường dẫn đến DLL (chỉnh sửa theo vị trí thực tế của DLL trên hệ thống)
Global Const $g_sDllPath = @AutoItX64 ? @ScriptDir & "\BarCodeGenerator_x64.dll" : @ScriptDir & "\BarCodeGenerator_x86.dll"
; Đường dẫn tới DLL (sửa lại cho đúng đường dẫn)
#include <GDIPlus.au3>

; Định nghĩa kiểu cấu trúc BarcodeResult theo định dạng:
; - wchar[4096]: chuỗi text chứa thông tin cấu hình và kết quả (cố định 4096 ký tự)
; - ptr hBitmap: con trỏ hBitmap trả về khi dùng xuất "in-memory" ảnh
Local $tBarcodeResultDef = "wchar[4096];ptr hBitmap"

ConsoleWrite("; EG 1 -----------------------------" & @CRLF)
; -----------------------------
; PHẦN 1: Gọi CreateBarcode để "in-memory" tạo mã QR
; (FilePath = "" nên hàm trả về HBITMAP, sẽ lưu ảnh PNG qua _GDIPlus)
Local $aRet = DllCall($g_sDllPath, "ptr", "CreateBarcode", _
        "wstr", "Hello World", _       ; textContent
        "wstr", "qr", _                ; outputType
        "wstr", "medium", _            ; eccStr
        "wstr", "000000", _            ; qrColorHex
        "wstr", "", _                  ; bgColorHex (rỗng nghĩa là mặc định)
        "wstr", "png", _               ; outputFormat
        "wstr", "", _                  ; filePath = rỗng => tạo ảnh trong bộ nhớ
        "int", 128, _                  ; sizeParam
        "int", 0, _                    ; scale
        "int", 1)                      ; borderModules

If Not IsArray($aRet) Then
    ConsoleWrite("CreateBarcode call failed." & @CRLF)
Else
    ; $aRet[0] là pointer đến cấu trúc BarcodeResult.
    Local $pResult = $aRet[0]
    ; Gắn cấu trúc BarcodeResult từ con trỏ trả về.
    Local $sBarcodeResult = DllStructCreate($tBarcodeResultDef, $pResult)
    ; Lấy chuỗi thông báo cấu hình & trạng thái
    Local $sInfo = DllStructGetData($sBarcodeResult, 1)
    ConsoleWrite("In-memory QR generation result info:" & @CRLF & $sInfo & @CRLF)

    ; Lấy HBITMAP
    Local $hBmp = DllStructGetData($sBarcodeResult, 2)
    If @error Then
        ConsoleWrite("! Error in DllStructGetData, Code: " & @error & @CRLF)
    EndIf

    ; Sử dụng _GDIPlus để lưu HBITMAP này ra file PNG.
    _GDIPlus_Startup()

    ; Chuyển HBITMAP sang đối tượng GDI+ Bitmap
    Local $hBmpGp = _GDIPlus_BitmapCreateFromHBITMAP($hBmp)
    If $hBmpGp = 0 Then
        ConsoleWrite("Failed to convert HBITMAP to GDI+ Bitmap." & @CRLF)
    Else
        ; Lưu ảnh ra file
        If _GDIPlus_ImageSaveToFile($hBmpGp, @ScriptDir & "\output_qr.png") Then
            ConsoleWrite("Saved in-memory QR code to file " & @ScriptDir & "\output_qr.png" & @CRLF)
            If FileExists(@ScriptDir & "\output_qr.png") Then ShellExecute(@ScriptDir & "\output_qr.png")
        Else
            ConsoleWrite("! Failed to save the image to file." & @CRLF)
        EndIf
        _GDIPlus_ImageDispose($hBmpGp)
    EndIf
    _GDIPlus_Shutdown()
EndIf

ConsoleWrite("; EG 2 -----------------------------" & @CRLF)
; -----------------------------
; PHẦN 2: Gọi CreateBarcode để xuất kết quả trực tiếp ra file.
; Ở đây, filePath được cung cấp nên DLL sẽ ghi file vào đĩa
Local $aRet2 = DllCall($g_sDllPath, "ptr", "CreateBarcode", _
        "wstr", "Hello World", _
        "wstr", "code128", _
        "wstr", "medium", _
        "wstr", "000000", _
        "wstr", "", _
        "wstr", "png", _
        "wstr", @ScriptDir & "\output_code128.png", _ ; Xuất trực tiếp ra file
        "int", 128, _
        "int", 0, _
        "int", 4)

If Not IsArray($aRet2) Then
    ConsoleWrite("! File output CreateBarcode call failed." & @CRLF)
Else
    Local $pResult2 = $aRet2[0]
    Local $sBarcodeResult2 = DllStructCreate($tBarcodeResultDef, $pResult2)
    Local $sInfo2 = DllStructGetData($sBarcodeResult2, 1)
    If FileExists(@ScriptDir & "\output_code128.png") Then ShellExecute(@ScriptDir & "\output_code128.png")
    ConsoleWrite("File output QR generation result info:" & @CRLF & $sInfo2 & @CRLF)
EndIf

ConsoleWrite("; Test 3 -----------------------------" & @CRLF)
; -----------------------------
; PHẦN 3: Gọi GetBarcodeInfo để lấy thêm thông tin về tác giả, version,...
Local $aRet3 = DllCall($g_sDllPath, "wstr", "GetBarcodeInfo")
If Not IsArray($aRet3) Then
    ConsoleWrite("! GetBarcodeInfo call failed." & @CRLF)
Else
    MsgBox(64 + 262144, "About ", $aRet3[0] & @CRLF)
    ;MsgBox(64 + 262144, "GetBarcodeInfo: ", CorrectFromCP1252ToUTF8($aRet3[0]) & @CRLF)
EndIf


; Hàm chuyển đổi chuỗi bị hiểu nhầm theo CP1252 về đúng UTF-8
Func CorrectFromCP1252ToUTF8($sMisinterpreted)
    ; Chuyển chuỗi (theo CP1252) thành binary
    Local $aBinary = StringToBinary($sMisinterpreted, 0)
    ; Tạo đối tượng ADODB.Stream để chuyển đổi encoding
    Local $oStream = ObjCreate("ADODB.Stream")
    If Not IsObj($oStream) Then
        MsgBox(16, "Error", "Không thể tạo đối tượng ADODB.Stream.")
        Return $sMisinterpreted
    EndIf
    ; Ghi binary vào stream
    $oStream.Type = 1    ; 1: Binary
    $oStream.Open
    $oStream.Write($aBinary)
    ; Đặt lại vị trí stream và chuyển sang chế độ text
    $oStream.Position = 0
    $oStream.Type = 2    ; 2: Text
    $oStream.Charset = "utf-8"   ; Đọc dữ liệu như UTF-8
    Local $sCorrect = $oStream.ReadText()
    $oStream.Close()
    Return $sCorrect
EndFunc   ;==>CorrectFromCP1252ToUTF8

4. Kết Luận

  • Tóm tắt: Bài viết đã giới thiệu cấu trúc các hàm export của BarCodeGenerator.dll với 2 hàm chính là CreateBarcodeGetBarcodeInfo. Các tham số bao gồm nội dung ký tự cần tạo mã, loại mã, error correction (ECC), màu sắc, định dạng xuất, và đường dẫn file (nếu có).

  • Sử dụng với AutoIt: Việc tương tác từ AutoIt được thực hiện thông qua DllCall và định nghĩa cấu trúc trả về bằng DllStructCreate(). Ngoài ra, với các file ảnh được tạo ra trong bộ nhớ, _GDIPlus (qua UDF _GDIPlus.au3) được dùng để chuyển HBITMAP thành file ảnh thực tế.

  • Mẹo: Khi cần xuất file trực tiếp, DLL sẽ kiểm tra và tạo cây thư mục nếu cần, giúp tránh lỗi ghi file do đường dẫn không hợp lệ.

Hy vọng bài viết này sẽ giúp bạn dễ dàng tích hợp và sử dụng BarCodeGenerator.dll trong các dự án AutoIt của mình. Chúc bạn thành công!

 

>> Link Download Dll/Exe  

(Because it contains exe cli file so to avoid warning when downloading I set password as number 1)

 

Bar-Code-Generator-x64-Screenshot-exe.pn

Limitation: due to using c++17, it will not support running on windows XP

________________________________________________________

 QR Code generator library  (c)  Nayuki

Edited by Trong
Article format

Regards,
 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...