< 返回版块

dongdong 发表于 2019-10-17 12:51

// C写的 .h 文件

int DLL_EXPORT Encode(IN const char *key, OUT char *value);

我在Python中是这样传参的

value= create_string_buffer(100)
lib.Encode(key, value)

在Rust中我使用的是libloading模块

extern crate libloading;
use libloading::{Library, Symbol};

use std::io::prelude::*;
use std::path::Path;
use std::ffi::CStr;
use std::fs::File;
use std::str;
use std::io;

type Encode = unsafe fn(【?】) -> *const i8;

const LIBRARY_PATH: &'static str = "test.dll";


fn get_value() -> String {

    // 从本地加载DLL文件
    let lib = Library::new(LIBRARY_PATH).unwrap();
    println!("{:?}", lib);

    let key = "111111",
    // 似乎 let mut value: *const c_char; 
    let value = 【?】;

    // 调用DLL获取标识码
    let res = unsafe {

        let func: Symbol<Encode> = lib.get(b"Encode").unwrap();

        let c_buf: *const i8 = func(【?】);
        let c_str: &CStr = CStr::from_ptr(c_buf);
        let str_slice: &str = c_str.to_str().unwrap();
        let str_buf: String = str_slice.to_owned();
        str_buf
    };
    res
}

fn main() {

    let value: String = get_value();
    println!("{:?}", value);
}

请问在Rust我该怎么申请一块内存当作指针传递给DLL呢,谢谢

评论区

写评论
fcbldopb 2019-10-18 07:19

https://doc.rust-lang.org/std/ffi/struct.CString.html#examples-3

1 共 1 条评论, 1 页