Skip to content

Simplify the library API calls using the windows-link macro.#2

Merged
wangfu91 merged 4 commits intomasterfrom
windows-link
Jun 23, 2025
Merged

Simplify the library API calls using the windows-link macro.#2
wangfu91 merged 4 commits intomasterfrom
windows-link

Conversation

@wangfu91
Copy link
Owner

  • Remove the libloading crate
  • Using the link! macro from the windows-link crate to simplify the API calls greatly

This pull request introduces significant changes to the OneOCR codebase, focusing on replacing the dynamic library loading approach with a static linking mechanism using the windows-link crate. This simplifies the codebase, improves type safety, and enhances maintainability. Key changes include the removal of macros for symbol loading, updates to FFI function signatures, and adjustments to error handling and type definitions.

Transition to Static Linking

  • Replaced dynamic library loading (libloading) with static linking using the windows-link crate for all FFI functions in src/ffi.rs, improving type safety and reducing runtime errors.

Updates to FFI Function Signatures and Types

  • Updated FFI function signatures to use *mut c_void instead of i64 for pointers, aligning with the new static linking approach. This change affects multiple methods in src/ffi.rs and src/ocr_engine.rs. [1] [2] [3] [4]
  • Changed the OcrApiError type in src/errors.rs to use i32 instead of i64 for the result field, reflecting the updated FFI function return types.

Removal of Redundant Code

  • Removed macros (load_symbol and release_ocr_resource) used for dynamic symbol loading in src/lib.rs, as they are no longer needed with static linking.
  • Eliminated redundant libloading imports and references in files such as src/ocr_engine.rs and src/ocr_line.rs. [1] [2]

Adjustments to OcrEngine

  • Simplified the OcrEngine struct by removing the Library field, as dynamic library management is no longer required. Updated related methods to directly call statically linked functions. [1] [2]
  • Updated methods like get_max_recognition_line_count, set_resize_resolution, and run_pipeline to reflect the new FFI function signatures and type changes. [1] [2] [3]

Adjustments to OcrLine

  • Updated the OcrLine struct to use *mut c_void for the line_handle field and modified related methods to use the new static linking approach. [1] [2]
  • Simplified the get_line_style method by removing dynamic symbol loading and directly calling the statically linked function.

Copilot AI review requested due to automatic review settings June 23, 2025 14:46
@wangfu91 wangfu91 self-assigned this Jun 23, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces dynamic library loading with static linking via the windows-link macro, updates FFI signatures to use *mut c_void and i32, and removes redundant loading macros and the libloading crate.

  • Transition all FFI bindings to windows-link::link! and drop libloading
  • Change pointer and return types in FFI signatures and update corresponding method signatures
  • Remove custom load_symbol!/release_ocr_resource! macros and adjust resource cleanup

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ocr_word.rs Updated OcrWord::new to use *mut c_void, removed libloading and symbol-loading macros
src/ocr_result.rs Simplified OcrResult to hold *mut c_void, removed lifetimes and release macro
src/ocr_line.rs Updated OcrLine to use *mut c_void, removed dynamic symbol loading
src/ocr_engine.rs Replaced dynamic loading with direct calls, changed option pointers to *mut c_void and return types to i32
src/lib.rs Removed load_symbol!/release_ocr_resource! macro definitions
src/ffi.rs Added link! definitions for all FFI functions, updated signatures
src/errors.rs Changed OcrApiError result field to i32
Cargo.toml Dropped libloading, added windows-link dependency
Comments suppressed due to low confidence (3)

src/errors.rs:16

  • [nitpick] The error message is confusing and repeats 'result'. Consider rephrasing to something like Failed to run OCR API (code: {result}): {message} for clarity.
    #[error("Failed to run ocr API {result}, result: {message}")]

src/ocr_engine.rs:85

  • [nitpick] Consider adding unit tests for get_max_recognition_line_count (and other newly updated methods) to verify the static FFI bindings and return type conversions behave as expected.
    pub fn get_max_recognition_line_count(&self) -> Result<i32, OneOcrError> {

src/ffi.rs:5

  • The FFI signature for OcrInitOptionsSetUseModelDelayLoad no longer accepts the model delay-load flag (c_char), causing a mismatch with the underlying C API and potential undefined behavior. Restore the second parameter for the flag and update calls accordingly.
link!("oneocr.dll"  "system"  fn OcrInitOptionsSetUseModelDelayLoad(init_option: *mut c_void) -> i32);

wangfu91 and others added 3 commits June 23, 2025 22:48
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@wangfu91 wangfu91 merged commit 34d725c into master Jun 23, 2025
1 check passed
@wangfu91 wangfu91 deleted the windows-link branch June 23, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant