Rust 有没有办法将DLL文件放入exe程序?
我写了一个工具,它可以通过dll获取设备id,并将id复制到剪贴板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
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代码>