python dll 指针_python 调用C函数的dll文件传递指针变量时错误

非IT行业的菜鸟,最近用到Python与C联合编程。C函数采用数组变量函数可以实现调用,但改为指针后就出错,

哪位大侠来解答一下。

C 代码:test.c

#include

void show_matrix(int *matrix, int rows)

{

int i;

for (i=0; i

printf("matrix[%d] = %d\n", i, *(matrix+i));

}

}

编译dll。 gcc -o test.dll -shared test.c

python 代码

import numpy as np

from ctypes import *

a = np.asarray(range(16), dtype=np.int32).reshape([4,4])

lib=CDLL('e:\\mysoft\\ctest\\ctypes1\\test.dll')

if not a.flags['C_CONTIGUOUS']:

a = np.ascontiguous(a, dtype=a.dtype) # 如果不是C连续的内存,必须强制转换

aptr=cast(a.ctypes.data, POINTER(c_int))

a1=c_int(8)

lib.show_matrix.argtypes =[POINTER(c_int),c_int]

lib.show_matrix.restype =None

lib.show_matrix(aptr,a1)

编译错误:

Traceback (most recent call last):

File "", line 1, in

File "E:\instal_soft\PyCharm 2018.3.4\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile

pydev_imports.execfile(filename, global_vars, local_vars) # execute the script

File "E:\instal_soft\PyCharm 2018.3.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile

exec(compile(contents+"\n", file, 'exec'), glob, loc)

File "E:/mysoft/ctest/ctypes1/cintp.py", line 6, in

lib=CDLL('e:\\mysoft\\ctest\\ctypes1\\test.dll')

File "E:\instal_soft\python35\lib\ctypes\__init__.py", line 347, in __init__

self._handle = _dlopen(self._name, mode)

OSError: [WinError 193] %1 不是有效的 Win32 应用程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值