Delphi 调用 rust 编写的 DLL 文件
Delphi 是一种基于对象的、可视化的、事件驱动的编程语言,它可以用来开发各种 Windows 应用,如桌面软件、数据库系统、网络服务等。delphi 支持调用外部的 DLL 文件,以实现与其他语言或库的互操作。
rust 是一种现代的、高性能的、安全的编程语言,它可以用来开发各种应用,如操作系统、网络服务、游戏、嵌入式系统等。rust 也支持编写和导出 DLL 文件,以提供给其他语言或程序调用。
要在 Delphi 中调用 rust 编写的 DLL 文件,你需要做以下几件事:
- 在 rust 中编写你想要导出的函数,并使用 #[no_mangle] 和 extern “C” 属性标记它们,以保证它们使用 C 风格的名称和调用约定1。例如:
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
a + b
}
- 在 rust 中使用 cargo new --lib 命令创建一个新的库项目,并将你编写的函数放在 src/lib.rs 文件中1。在Cargo.toml 文件中,添加以下内容,以指定生成 DLL 文件:
[lib]
name = "myrustlib"
crate-type = ["cdylib"]
- 在 rust 中使用 cargo build 命令编译你的库项目,并在 target/debug 或 target/release
文件夹中找到生成的 DLL 文件1。例如:myrustlib.dll。 - 在 Delphi 中创建一个新的项目,并将 rust 生成的 DLL 文件复制到项目文件夹中。Delphi在目标平台中添加Windows 64-bit作为当前工程目标, 目前主要的Windows 10, Windows 11都是64位的,rust默认安装也是64位的。
- 在 Delphi 中使用 external 关键字声明你想要调用的 rust 函数,并指定它们所在的 DLL
文件名和调用约定。例如:
function add(a: Integer; b: Integer): Integer; cdecl; external 'myrustlib.dll';
- 在 Delphi 中编写你想要使用 rust 函数的代码,并直接调用它们23。例如: procedure
TForm1.Button1Click(Sender: TObject);
var
x, y, z: Integer;
begin
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
z := add(x, y);
ShowMessage(IntToStr(z));
end;
这样,你就完成了在 Delphi 中调用 rust 编写的 DLL 文件的过程。如果你想要了解更多关于 Delphi 和 rust 的信息,你可以参考以下链接:
Calling Rust functions from other languages
How to call rust code from delphi
Properly call external dll in Delphi?
How to Write and Call DLL’s within Delphi
1088



被折叠的 条评论
为什么被折叠?



