move bin file and add fallback

This commit is contained in:
Christoph Brandau
2025-11-15 20:56:33 +01:00
parent f22ac8075a
commit c8718fb62c
7 changed files with 155 additions and 98 deletions
View File
View File
+155 -98
View File
@@ -1,6 +1,6 @@
#![allow(non_snake_case)]
use libloading::{Library, Symbol};
use std::path::Path;
use libloading::{ Library, Symbol };
use std::path::{ Path, PathBuf };
/// Pointer to a UTF-16 string (wide char) terminated with `0`.
pub type PCWSTR = *const u16;
@@ -256,15 +256,21 @@ type DisconnectFn = unsafe extern "C" fn() -> OpenReturnCodes;
type OpenProjectFn = unsafe extern "C" fn(PCWSTR) -> OpenReturnCodes;
type CreateProjectFn = unsafe extern "C" fn(PCWSTR, PCWSTR) -> OpenReturnCodes;
type CreateProjectWithScalingFn = unsafe extern "C" fn(PCWSTR, PCWSTR, PCWSTR) -> OpenReturnCodes;
type CreateProjectWithScalingNcuNumberFn =
unsafe extern "C" fn(PCWSTR, PCWSTR, PCWSTR, u32) -> OpenReturnCodes;
type CreateProjectWithScalingNcuNumberFn = unsafe extern "C" fn(
PCWSTR,
PCWSTR,
PCWSTR,
u32
) -> OpenReturnCodes;
type SaveProjectFn = unsafe extern "C" fn() -> OpenReturnCodes;
type CloseProjectFn = unsafe extern "C" fn() -> OpenReturnCodes;
type BootControllerFn = unsafe extern "C" fn() -> OpenReturnCodes;
type ShutdownControllerFn = unsafe extern "C" fn() -> OpenReturnCodes;
type ResetControllerFn = unsafe extern "C" fn() -> OpenReturnCodes;
type RegisterForEventsFn =
unsafe extern "C" fn(u32, *const OpenEventMaskDefinition) -> OpenReturnCodes;
type RegisterForEventsFn = unsafe extern "C" fn(
u32,
*const OpenEventMaskDefinition
) -> OpenReturnCodes;
type UnregisterEventsFn = unsafe extern "C" fn() -> OpenReturnCodes;
type WaitForEventsFn = unsafe extern "C" fn() -> OpenReturnCodes;
type ContinueSimulationFn = unsafe extern "C" fn() -> OpenReturnCodes;
@@ -411,9 +417,20 @@ pub struct OpenApi {
impl OpenApi {
/// Load DLL from a path, binding required symbols.
///
/// If `path` resolves to an empty string (e.g. `""`), the default `bin/open.dll`
/// relative to the current working directory is used.
pub unsafe fn load<P: AsRef<Path>>(path: P) -> Result<Self, libloading::Error> {
// Load the DLL
let lib = Library::new(path.as_ref())?;
let provided_path = path.as_ref();
let fallback_path;
let dll_path = if provided_path.as_os_str().is_empty() {
fallback_path = PathBuf::from("bin").join("open.dll");
fallback_path.as_path()
} else {
provided_path
};
let lib = Library::new(dll_path)?;
// Extend the library's lifetime to 'static by leaking it.
// (Alternatively: design OpenApi with lifetimes and avoid leaking.)
let lib: &'static Library = Box::leak(Box::new(lib));
@@ -424,13 +441,15 @@ impl OpenApi {
let disconnect = load_symbol::<DisconnectFn>(lib, b"disconnect")?;
let open_project = load_symbol::<OpenProjectFn>(lib, b"openProject")?;
let create_project = load_symbol::<CreateProjectFn>(lib, b"createProject")?;
let create_project_with_scaling =
load_symbol::<CreateProjectWithScalingFn>(lib, b"createProjectWithScaling")?;
let create_project_with_scaling_ncu_number = load_symbol::<
CreateProjectWithScalingNcuNumberFn,
>(
lib, b"createProjectWithScalingNCUNumber"
let create_project_with_scaling = load_symbol::<CreateProjectWithScalingFn>(
lib,
b"createProjectWithScaling"
)?;
let create_project_with_scaling_ncu_number =
load_symbol::<CreateProjectWithScalingNcuNumberFn>(
lib,
b"createProjectWithScalingNCUNumber"
)?;
let save_project = load_symbol::<SaveProjectFn>(lib, b"saveProject")?;
let close_project = load_symbol::<CloseProjectFn>(lib, b"closeProject")?;
let boot_controller = load_symbol::<BootControllerFn>(lib, b"bootController")?;
@@ -443,36 +462,62 @@ impl OpenApi {
let get_dictionary_id = load_symbol::<GetDictionaryIdFn>(lib, b"getDictionaryId")?;
let register_watch = load_symbol::<RegisterWatchFn>(lib, b"registerWatch")?;
let unregister_watch = load_symbol::<UnregisterWatchFn>(lib, b"unregisterWatch")?;
let get_dictionary_element_type =
load_symbol::<GetDictionaryElementTypeFn>(lib, b"getDictionaryElementType")?;
let read_unsigned_integer_64_bit_array =
load_symbol::<ReadUnsignedInteger64BitArrayFn>(lib, b"readUnsignedInteger64BitArray")?;
let read_signed_integer_32_bit_array =
load_symbol::<ReadSignedInteger32BitArrayFn>(lib, b"readSignedInteger32BitArray")?;
let read_unsigned_integer_32_bit_array =
load_symbol::<ReadUnsignedInteger32BitArrayFn>(lib, b"readUnsignedInteger32BitArray")?;
let read_unsigned_integer_16_bit_array =
load_symbol::<ReadUnsignedInteger16BitArrayFn>(lib, b"readUnsignedInteger16BitArray")?;
let read_unsigned_integer_8_bit_array =
load_symbol::<ReadUnsignedInteger8BitArrayFn>(lib, b"readUnsignedInteger8BitArray")?;
let get_dictionary_element_type = load_symbol::<GetDictionaryElementTypeFn>(
lib,
b"getDictionaryElementType"
)?;
let read_unsigned_integer_64_bit_array = load_symbol::<ReadUnsignedInteger64BitArrayFn>(
lib,
b"readUnsignedInteger64BitArray"
)?;
let read_signed_integer_32_bit_array = load_symbol::<ReadSignedInteger32BitArrayFn>(
lib,
b"readSignedInteger32BitArray"
)?;
let read_unsigned_integer_32_bit_array = load_symbol::<ReadUnsignedInteger32BitArrayFn>(
lib,
b"readUnsignedInteger32BitArray"
)?;
let read_unsigned_integer_16_bit_array = load_symbol::<ReadUnsignedInteger16BitArrayFn>(
lib,
b"readUnsignedInteger16BitArray"
)?;
let read_unsigned_integer_8_bit_array = load_symbol::<ReadUnsignedInteger8BitArrayFn>(
lib,
b"readUnsignedInteger8BitArray"
)?;
let read_bool_array = load_symbol::<ReadBoolArrayFn>(lib, b"readBoolArray")?;
let read_double_array = load_symbol::<ReadDoubleArrayFn>(lib, b"readDoubleArray")?;
let read_string = load_symbol::<ReadStringFn>(lib, b"readString")?;
let read_string_array = load_symbol::<ReadStringArrayFn>(lib, b"readStringArray")?;
let read_unsigned_integer_16_bit =
load_symbol::<ReadUnsignedInteger16BitFn>(lib, b"readUnsignedInteger16Bit")?;
let read_signed_integer_16_bit =
load_symbol::<ReadSignedInteger16BitFn>(lib, b"readSignedInteger16Bit")?;
let read_unsigned_integer_32_bit =
load_symbol::<ReadUnsignedInteger32BitFn>(lib, b"readUnsignedInteger32Bit")?;
let read_signed_integer_32_bit =
load_symbol::<ReadSignedInteger32BitFn>(lib, b"readSignedInteger32Bit")?;
let read_unsigned_integer_64_bit =
load_symbol::<ReadUnsignedInteger64BitFn>(lib, b"readUnsignedInteger64Bit")?;
let read_signed_integer_64_bit =
load_symbol::<ReadSignedInteger64BitFn>(lib, b"readSignedInteger64Bit")?;
let get_num_array_elements =
load_symbol::<GetNumArrayElementsFn>(lib, b"getNumArrayElements")?;
let read_unsigned_integer_16_bit = load_symbol::<ReadUnsignedInteger16BitFn>(
lib,
b"readUnsignedInteger16Bit"
)?;
let read_signed_integer_16_bit = load_symbol::<ReadSignedInteger16BitFn>(
lib,
b"readSignedInteger16Bit"
)?;
let read_unsigned_integer_32_bit = load_symbol::<ReadUnsignedInteger32BitFn>(
lib,
b"readUnsignedInteger32Bit"
)?;
let read_signed_integer_32_bit = load_symbol::<ReadSignedInteger32BitFn>(
lib,
b"readSignedInteger32Bit"
)?;
let read_unsigned_integer_64_bit = load_symbol::<ReadUnsignedInteger64BitFn>(
lib,
b"readUnsignedInteger64Bit"
)?;
let read_signed_integer_64_bit = load_symbol::<ReadSignedInteger64BitFn>(
lib,
b"readSignedInteger64Bit"
)?;
let get_num_array_elements = load_symbol::<GetNumArrayElementsFn>(
lib,
b"getNumArrayElements"
)?;
let read_bit = load_symbol::<ReadBitFn>(lib, b"readBit")?;
let read_bool = load_symbol::<ReadBoolFn>(lib, b"readBool")?;
let read_byte = load_symbol::<ReadByteFn>(lib, b"readByte")?;
@@ -481,8 +526,10 @@ impl OpenApi {
let read_double_word = load_symbol::<ReadDoubleWordFn>(lib, b"readDoubleWord")?;
let read_long_word = load_symbol::<ReadLongWordFn>(lib, b"readLongWord")?;
let read_next_event = load_symbol::<ReadNextEventFn>(lib, b"readNextEvent")?;
let read_next_event_with_ncu_index =
load_symbol::<ReadNextEventWithNcuIndexFn>(lib, b"readNextEventWithNcuIndex")?;
let read_next_event_with_ncu_index = load_symbol::<ReadNextEventWithNcuIndexFn>(
lib,
b"readNextEventWithNcuIndex"
)?;
let write_bit = load_symbol::<WriteBitFn>(lib, b"writeBit")?;
let write_bool = load_symbol::<WriteBoolFn>(lib, b"writeBool")?;
let write_byte = load_symbol::<WriteByteFn>(lib, b"writeByte")?;
@@ -502,20 +549,34 @@ impl OpenApi {
let delete_directory = load_symbol::<DeleteDirectoryFn>(lib, b"deleteDirectory")?;
let create_directory = load_symbol::<CreateDirectoryFn>(lib, b"createDirectory")?;
let list_directory = load_symbol::<ListDirectoryFn>(lib, b"listDirectory")?;
let get_version_information =
load_symbol::<GetVersionInformationFn>(lib, b"getVersionInformation")?;
let get_project_information =
load_symbol::<GetProjectInformationFn>(lib, b"getProjectInformation")?;
let get_current_project_information =
load_symbol::<GetCurrentProjectInformationFn>(lib, b"getCurrentProjectInformation")?;
let get_current_project_state =
load_symbol::<GetCurrentProjectStateFn>(lib, b"getCurrentProjectState")?;
let import_project_settings =
load_symbol::<ImportProjectSettingsFn>(lib, b"importProjectSettings")?;
let export_project_settings =
load_symbol::<ExportProjectSettingsFn>(lib, b"exportProjectSettings")?;
let write_unsigned_integer_8_bit_array =
load_symbol::<WriteUnsignedInteger8BitArrayFn>(lib, b"writeUnsignedInteger8BitArray")?;
let get_version_information = load_symbol::<GetVersionInformationFn>(
lib,
b"getVersionInformation"
)?;
let get_project_information = load_symbol::<GetProjectInformationFn>(
lib,
b"getProjectInformation"
)?;
let get_current_project_information = load_symbol::<GetCurrentProjectInformationFn>(
lib,
b"getCurrentProjectInformation"
)?;
let get_current_project_state = load_symbol::<GetCurrentProjectStateFn>(
lib,
b"getCurrentProjectState"
)?;
let import_project_settings = load_symbol::<ImportProjectSettingsFn>(
lib,
b"importProjectSettings"
)?;
let export_project_settings = load_symbol::<ExportProjectSettingsFn>(
lib,
b"exportProjectSettings"
)?;
let write_unsigned_integer_8_bit_array = load_symbol::<WriteUnsignedInteger8BitArrayFn>(
lib,
b"writeUnsignedInteger8BitArray"
)?;
let get_number_of_ncus = load_symbol::<GetNumberOfNcusFn>(lib, b"getNumberOfNCUs")?;
Ok(Self {
@@ -624,7 +685,7 @@ impl OpenApi {
pub unsafe fn create_project(
&self,
str_file_name: PCWSTR,
str_version: PCWSTR,
str_version: PCWSTR
) -> OpenReturnCodes {
(self.create_project)(str_file_name, str_version)
}
@@ -634,7 +695,7 @@ impl OpenApi {
&self,
str_file_name: PCWSTR,
str_version: PCWSTR,
str_nck_scaling: PCWSTR,
str_nck_scaling: PCWSTR
) -> OpenReturnCodes {
(self.create_project_with_scaling)(str_file_name, str_version, str_nck_scaling)
}
@@ -645,13 +706,13 @@ impl OpenApi {
str_file_name: PCWSTR,
str_version: PCWSTR,
str_nck_scaling: PCWSTR,
ncu_number: u32,
ncu_number: u32
) -> OpenReturnCodes {
(self.create_project_with_scaling_ncu_number)(
str_file_name,
str_version,
str_nck_scaling,
ncu_number,
ncu_number
)
}
@@ -684,7 +745,7 @@ impl OpenApi {
pub unsafe fn register_for_events(
&self,
event_list_length: u32,
event_list: *const OpenEventMaskDefinition,
event_list: *const OpenEventMaskDefinition
) -> OpenReturnCodes {
(self.register_for_events)(event_list_length, event_list)
}
@@ -708,7 +769,7 @@ impl OpenApi {
pub unsafe fn get_dictionary_id(
&self,
dictionary_element: PCWSTR,
dictionary_id: *mut u32,
dictionary_id: *mut u32
) -> OpenReturnCodes {
(self.get_dictionary_id)(dictionary_element, dictionary_id)
}
@@ -717,7 +778,7 @@ impl OpenApi {
pub unsafe fn register_watch(
&self,
watch_list_length: u32,
dictionary_id_list: *const u32,
dictionary_id_list: *const u32
) -> OpenReturnCodes {
(self.register_watch)(watch_list_length, dictionary_id_list)
}
@@ -731,7 +792,7 @@ impl OpenApi {
pub unsafe fn get_dictionary_element_type(
&self,
dictionary_id: u32,
value: *mut u8,
value: *mut u8
) -> OpenReturnCodes {
(self.get_dictionary_element_type)(dictionary_id, value)
}
@@ -741,7 +802,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut u64,
value_list: *mut u64
) -> OpenReturnCodes {
(self.read_unsigned_integer_64_bit_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -751,7 +812,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut i32,
value_list: *mut i32
) -> OpenReturnCodes {
(self.read_signed_integer_32_bit_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -761,7 +822,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut u32,
value_list: *mut u32
) -> OpenReturnCodes {
(self.read_unsigned_integer_32_bit_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -771,7 +832,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut u16,
value_list: *mut u16
) -> OpenReturnCodes {
(self.read_unsigned_integer_16_bit_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -781,7 +842,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut u8,
value_list: *mut u8
) -> OpenReturnCodes {
(self.read_unsigned_integer_8_bit_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -791,7 +852,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut bool,
value_list: *mut bool
) -> OpenReturnCodes {
(self.read_bool_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -801,7 +862,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *mut f64,
value_list: *mut f64
) -> OpenReturnCodes {
(self.read_double_array)(dictionary_id, value_list_max_length, value_list)
}
@@ -811,7 +872,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
string_max_length: u32,
string_buffer: PWSTR,
string_buffer: PWSTR
) -> OpenReturnCodes {
(self.read_string)(dictionary_id, string_max_length, string_buffer)
}
@@ -822,13 +883,13 @@ impl OpenApi {
dictionary_id: u32,
string_array_max_length: u32,
string_max_length: u32,
string_array: PPWSTR,
string_array: PPWSTR
) -> OpenReturnCodes {
(self.read_string_array)(
dictionary_id,
string_array_max_length,
string_max_length,
string_array,
string_array
)
}
@@ -836,7 +897,7 @@ impl OpenApi {
pub unsafe fn read_unsigned_integer_16_bit(
&self,
dictionary_id: u32,
value: *mut u16,
value: *mut u16
) -> OpenReturnCodes {
(self.read_unsigned_integer_16_bit)(dictionary_id, value)
}
@@ -845,7 +906,7 @@ impl OpenApi {
pub unsafe fn read_signed_integer_16_bit(
&self,
dictionary_id: u32,
value: *mut i16,
value: *mut i16
) -> OpenReturnCodes {
(self.read_signed_integer_16_bit)(dictionary_id, value)
}
@@ -854,7 +915,7 @@ impl OpenApi {
pub unsafe fn read_unsigned_integer_32_bit(
&self,
dictionary_id: u32,
value: *mut u32,
value: *mut u32
) -> OpenReturnCodes {
(self.read_unsigned_integer_32_bit)(dictionary_id, value)
}
@@ -863,7 +924,7 @@ impl OpenApi {
pub unsafe fn read_signed_integer_32_bit(
&self,
dictionary_id: u32,
value: *mut i32,
value: *mut i32
) -> OpenReturnCodes {
(self.read_signed_integer_32_bit)(dictionary_id, value)
}
@@ -872,7 +933,7 @@ impl OpenApi {
pub unsafe fn read_unsigned_integer_64_bit(
&self,
dictionary_id: u32,
value: *mut u64,
value: *mut u64
) -> OpenReturnCodes {
(self.read_unsigned_integer_64_bit)(dictionary_id, value)
}
@@ -881,7 +942,7 @@ impl OpenApi {
pub unsafe fn read_signed_integer_64_bit(
&self,
dictionary_id: u32,
value: *mut i64,
value: *mut i64
) -> OpenReturnCodes {
(self.read_signed_integer_64_bit)(dictionary_id, value)
}
@@ -890,7 +951,7 @@ impl OpenApi {
pub unsafe fn get_num_array_elements(
&self,
dictionary_id: u32,
num_array_elements: *mut u32,
num_array_elements: *mut u32
) -> OpenReturnCodes {
(self.get_num_array_elements)(dictionary_id, num_array_elements)
}
@@ -939,7 +1000,7 @@ impl OpenApi {
pub unsafe fn read_next_event_with_ncu_index(
&self,
event: *mut u64,
ncu_index: *mut i32,
ncu_index: *mut i32
) -> OpenReturnCodes {
(self.read_next_event_with_ncu_index)(event, ncu_index)
}
@@ -984,7 +1045,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_length: u32,
value_list: *const bool,
value_list: *const bool
) -> OpenReturnCodes {
(self.write_bool_array)(dictionary_id, value_list_length, value_list)
}
@@ -1003,7 +1064,7 @@ impl OpenApi {
pub unsafe fn get_card_path(
&self,
card_path_max_length: u32,
card_path: PWSTR,
card_path: PWSTR
) -> OpenReturnCodes {
(self.get_card_path)(card_path_max_length, card_path)
}
@@ -1013,20 +1074,16 @@ impl OpenApi {
&self,
card_paths_array_max_length: u32,
card_path_max_length: u32,
card_paths: PPWSTR,
card_paths: PPWSTR
) -> OpenReturnCodes {
(self.get_card_paths)(
card_paths_array_max_length,
card_path_max_length,
card_paths,
)
(self.get_card_paths)(card_paths_array_max_length, card_path_max_length, card_paths)
}
#[inline]
pub unsafe fn get_file(
&self,
source_file_path: PCWSTR,
destination_file_path: PCWSTR,
destination_file_path: PCWSTR
) -> OpenReturnCodes {
(self.get_file)(source_file_path, destination_file_path)
}
@@ -1035,7 +1092,7 @@ impl OpenApi {
pub unsafe fn put_file(
&self,
source_file_path: PCWSTR,
destination_file_path: PCWSTR,
destination_file_path: PCWSTR
) -> OpenReturnCodes {
(self.put_file)(source_file_path, destination_file_path)
}
@@ -1044,7 +1101,7 @@ impl OpenApi {
pub unsafe fn select_file(
&self,
source_file_path: PCWSTR,
channel_name: PCWSTR,
channel_name: PCWSTR
) -> OpenReturnCodes {
(self.select_file)(source_file_path, channel_name)
}
@@ -1068,7 +1125,7 @@ impl OpenApi {
pub unsafe fn list_directory(
&self,
directory_path: PCWSTR,
result_file_path: PCWSTR,
result_file_path: PCWSTR
) -> OpenReturnCodes {
(self.list_directory)(directory_path, result_file_path)
}
@@ -1082,7 +1139,7 @@ impl OpenApi {
pub unsafe fn get_project_information(
&self,
vcp_file_path: PCWSTR,
destination_file_path: PCWSTR,
destination_file_path: PCWSTR
) -> OpenReturnCodes {
(self.get_project_information)(vcp_file_path, destination_file_path)
}
@@ -1090,7 +1147,7 @@ impl OpenApi {
#[inline]
pub unsafe fn get_current_project_information(
&self,
destination_file_path: PCWSTR,
destination_file_path: PCWSTR
) -> OpenReturnCodes {
(self.get_current_project_information)(destination_file_path)
}
@@ -1098,7 +1155,7 @@ impl OpenApi {
#[inline]
pub unsafe fn get_current_project_state(
&self,
project_state: *mut OpenProjectStates,
project_state: *mut OpenProjectStates
) -> OpenReturnCodes {
(self.get_current_project_state)(project_state)
}
@@ -1107,7 +1164,7 @@ impl OpenApi {
pub unsafe fn import_project_settings(
&self,
file_name: PCWSTR,
import_content_file_name: PCWSTR,
import_content_file_name: PCWSTR
) -> OpenReturnCodes {
(self.import_project_settings)(file_name, import_content_file_name)
}
@@ -1122,7 +1179,7 @@ impl OpenApi {
&self,
dictionary_id: u32,
value_list_max_length: u32,
value_list: *const u8,
value_list: *const u8
) -> OpenReturnCodes {
(self.write_unsigned_integer_8_bit_array)(dictionary_id, value_list_max_length, value_list)
}