gpt4 book ai didi

winapi - 调用 GetBinaryTypeA 时出现段错误

转载 作者:行者123 更新时间:2023-11-29 08:27:15 27 4
gpt4 key购买 nike

我尝试导入 GetBinaryTypeA功能:

use std::ffi::CString;

use ::std::os::raw::{c_char, c_ulong};
extern { fn GetBinaryTypeA(s: *const c_char, out: *mut c_ulong) -> i32; }

fn main() {
let path = "absolute/path/to/bin.exe";

let cpath = CString::new(path).unwrap();
let mut out: c_ulong = 0;

println!("{:?}", cpath);
unsafe { GetBinaryTypeA(cpath.as_ptr(), out as *mut c_ulong); }
println!("{:?}", cpath);
}

输出:

error: process didn't exit successfully: `target\debug\bin_deploy.exe` (exit code: 3221225477)
Process finished with exit code -1073741819 (0xC0000005)

如果我设置了一个无效的路径,那么它会成功执行并且 GetLastError() 返回 2(“系统找不到指定的文件”),所以看起来导入的函数有效。

我在使用 kernel32-sys crate 时收到了同样的错误。错误还可能出在哪里?

最佳答案

您正在将值 0 转换为指针。在当今使用的绝大多数计算机上,值为 0 的指针被称为 NULL。因此,您正在尝试写入 NULL 指针,这会导致崩溃。

您想写入值的地址:

&mut out as *mut c_ulong

甚至不需要强制转换:

unsafe {
GetBinaryTypeA(cpath.as_ptr(), &mut out);
}

关于winapi - 调用 GetBinaryTypeA 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424754/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com