在 python 中定义一个结构体以使用 C 库 (dll) 中的函数

发布于 2024-10-17 04:02:34 字数 1391 浏览 2 评论 0原文

首先,对不起我的英语。

我在 Python 脚本中使用动态库(gnu gsl 库)中的函数时遇到一些麻烦。

事实上,我知道使用一个函数,但我需要定义一个结构白色 C 类型数据来使用特定的函数。

首先,这是在 C++ 脚本中使用该函数的示例,我将在 Python 脚本中使用该函数:

    double fn1 (double x, void * params)
{
    return cos(x) + 1.0;
}
int
main (void)
{
       gsl_min_fminimizer *s;
       double m = 2.0, m_expected = M_PI;
       double a = 0.0, b = 6.0;
       gsl_function F;

       F.function = &fn1;
       F.params = 0;

       T = gsl_min_fminimizer_brent;
       s = gsl_min_fminimizer_alloc (T);
       gsl_min_fminimizer_set (s, &F, m, a, b);

       ...
}

标头中 gsl_function 的 typedef 是:

struct gsl_function_struct 
{
  double (* function) (double x, void * params);
  void * params;
};

typedef struct gsl_function_struct gsl_function ;

#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)

我知道使用带有 ctypes 的函数:

#Library Call

lib_gsl=CDLL(path_gsl+"gsl.dll")

#Functions Define

gsl_opti_brent=lib_gsl.gsl_min_fminimizer_brent
gsl_opti_alloc=lib_gsl.gsl_min_fminimizer_alloc
gsl_opti_init=lib_gsl.gsl_min_fminimizer_set

T=gsl_opti_brent
T_=pointer(T)
s=gsl_opti_alloc(T_)

在此之前,没有问题,但是当我想使用函数 gsl_min_fminimizer_set,我需要在名为 gsl_function 的标头中定义一个特定类型。

有人知道如何在Python中定义typedef gsl_function来使用名为函数gsl_min_fminimizer_set的dll?

谢谢。 我希望我是明确的

For beginning, sorry for my english.

I have some trouble to use a function from a dynamic library (the gnu gsl library) in a Python script.

Indeed, I know use a function but I need to define a Structure white C type datas to use a particulary function.

In first this is an example of the use of the function in a C++ script that I would use in Python script:

    double fn1 (double x, void * params)
{
    return cos(x) + 1.0;
}
int
main (void)
{
       gsl_min_fminimizer *s;
       double m = 2.0, m_expected = M_PI;
       double a = 0.0, b = 6.0;
       gsl_function F;

       F.function = &fn1;
       F.params = 0;

       T = gsl_min_fminimizer_brent;
       s = gsl_min_fminimizer_alloc (T);
       gsl_min_fminimizer_set (s, &F, m, a, b);

       ...
}

The typedef of gsl_function in the header is:

struct gsl_function_struct 
{
  double (* function) (double x, void * params);
  void * params;
};

typedef struct gsl_function_struct gsl_function ;

#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)

I know use a function with ctypes:

#Library Call

lib_gsl=CDLL(path_gsl+"gsl.dll")

#Functions Define

gsl_opti_brent=lib_gsl.gsl_min_fminimizer_brent
gsl_opti_alloc=lib_gsl.gsl_min_fminimizer_alloc
gsl_opti_init=lib_gsl.gsl_min_fminimizer_set

T=gsl_opti_brent
T_=pointer(T)
s=gsl_opti_alloc(T_)

Until this, there is no trouble but when I want use the function gsl_min_fminimizer_set, I need a particulary type define in a header called gsl_function.

There is somebody knows how can I define the typedef gsl_function in Python to use the dll called function gsl_min_fminimizer_set?

Thank you.
I expect I am explicit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-10-24 04:02:34

您应该查看 ctypes 教程(特别是结构和回调函数)。

它应该是这样的:

MY_FUNC_TYPE = CFUNCTYPE(c_double, c_double, c_void_p)
class GSL_FUNCTION(Structure):
    _fields_ = [("function", MY_FUNC_TYPE),
                ("params", c_void_p)]

def py_fn1(x, params):
    return cos(x) + 1.0

fn1 = MY_FUNC_TYPE(py_fn1)

F = GSL_FUNCTION(fn1, 0)

You should take a look at this ctypes tutorial (specially Structures and Callback functions).

It should be something like this:

MY_FUNC_TYPE = CFUNCTYPE(c_double, c_double, c_void_p)
class GSL_FUNCTION(Structure):
    _fields_ = [("function", MY_FUNC_TYPE),
                ("params", c_void_p)]

def py_fn1(x, params):
    return cos(x) + 1.0

fn1 = MY_FUNC_TYPE(py_fn1)

F = GSL_FUNCTION(fn1, 0)
尘曦 2024-10-24 04:02:34

我认为您正在寻找ctypes.Structure。这是在 python 中创建结构体的一种方法;

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = [("x", c_int),
...                 ("y", c_int)]
...
>>> point = POINT(10, 20)
>>> print point.x, point.y
10 20

有关更多示例,您可以查看 ctypes 文档。

I think you are looking for ctypes.Structure. Here is a way to create structs in python;

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = [("x", c_int),
...                 ("y", c_int)]
...
>>> point = POINT(10, 20)
>>> print point.x, point.y
10 20

For more examples you can check out ctypes documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文