- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有以下签名的 c 函数
__declspec( dllexport ) void* setup(int c_force, int c_stepping, int c_iteration, int c_roots,
struct para* c_userdata, double* c_y0,
double c_reltol, double c_abstol)
{
....
return "a pointer";
}
其中 para 定义如下。我想用这个函数(和其他函数)构建一个 dll 并从 Python 访问它。重要的 Python 行是
import ctypes as ct
lib = ct.cdll.LoadLibrary('lib_path.dll')
getattr(lib, 'setup')
lib.setup.restype = ct.c_void_p
# pystruct as defined below to avoid clogging code
lib.setup.argtypes = [ct.c_int, ct.c_int, ct.c_int, ct.c_int,
ct.POINTER(para), ct.POINTER(ct.c_double),
ct.c_double, ct.c_double]
ptr = lib.setup(cf, cs, ci, cr, ct.byref(para), c_y0, c_reltol, c_abstol)
myobj = ct.c_void_p(ptr)
其中 cf
、cs
、ci
、cr
是 (python) 整数,para
是如下定义的结构类型,c_y0=(ct_c_double * 2)()
(长度应为 2,无论是从 python 端还是在 c 端)和 c_reltol
和 c_abstol
被转换为 ct.c_double,如 c_reltol = ct.c_double(reltol)
。
当我尝试运行我的主应用程序时,我在 lib.setup
函数调用中收到 WindowsError: 异常:访问冲突写入 xxx
,我不明白为什么。 . 在调用 lib.setup
之前打印,给出传递给函数的参数值和类型的以下输出(按顺序)
1 2 2 1 <cparam 'P' (0000000003C84EB0)> <cvode_library.c_double_Array_2 object at 0x0000000003F95BC8> c_double(1e-06) c_double(1e-08)
<type 'int'> <type 'int'> <type 'int'> <type 'int'> <type 'CArgObject'> <class 'cvode_library.c_double_Array_2'> <class 'ctypes.c_double'> <class 'ctypes.c_double'>
我一直在尝试使用 this 进行调试和 this问题,但没有成功。由于 c 函数的调用签名相当简单,我不明白为什么它会中断。
附注它在 Ubuntu 上完美运行,将 __declspec...
替换为 extern
c-struct 定义为
typedef struct para PARA;
struct para
{
double a;
double b;
double c;
};
相应的pystruct为
class para(ct.Structure):
_fields_ = [('a', ct.c_double),
('b', ct.c_double),
('c', ct.c_double)]
编辑 c_y0
定义为
y0 = np.array([0., 0.])
c_y0 = (ct.c_double * 2)()
c_y0[0] = y0[0]
c_y0[1] = y0[1]
所有“cvode”函数和 N_Vector
都是 Sundials suite for solving nonlinear equations 的一部分
__declspec( dllexport ) void* setup(int c_force, int c_stepping, int c_iteration, int c_roots,
struct para* c_userdata, double* c_y0,
double c_reltol, double c_abstol)
{
int flag;
N_Vector y;
void* cvode_mem;
PARA* ptr_para;
ptr_para = c_userdata;
// ****** Set up vector with initial conditions ******
y = N_VNew_Serial(2);
NV_Ith_S(y,0) = c_y0[0];
NV_Ith_S(y,1) = c_y0[1];
// ****** Create cvode object with stepping and iteration method ******
if(c_iteration==CV_FUNCTIONAL)
cvode_mem = CVodeCreate(c_stepping, 1); // Functional iteration
else
cvode_mem = CVodeCreate(c_stepping, 2); // Newton interation
if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(NULL);
flag = CVodeInit(cvode_mem, ode, 0, y);
if(check_flag(&flag, "CVodeInit", 1)) return(NULL);
// ****** Specify integration tolerances ******
flag = CVodeSStolerances(cvode_mem, c_reltol, c_abstol);
if(check_flag(&flag, "CVodeSStolerances", 1)) return(NULL);
// ****** Set up linear solver module if required ******
if(c_iteration==CV_DENSE_USER)
{
printf("Dense user supplied Jacobian\n");
// Dense user-supplied Jacobian
flag = CVDense(cvode_mem, 2);
if(check_flag(&flag, "CVDense", 1)) return(NULL);
flag = CVDlsSetDenseJacFn(cvode_mem, jac);
if(check_flag(&flag, "CVDlsSetDenseJacFn", 1)) return(NULL);
}
else if(c_iteration==CV_DENSE_DQ)
{
// Dense difference quotient Jacobian
flag = CVDlsSetDenseJacFn(cvode_mem, NULL);
if(check_flag(&flag, "CVDlsSetDenseJacFn", 1)) return(NULL);
}
// Set optional inputs
flag = CVodeSetUserData(cvode_mem, c_userdata);
if(check_flag(&flag, "CVodeSetUserData", 1)) return(NULL);
// Attach linear solver module
// Specify rootfinding problem
if(c_roots!=ROOTS_OFF)
{
flag = CVodeRootInit(cvode_mem, 1, root_func);
}
return cvode_mem;
}
最佳答案
这是一个 MCVE。这表明您的声明是正确的,因此问题可能出在函数实现中。如果以下内容对您不起作用,请使用重现您的失败的类似 MCVE 更新您的问题。
测试.c
#include <stdio.h>
typedef struct para PARA;
struct para
{
double a;
double b;
double c;
};
__declspec(dllexport) void* setup(int c_force, int c_stepping, int c_iteration, int c_roots,
struct para* c_userdata, double* c_y0,
double c_reltol, double c_abstol)
{
printf("%d %d %d %d %lf %lf %lf %lf %lf %lf %lf\n",c_force,c_stepping,c_iteration,c_roots,c_userdata->a,c_userdata->b,c_userda
ta->c,c_y0[0],c_y0[1],c_reltol,c_abstol);
return NULL;
}
测试.py
import ctypes as ct
class para(ct.Structure):
_fields_ = [('a', ct.c_double),
('b', ct.c_double),
('c', ct.c_double)]
lib = ct.CDLL('test')
lib.setup.restype = ct.c_void_p
# pystruct as defined below to avoid clogging code
lib.setup.argtypes = [ct.c_int, ct.c_int, ct.c_int, ct.c_int,
ct.POINTER(para), ct.POINTER(ct.c_double),
ct.c_double, ct.c_double]
p = para(1.5,2.5,3.5)
c_y0 = (ct.c_double * 2)(4.5,5.5)
ptr = lib.setup(1,2,3,4,p, c_y0,6.5,7.5)
myobj = ct.c_void_p(ptr)
输出
1 2 3 4 1.500000 2.500000 3.500000 4.500000 5.500000 6.500000 7.500000
关于python - 用 ctypes 包装 Python 中的 c 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45224808/
正在尝试创建一个 python 包。似乎有效,但我收到警告。我的 setup.py 是: #! /usr/bin/env python from distutils.core import setup
我导入了一个数据类型 X ,定义为 data X a = X a 在本地,我定义了一个通用量化的数据类型,Y type Y = forall a. X a 现在我需要定义两个函数, toY 和 fro
我似乎无法让编译器让我包装 Tokio AsyncRead: use std::io::Result; use core::pin::Pin; use core::task::{Context, Po
我有两个函数“a”和“b”。当用户上传文件时,“b”被调用。 “b”重命名文件并返回新文件名。之后应该编辑该文件。像这样: def a(): edits file def b(): r
我使用 Entity Framework 作为我的 ORM,我的每个类都实现了一个接口(interface),该接口(interface)基本上表示表结构(每个字段一个只读属性)。这些接口(inter
有没有办法打开一个程序,通常会打开一个新的jframe,进入一个现有的jframe? 这里是解释,我下载了一个java游戏,其中一个是反射游戏,它在一个jframe中打开,框架内有一堆子面板,我想要做
我想要下面的布局 | AA BBBBBBB | 除非没有足够的空间,在这种情况下 | AA | | BBBBBBB | 在这种情况下,A 是复选框,B 是复选框旁边的 Text
我正在尝试以不同的方式包装我的网站,以便将背景分为 2 部分。灰色部分是主要背景,还有白色部分,它较小并包装主要内容。 基本上我想要this看起来像this . 我不太确定如何添加图像来创建阴影效果,
我正在使用 : 读取整数文件 int len = (int)(new File(file).length()); FileInputStream fis = new FileInputStream(f
我使用 maven 和 OpenJDK 1.8 打包了一个 JavaFX 应用程序我的 pom.xml 中的相关部分: maven-assembly-plugin
我正在使用两个不同的 ItemsControl 来生成一个按钮列表。
我有一个情况,有一个变量会很方便,to , 可以是 TimerOutput或 nothing .我有兴趣提供一个采用与 @timeit 相同参数的宏来自 TimerOutputs(例如 @timeit
我正在尝试包装一个名为 content 的 div与另一个具有不同背景的 div。 但是,当将“margin-top”与 content 一起使用时div,似乎包装 DIV 获得了边距顶部而不是 co
文档不清楚,它似乎允许包装 dll 和 csproj 以在 Asp.Net Core 5 应用程序中使用。它是否允许您在 .Net Core 5 网站中使用针对 .Net Framework 4.6
我被要求开发一个层,该层将充当通用总线,而不直接引用 NServiceBus。到目前为止,由于支持不引人注目的消息,这并不太难。除了现在,我被要求为 IHandleMessages 提供我们自己的定义
我正在尝试包装 getServersideProps使用身份验证处理程序函数,但不断收到此错误:TypeError: getServerSideProps is not a function我的包装看
我有一个项目,它在特定位置(不是/src/resources)包含资源(模板文件)。我希望在运行 package-bin 时将这些资源打包。 我看到了 package-options 和 packag
我正在寻找打印从一系列对象中绘制的 div。我可以通过使用下面的管道语法来实现这一点。 each i, key in faq if (key == 0) |
我在 Meteor.js“main.js - Server”中有这个方法。 Meteor.methods({ messageSent: function (message) { var a
我注意到,如果我的自定义Polymer 1.x元素的宽度比纸张输入元素上的验证错误消息的宽度窄,那么错误将超出自定义元素的右边界。参见下图: 有没有一种机制可以防止溢出,例如在到达自定义元素的边界时自
我是一名优秀的程序员,十分优秀!