如何调用返回多种不同函数指针的C函数?

时间:2017-08-04 20:17:52

标签: rust ffi

我正在尝试访问OpenGL函数glCreateVertexArrays。考虑以下小型C ++程序:

#include <GL/glx.h>
#include <GLFW/glfw3.h>

int main() {
    // Init GLFW
    glfwInit();

    // Set OpenGL Version
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    // Select core profile
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);

    // Create Window
    auto window = glfwCreateWindow(400, 400, "Test", nullptr, nullptr);
    glfwMakeContextCurrent(window);

    void (*glCreateVertexArrays)(unsigned int, unsigned int*) = nullptr;
    glCreateVertexArrays = (decltype(glCreateVertexArrays))glXGetProcAddress((const       GLubyte*)"glCreateVertexArrays");

    unsigned int vao;
    glCreateVertexArrays(1, &vao); // <-- This is the important part

    return 0;
}

此应用程序不会出现段错误;现在我想在Rust中重新创建相同的内容:

extern crate libc;

use std::ffi::CString;
use std::os;
use std::ptr;

enum GLFWwindow {}
enum GLFWmonitor {}
#[link(name = "glfw")]
extern "C" {
    fn glfwInit() -> bool;
    fn glfwWindowHint(target: libc::uint32_t, hint: libc::int32_t);
    fn glfwCreateWindow(
        width: libc::c_int,
        height: libc::c_int,
        title: *const os::raw::c_char,
        monitor: *mut GLFWmonitor,
        window: *mut GLFWwindow,
    ) -> *mut GLFWwindow;
    fn glfwMakeContextCurrent(window: *mut GLFWwindow);
}

#[link(name = "GL")]
extern "C" {
    fn glXGetProcAddress(procname: *const os::raw::c_char) -> extern "C" fn();
}

static GLFW_CONTEXT_VERSION_MAJOR: u32 = 0x22002;
static GLFW_CONTEXT_VERSION_MINOR: u32 = 0x22003;
static GLFW_OPENGL_FORWARD_COMPAT: u32 = 0x22006;
static GLFW_OPENGL_PROFILE: u32 = 0x22008;
static GLFW_OPENGL_CORE_PROFILE: i32 = 0x00032001;

type FnCreateVertexArrays = extern "C" fn(n: libc::c_int, arrays: *mut libc::c_uint);

fn main() {
    let w_name = CString::new("Test").unwrap();
    let fn_name = CString::new("glCreateVertexArrays").unwrap();

    unsafe {
        glfwInit();

        // Set OpenGL Version
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

        // Select core profile
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);

        // Create Window
        let window = glfwCreateWindow(
            400,
            400,
            w_name.as_ptr(),
            ptr::null_mut(),
            ptr::null_mut(),
        );
        glfwMakeContextCurrent(window);
    }

    let create_vertex_arrays: FnCreateVertexArrays = unsafe {
        glXGetProcAddress(fn_name.as_ptr())
    };

    let mut vao: libc::c_uint = 0;
    unsafe {
        (create_vertex_arrays)(1, &mut vao);
    }

    println!("Hello, world!");
}

然而,演员阵容不正确:

error[E0308]: mismatched types
  --> src/main.rs:63:9
   |
63 |         glXGetProcAddress(fn_name.as_ptr())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
   |
   = note: expected type `extern "C" fn(i32, *mut u32)`
              found type `extern "C" fn()`

这里的主要问题是OpenGL函数glXGetProcAddress返回一个void*()指针,该指针必须转换为适当的函数指针类型。怎么可能这样做?

我知道OpenGL / GLFW已有包装器和引擎,但我想为了学习而这样做。

1 个答案:

答案 0 :(得分:1)

这是一个稍微小一点的复制品:

extern crate libc;

extern "C" {
    fn getProcAddress(procname: libc::c_int) -> extern "C" fn();
}

type CreateVertexArrays = extern "C" fn(n: libc::c_int, arrays: *mut libc::c_uint);

fn main() {
    let create_vertex_arrays: CreateVertexArrays = unsafe {
        getProcAddress(42)
    };

    let mut vao = 0;
    (create_vertex_arrays)(1, &mut vao);
}

一个解决方案是 The Big Hammer “将此类型变为该类型”:mem::transmute

fn main() {
    let create_vertex_arrays: CreateVertexArrays = unsafe {
        ::std::mem::transmute(getProcAddress(42))
    };

    let mut vao = 0;
    (create_vertex_arrays)(1, &mut vao);
}

mem::transmute确实应该被用作最后的手段。我所知道的其他转换技术似乎都不适用于此......

另见: