1

我试图从 Rust 调用 Btrieve(一个非常古老的数据库引擎)。这有点长,但这是我第一次从 Rust 尝试 FFI,我想描述我所做的一切。

Btrieve 引擎在 DLL w3btrv7.dll 中实现,它是一个 32 位 DLL。我使用 32 位 MSVC 工具为它创建了一个导入库(官方没有提供):

lib /Def:w3btrv7.def /Out:w3btrv7.lib /Machine:x86

然后我安装了 32 位 Rust 工具链stable-i686-pc-windows-msvc 并将其设置为我的默认值。Bindgen barfs 在官方 Btrieve 标题上,所以我必须自己制作。幸运的是,我们只需要包装一个函数, BTRCALL.

我的 wrapper.h 中有这个:

short int BTRCALL(
    unsigned short  operation,
    void*           posBlock,
    void*           dataBuffer,
    unsigned short* dataLength,
    void*           keyBuffer,
    unsigned char   keyLength,
    char            ckeynum);

我链接为:

println!("cargo:rustc-link-lib=./src/pervasive/w3btrv7");

这似乎可行:程序运行,是一个 32 位 exe,我可以在 Process Explorer 中看到它已加载w3btrv7.dll.

当我通过 bindgen 发送标头时,我得到:

extern "C" {
    pub fn BTRCALL(
        operation: ::std::os::raw::c_ushort,
        posBlock: *mut ::std::os::raw::c_void,
        dataBuffer: *mut ::std::os::raw::c_void,
        dataLength: *mut ::std::os::raw::c_ushort,
        keyBuffer: *mut ::std::os::raw::c_void,
        keyLength: ::std::os::raw::c_uchar,
        ckeynum: ::std::os::raw::c_char,
    ) -> ::std::os::raw::c_short;
}

类型和大小似乎都正确计算,并且它们与我从 C# 应用程序中获得的 DllImport 完美匹配:

[DllImport("w3btrv7.dll", CharSet = CharSet.Ansi)]
private static extern short BTRCALL(
    ushort operation, // In C#, ushort = UInt16.
    [MarshalAs(UnmanagedType.LPArray, SizeConst = 128)] byte[] posBlock,
    [MarshalAs(UnmanagedType.LPArray)] byte[] dataBuffer,
    ref ushort dataLength,
    [MarshalAs(UnmanagedType.LPArray)] byte[] keyBuffer,
    byte keyLength,     // unsigned byte
    char keyNumber);    // 2 byte char

略有不同,但我在keyNumber有符号和无符号变体中都尝试了字节和短裤,但它仍然不起作用。

不幸的是,当我运行我的程序时,它在第一次调用 BTRCALL 后就崩溃了。(嗯,实际上是当这个调用所在的函数返回时)。我已经将所有参数提取到局部变量中并检查了它们的类型,并且看起来都是正确的:

let op: u16 = 0;
let mut pos_block: [u8; 128] = self.pos_block.clone();
let pos_block_ptr: *mut std::ffi::c_void = pos_block.as_mut_ptr() as *mut _;
let mut data_buffer: [u8; 32768] = self.data_buffer.clone();
let data_buffer_ptr: *mut std::ffi::c_void = data_buffer.as_mut_ptr() as *mut _;
let mut data_length: u16 = data_buffer.len() as u16;
let mut key_buffer: [u8; 256] = self.key_buffer.clone();
let key_buffer_ptr: *mut std::ffi::c_void = key_buffer.as_mut_ptr() as *mut _;
let key_length: u8 = 255; //self.key_length;
let key_number: i8 = self.key_number.try_into().unwrap();

let status: i16 = BTRCALL(
    op,
    pos_block_ptr,
    data_buffer_ptr,
    &mut data_length,
    key_buffer_ptr,
    key_length,
    key_number
);

它使程序崩溃

error: process didn't exit successfully: `target\debug\blah.exe` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)

根据我的阅读,这可能是由于地址访问不正确造成的。

实际上,当我进行一些跟踪以检查变量时,会出现一些非常有趣的行为,因为按值传递的局部变量似乎被覆盖了。这里的日志只是转储缓冲区的前 30 个字节,因为其余的只是零:

pos_block = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
pos_block_ptr = 0xad6524
data_buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
data_buffer_ptr = 0xad65a8
data_length = 32768
key_buffer = [34, 67, 58, 92, 116, 101, 109, 112, 92, 99, 115, 115, 92, 120, 100, 98, 92, 67, 65, 83, 69, 46, 68, 66, 34, 0, 0, 0, 0, 0]
key_buffer_ptr = 0xade5b0
key_length = 255
key_number = 0

>>>>>>>>>>>>>>> AFTER THE CALL TO BTRCALL:

pos_block = [0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 203, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0]
pos_block_ptr = 0x0
data_buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
data_buffer_ptr = 0x42442e45
data_length = 0
key_buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_buffer_ptr = 0x0
key_length = 173
key_number = 0
BTRCALL() returned B_NO_ERROR

除其他外,通知pos_block_ptr已设置为 0。相反,从 C# 代码中成功执行完全相同的调用只是将一些数据写入前 18 个字节,pos_block 而不会更改任何其他变量。

就好像它有点发疯了,刚刚开始覆盖记忆......

在这一点上,我不知道下一步该尝试什么。

4

1 回答 1

0

将声明从更改extern "C"extern "stdcall" 工作:

extern "stdcall" {
    pub fn BTRCALL(
        operation: ::std::os::raw::c_ushort,
        posBlock: *mut ::std::os::raw::c_void,
        dataBuffer: *mut ::std::os::raw::c_void,
        dataLength: *mut ::std::os::raw::c_ushort,
        keyBuffer: *mut ::std::os::raw::c_void,
        keyLength: ::std::os::raw::c_uchar,
        ckeynum: ::std::os::raw::c_char,
    ) -> ::std::os::raw::c_short;
}
于 2020-01-01T17:28:51.577 回答