python ctypes传数组

3 篇文章 0 订阅

存在C++ 编写的DLL,主要代码如下

// list 包含 string
// list float
float maxlist(char ** modul, float * images, int num) {
	const char * split = ",";    // 用来进行切割
	char * p;
	float degree = 0;

	for ( int i = 0; i < num; i++ ){
		int j = 0;
		float degreetmp = 0;
		// split  用strok函数需要用,否则会告诉你不安全,要你用strok_s替代#pragma warning (disable : 4996)
		p = strtok( modul[i] , split );
		float ptmp = std::stof(p);    // this is much better way to do it
		while( p != NULL ){
			// 进行比较
			if (images[j] != ptmp){
				degreetmp = degreetmp + 10086 * float(ptmp + images[j]);
			}else{
				degreetmp += 1;
			}
			// 比较完继续遍历
			j++;
			p = strtok( NULL , split);
			if ( p != NULL)
				ptmp = std::stof(p);		
		}
		degreetmp = float(degreetmp/ j);
		if (degreetmp > degree){
			degree = degreetmp;
		}
	}
	return degree;
}

 

对应的python调用方法如下

from ctypes import *
 
module = ['2.0, 2.0, 2.0, 2.0, 2.00', '2.0, 5.0, 2.4, 2.1, 2.0', '3.0, 1.0, 2.0, 2.5, 2.00']
hist = [2, 2, 2, 2, 2]
# print len(module)
test = str(module)[1:-1]
test_1 = str(hist)[1:-1]
list_test = []

# 这里简单粗暴一下,复制了三次。。。
list_test.append(test.replace(' ', ''))
list_test.append(test.replace(' ', ''))
list_test.append(test.replace(' ', ''))

test_2 = (c_char_p*3)()
for i in xrange(0, 3):
    # 这里给的是地址
    test_2[i] = list_test[i]
 
test2 = (c_float*5)()
for i in xrange(0, 5):
    # 这里给的是数字
    test2[i] = hist[i]
 
test_ = cdll.LoadLibrary("dll.dll")
 
test_.maxlist.restype = c_float
results1 = test_.maxlist(test_2, test2, 3)
print "result1:", results1

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AgtNN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值