python文件参数错误_Python ctypes参数错误

I wrote a test dll in C++ to make sure things work before I start using a more important dll that I need. Basically it takes two doubles and adds them, then returns the result. I've been playing around and with other test functions I've gotten returns to work, I just can't pass an argument due to errors.

My code is:

import ctypes

import string

nDLL = ctypes.WinDLL('test.dll')

func = nDLL['haloshg_add']

func.restype = ctypes.c_double

func.argtypes = (ctypes.c_double,ctypes.c_double)

print(func(5.0,5.0))

It returns the error for the line that called "func":

ValueError: Procedure probably called with too many arguments (8 bytes in excess)

What am I doing wrong? Thanks.

解决方案

You probably got the calling conventions mixed up. I'm guessing you have a C function declared something like this:

double haloshg_add(double d1, double s2)

{

return d1+d2;

}

This will use the C calling convention by default. The simplest approach would be to change the calling convention in your ctypes code:

nDLL = ctypes.CDLL('test.dll')

If you wanted to change the calling convention in the C code to stdcall (to match ctypes.WinDLL) then you would do this:

double __stdcall haloshg_add(double d1, double s2)

Whatever you do, only do one of these changes. If you do both you'll have the reverse failure!

If it were me, I'd just change the Python code to use C calling convention (use CDLL). That change has the least impact.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值