python调用dll文件时传入参数是int*,返回值是char*,对传入的char*所指的内容的修改
2014-09-25 10:17
459 查看
首先载入dll:test = ctypes.windll.LoadLibrary('dlltest.dll')
若调用的dll中的函数的参数是int*:
a = ctypes.c_int(5)
#add是dll中的函数。int add(int* x, int y)
test.add(ctypes.byref(a),4)
############################################################
若返回值是char*:
C Code:
char* f(char * str)
{
return str;
}
--------------------------------------------------------------------------------
Python Code:
loadlibrary。。。
ff = dll.f
ff.argtypes = [ctypes.c_char_p]
ff.restype = ctypes.c_char_p #no []!!!!! and restype not restypes
input = "hello world"
output = ff(input)
print output
#################################################################
C Code:
char* t(char* ss)
{
int i = strlen(ss);
ss[1]='1';
ss[i]='G';
ss[i+1]='\0';
return ss;
}
//在这里只会改变ss所指的内容,但不会改变ss所指内容的长度!!!
---------------------------------------------------------------
Python Code:
loadlibrary。。。
t = dlltest.t
t.argtypes = [ctypes.c_char_p]
t.restype = ctypes.c_char_p
arg = "jjjjjjjjjjjjj"
print "input ",arg
#output = ctypes.c_char_p
output = t(arg)
print "output ",type(output)," ",output
#若是输出arg,后面的'G'不会显示,但前面修改成的'1'会表现。
#######################################################
对传入的char*所指的内容进行修改:
C Code:
char* t(char* ss)
{
char d[] = "12345678";
strcat(ss,d);
return ss;
}
--------------------------------------------------------------------------------------
Python Code:
dlltest = ctypes.windll.LoadLibrary('mydll.dll')
t = dlltest.t
t.argtypes = [ctypes.c_char_p]
t.restype = ctypes.c_char_p
p = ctypes.create_string_buffer(10) #!!!!!!!!
s = t(p)
#print type(s),len(s),type(p.value),len(p.value)
print "s:",s," / p.value:",p.value
#print p
此时,s和p.value都被修改了,并且其长度都是8。(实现了获取参数中被修改的char*值)
若调用的dll中的函数的参数是int*:
a = ctypes.c_int(5)
#add是dll中的函数。int add(int* x, int y)
test.add(ctypes.byref(a),4)
############################################################
若返回值是char*:
C Code:
char* f(char * str)
{
return str;
}
--------------------------------------------------------------------------------
Python Code:
loadlibrary。。。
ff = dll.f
ff.argtypes = [ctypes.c_char_p]
ff.restype = ctypes.c_char_p #no []!!!!! and restype not restypes
input = "hello world"
output = ff(input)
print output
#################################################################
C Code:
char* t(char* ss)
{
int i = strlen(ss);
ss[1]='1';
ss[i]='G';
ss[i+1]='\0';
return ss;
}
//在这里只会改变ss所指的内容,但不会改变ss所指内容的长度!!!
---------------------------------------------------------------
Python Code:
loadlibrary。。。
t = dlltest.t
t.argtypes = [ctypes.c_char_p]
t.restype = ctypes.c_char_p
arg = "jjjjjjjjjjjjj"
print "input ",arg
#output = ctypes.c_char_p
output = t(arg)
print "output ",type(output)," ",output
#若是输出arg,后面的'G'不会显示,但前面修改成的'1'会表现。
#######################################################
对传入的char*所指的内容进行修改:
C Code:
char* t(char* ss)
{
char d[] = "12345678";
strcat(ss,d);
return ss;
}
--------------------------------------------------------------------------------------
Python Code:
dlltest = ctypes.windll.LoadLibrary('mydll.dll')
t = dlltest.t
t.argtypes = [ctypes.c_char_p]
t.restype = ctypes.c_char_p
p = ctypes.create_string_buffer(10) #!!!!!!!!
s = t(p)
#print type(s),len(s),type(p.value),len(p.value)
print "s:",s," / p.value:",p.value
#print p
此时,s和p.value都被修改了,并且其长度都是8。(实现了获取参数中被修改的char*值)
相关文章推荐
- 生成dll文件以及python对DLL中函数的调用(参数类型以及返回值)
- java调用Python脚本文件的同时,并向其中传入参数
- python 调用C++,传递int,char,char*,数组和多维数组
- Python中将函数作为另一个函数的参数传入并调用
- Windows下QT中用C++调用Python之三 - 基础参数的传入和传出
- Python学习笔记(10)-函数-函数定义、调用、参数、返回值、嵌
- python *args **kwargs,传入不固定的参数给函数,或者传入很多的内容给函数,常用在构造函数中。
- 一道填空题:移动字符串内容,,传入参数char *a和m
- liunx python调用c++库(类、函数),传入传出参数
- Python学习笔记(10)-函数-函数定义、调用、参数、返回值、嵌
- C++调用Python函数并传入传出参数
- C++ 调用Python文件方法传递字典参数并接收返回值
- python调用dll动态库传入结构体作为参数
- C语言学习4: 函数返回值与传入参数,关于函数值传递和类型隐性转换,变量不同的作用域,static变量,多文件编译例如两个C文件,显示函数调用语句跳转,递归,斐波那契数列,多文件编译相同变量的问题。
- 在函数中不修改函数传入参数的情况下取到调用程序中的值的方法
- 通过双击ALV中的字段名判断调用的事物代码和传入的参数
- 能否不重载CRecordSet类,用MFC ODBC调用带返回值和参数的存储过程
- ASP.NET中调用存储过程带(输入/输出)参数和返回值
- 双击ALV调用事务代码并传入参数
- 关于sqlhelper调用存储过程和获取参数返回值