Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 有没有办法将DLL文件放入exe程序?_Rust_Rust Cargo - Fatal编程技术网

Rust 有没有办法将DLL文件放入exe程序?

Rust 有没有办法将DLL文件放入exe程序?,rust,rust-cargo,Rust,Rust Cargo,我写了一个工具,它可以通过dll获取设备id,并将id复制到剪贴板 extern crate clipboard; use clipboard::ClipboardProvider; use clipboard::ClipboardContext; extern crate libloading; use libloading::{Library, Symbol}; use std::ffi::CStr; use std::str; type GetHylinkDeviceId = uns

我写了一个工具,它可以通过dll获取设备id,并将id复制到剪贴板

extern crate clipboard;
use clipboard::ClipboardProvider;
use clipboard::ClipboardContext;

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

use std::ffi::CStr;
use std::str;

type GetHylinkDeviceId = unsafe fn() -> *const i8;

fn copy_to_clipboard(x3id: String) {
    let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
    println!("{:?}", ctx.get_contents());
    ctx.set_contents(x3id);
}

fn get_x3id() -> String {

    let library_path = "PcInfo.dll";
    println!("Loading add() from {:?}", library_path);

    let lib = Library::new(library_path).unwrap();

    let x3id = unsafe {

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

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

    };
    x3id
}

fn main() {

    let x3id: String = get_x3id();
    println!("{:?}", x3id);

    copy_to_clipboard(x3id)

}
外部板条箱剪贴板;
使用剪贴板::ClipboardProvider;
使用剪贴板::ClipboardContext;
外部板条箱装载;
使用libloading::{Library,Symbol};
使用std::ffi::CStr;
使用std::str;
键入GetHylinkDeviceId=unsafe fn()->*const i8;
fn将\u复制到\u剪贴板(x3id:String){
让mut ctx:ClipboardContext=ClipboardProvider::new().unwrap();
println!(“{:?}”,ctx.get_contents());
ctx.set_内容(x3id);
}
fn get_x3id()->字符串{
让库_path=“PcInfo.dll”;
println!(“从{:?}加载add(),库路径);
让lib=Library::new(Library_path).unwrap();
让x3id=不安全{
让func:Symbol=lib.get(b“GetHylinkDeviceId”).unwrap();
设c_buf:*常数i8=func();
设c_str:&CStr=unsafe{CStr::from_ptr(c_buf)};
让str_切片:&str=c_str.to_str().unwrap();
让str_buf:String=str_slice.to_owned();
斯特鲁布夫
};
x3id
}
fn main(){
让x3id:String=get_x3id();
println!(“{:?}”,x3id);
复制到剪贴板(x3id)
}
它工作得很好,我使用cargo构建它,自动生成一个可执行文件。我需要把dll文件和exe在同一个目录时运行它


我想知道是否有任何方法可以通过cargo将dll文件打包到exe中?

您可以使用宏,然后根据数据创建dll文件。@red75prime的副本谢谢,我搜索了很多演示,他们使用
包含字节
加载这样的
主机
文件,将
静态[u8]
数据转换为
静态str
并输出文本,我注意到
库::new()
需要
AsRef
参数,但我不知道如何将
静态[u8]
转换为
AsRef
。我尝试使用
str::from_utf8
静态[u8]
转换为
静态str
,并将
静态str
转换为
OsStr
,但当
和[u8]
转换为
静态str
(线程“main”在“无效UTF-8序列:索引2中1字节的无效UTF-8序列”处惊慌失措)我想可能DLL文件无法转换为utf-8,那么当使用
include\u字节加载时,我应该如何使用DLL可以使用宏,然后从数据创建dll文件。@red75prime的副本谢谢,我搜索了很多演示,他们使用
包含字节
加载这样的
主机
文件,将
静态[u8]
数据转换为
静态str
并输出文本,我注意到
库::new()
需要
AsRef
参数,但我不知道如何将
静态[u8]
转换为
AsRef
。我尝试使用
str::from_utf8
静态[u8]
转换为
静态str
,并将
静态str
转换为
OsStr
,但当
和[u8]
转换为
静态str
(线程“main”在“无效UTF-8序列:索引2中1字节的无效UTF-8序列”处惊慌失措)我想可能DLL文件无法转换为utf-8,那么当使用
include\u字节加载时,我应该如何使用DLL