mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.43k stars 314 forks source link

[Feature Request] Please add a switch to indicate whether to generate function pointer declarations. #1016

Open ssrlive opened 1 month ago

ssrlive commented 1 month ago

Please add a switch to indicate whether to generate function pointer declarations.

If you think it is optional, you can add a switch to indicate whether to generate it.

Now, only function declarations of the form

int foo(int a, int b);

can be generated from the cbindgen tool, but no function pointer declarations of the form

int (*pfn_foo)(int a, int b);

can be generated.

This makes it very inconvenient when we dynamically load the dll library generated by rust.

I hope you will consider this request.

For example,

#include <windows.h>
#include <stdio.h>

// define a function pointer type by ourselves
typedef int (*pfn_foo)(int, int);

int main() {
    HMODULE hModule = LoadLibrary("a.dll");
    if (hModule == NULL) {
        return 1;
    }

    pfn_foo foo = (pfn_foo)GetProcAddress(hModule, "foo");
    if (foo == NULL) {
        FreeLibrary(hModule);
        return 1;
    }

    int result = foo(5, 10);
    printf("result: %d\n", result);

    FreeLibrary(hModule);
    return 0;
}