首页 > 编程知识 正文

python动态调用链接库(python 动态链接库)

时间:2023-12-17 12:25:52 阅读:316441 作者:IFBX

本文目录一览:

python 是否有能列出动态链接库中有哪些方法的库

最近看了《Gray hat python》一书,这才知道为什么python是黑客必学的编程语言。通过python的ctypes模块,可以直接调用动态链接库中的导出函数,而且甚至可以直接在python中构建出复杂的C结构体!!!使得python也具备了底层内存操作的能力,再配合python本身强大的表达能力,能不让人激动么。

之前为了在python中调用动态链接库导出的函数,你需要自行解析出这些导出函数的地址。而现在ctypes库会替我们完成这个麻烦的过程,大大方便了我们直接在python中调用C函数的能力。

ctypes模块中有三种不同的动态链接库加载方式:cdll, windll, oledll。不同之处在于链接库中的函数所遵从的函数调用方式(calling convention)以及返回方式有所不同。

cdll用于加载遵循cdecl标准函数调用约定的链接库。windll则用于加载遵循stdcall调用约定的动态链接库。oledll与windll完全相同,只是会默认其载入的函数会统一返回一个Windows HRESULT错误编码。

先复习一下有关函数调用约定的知识:函数调用约定指的是函数参数入栈的顺序、哪些参数入栈、哪些通过寄存器传值、函数返回时栈帧的回收方式(是由调用者负责清理,还是被调用者清理)、函数名称的修饰方法等等。基本上我们最常见的调用约定就是cdecl和stdcall两种。在《程序员的自我修养--链接、装载与库》一书的第10章有对函数调用约定的更详细介绍。

cdecl规定函数参数列表以从右到左的方式入栈,且由函数的调用者负责清除栈帧上的参数。stdcall的参数入栈方式与cdecl一致,但函数返回时是由被调用者自己负责清理栈帧。而且stdcall是Win32 API函数所使用的调用约定。OK,就这么多,够了。

测试一下在Linux平台和Windows平台下通过ctypes模块导入C库中函数的小例子:

Windows 下:

from ctypes import *

msvcrt = cdll.msvcrt

msg = "Hello world!n"

msvcrt.printf("Testing: %s", msg)

Linux下:

from ctypes import *

libc = CDLL("libc.so.6")

msg = "Hello, world!n"

libc.printf("Testing: %s", msg)

可以看到动态链接库中的printf被直接导入到python中来调用了。

那么,在python中怎么表示C的类型?不用担心,下面这张表就能搞定。

有了这个映射关系,多复杂的C类型也能在python中表达出来。

在C中定义一个联合:

union

{

long barley_long;

int barley_int;

char barley_char[8];

}barley_amount;

而在python中同等的定义为:注意一下python中定义数组的方式。

class barley_amount(Union):

_fields_ = [

("barley_long", c_long),

("barley_int", c_int),

("barley_char", c_char * 8),

]

测试一下这个例子,在python中定义一个联合体,为其赋值,再分别访问其成员。

from ctypes import *

class barley_amount(Union):

_fields_ = [

("barley_long", c_long),

("barley_int", c_int),

("barley_char", c_char * 8),

]

value = raw_input("Enter the amount of barley to put into the beer vat:")

my_barley = barley_amount(int(value))

print "Barley amount as a long: %ld" % my_barley.barley_long

print "Barley amount as an int: %d" % my_barley.barley_int

print "Barley amount as a char: %s" % my_barley.barley_char

请教python调用dll动态库的传参问题

第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:

LIBEXPORT_API int mySum(int a,int b){ return a+b;}

C# 导入定义:

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]

public static extern int mySum (int a,int b);

}

在C#中调用测试:

int iSum = RefComm.mySum(,);

运行查看结果iSum为5,调用正确。第一步试验完成,说明在C#中能够调用自定义的动态链接库函数。

第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:

LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;}

C# 导入定义:

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Auto,

CallingConvention=CallingConvention.StdCall)]

public static extern string mySum (string a, string b);

}

在C#中调用测试:

string strDest="";

string strTmp= RefComm.mySum("45", strDest);

运行查看结果 strTmp 为"45",但是strDest为空。我修改动态链接库实现,返回结果为串b:

LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;}

修改 C# 导入定义,将串b修改为ref方式:

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]

public static extern string mySum (string a, ref string b);

}

在C#中再调用测试:

string strDest="";

string strTmp= RefComm.mySum("45", ref strDest);

运行查看结果 strTmp 和 strDest 均不对,含不可见字符。再修改 C# 导入定义,将CharSet从Auto修改为Ansi:

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]

public static extern string mySum (string a, string b);

}

在C#中再调用测试:

string strDest="";

string strTmp= RefComm. mySum("45", ref strDest);

运行查看结果 strTmp 为"45",但是串 strDest 没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。再次修改 C# 导入定义,将串b修改为引用(ref):

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]

public static extern string mySum (string a, ref string b);

}

运行时调用失败,不能继续执行。

第三步,修改动态链接库实现,将b修改为双重指针:

LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),"%s",a); return *b;}

C#导入定义:

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]

public static extern string mySum (string a, ref string b);

}

在C#中调用测试:

string strDest="";

string strTmp= RefComm. mySum("45", ref strDest);

运行查看结果 strTmp 和 strDest 均为"45",调用正确。第三步实现了函数出口参数正确输出结果。

第四步,修改动态链接库实现,实现整数参数的输出:

LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;}

C#导入的定义:

public class RefComm

{

[DllImport("LibEncrypt.dll",

EntryPoint=" mySum ",

CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]

public static extern int mySum (int a, int b,ref int c);

}

在C#中调用测试:

int c=0;

int iSum= RefComm. mySum(,, ref c);

运行查看结果iSum 和c均为5,调用正确。

经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。

三、结论

在 C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。

对于函数返回值,C# 导入定义和 C++ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 CharSet 和 CallingConvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 C# 程序的目录下即可,我这里是一个 C# 的动态链接库,两个动态链接库就在同一个目录下运行。

python怎么调用c的动态链接库

Python调用C/C++动态链接库的需求

在自动化测试过程中,难免会遇到语言混合使用的情况,这不,我们也遇到了。初步决定采用Robot Framework作为自动化测试框架后,其支持Java和Python,而Python作为主流的语言,怎么能放弃使用它的机会^_^。 然而产品采用是古老90年代开发的C/S结构,因为古老,当时也没有考虑到对产品的测试进行自动化,Client端并没有预留CLI(Command Line interface)形式的接口,真是雪上加霜啊。

那怎么自动化?采用AutoIT来对客户端界面进行自动化测试?可惜AutoIT对当初开发采用的控件识别不是很好,如果采用控件所在位置来进行控制的方式,又会导致自动化测试并不是很稳定。那么!!!只有自己开发接口了,目前在Client端开发出CLI形式的接口,将其封装为DLL,然后在Robot FrameWork框架中采用Python对DLL进行调用。任务艰巨哪!

Python调用DLL例子

示例一

首先,在创建一个DLL工程(本人是在VS 2005中创建),头文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.h

#ifdef EXPORT_HELLO_DLL

#define HELLO_API __declspec(dllexport)

#else

#define HELLO_API __declspec(dllimport)

#endif

extern "C"

{

HELLO_API int IntAdd(int , int);

}

CPP文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.cpp

#define EXPORT_HELLO_DLL

#include "hello.h"

HELLO_API int IntAdd(int a, int b)

{

return a + b;

}

这里有两个注意点:

(1)弄清楚编译的时候函数的调用约定采用的__cdecl还是__stdcall,因为根据DLL中函数调用约定方式,Python将使用相应的函数加载DLL。

(2)如果采用C++的工程,那么导出的接口需要extern "C",这样python中才能识别导出的函数。

我的工程中采用__cdecl函数调用约定方式进行编译链接产生hello.dll,然后Python中采用ctypes库对hello.dll进行加载和函数调用:

[python] view plain copy 在CODE上查看代码片派生到我的代码片from ctypes import *

dll = cdll.LoadLibrary('hello.dll');

ret = dll.IntAdd(2, 4);

print ret;

OK,一个小例子已经完成了,如果你感兴趣,但还没试过,那就尝试一下吧。

示例二

示例一只是一个"hello world"级别的程序,实际运用中更多的需要传递数据结构、字符串等,才能满足我们的需求。那么这个示例将展示,如何传递数据结构参数,以及如何通过数据结构获取返回值。

首先编写DLL工程中的头文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.h

#ifdef EXPORT_HELLO_DLL

#define HELLO_API __declspec(dllexport)

#else

#define HELLO_API __declspec(dllimport)

#endif

#define ARRAY_NUMBER 20

#define STR_LEN 20

struct StructTest

{

int number;

char* pChar;

char str[STR_LEN];

int iArray[ARRAY_NUMBER];

};

extern "C"

{

//HELLO_API int IntAdd(int , int);

HELLO_API char* GetStructInfo(struct StructTest* pStruct);}

CPP文件如下:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.cpp

#include string.h

#define EXPORT_HELLO_DLL

#include "hello.h"

HELLO_API char* GetStructInfo(struct StructTest* pStruct){

for (int i = 0; i ARRAY_NUMBER; i++)

pStruct-iArray[i] = i;

pStruct-pChar = "hello python!";

strcpy (pStruct-str, "hello world!");

pStruct-number = 100;

return "just OK";

}

GetStructInfo这个函数通过传递一个StructTest类型的指针,然后对对象中的属性进行赋值,最后返回"just OK".

编写Python调用代码如下,首先在Python中继承Structure构造一个和C DLL中一致的数据结构StructTest,然后设置函数GetStructInfo的参数类型和返回值类型,最后创建一个StructTest对象,并将其转化为指针作为参数,调用函数GetStrcutInfo,最后通过输出数据结构的值来检查是否调用成功:

[python] view plain copy 在CODE上查看代码片派生到我的代码片from ctypes import *

ARRAY_NUMBER = 20;

STR_LEN = 20;

#define type

INTARRAY20 = c_int * ARRAY_NUMBER;

CHARARRAY20 = c_char * STR_LEN;

#define struct

class StructTest(Structure):

_fields_ = [

("number", c_int),

("pChar", c_char_p),

("str", CHARARRAY20),

("iArray", INTARRAY20)

]

#load dll and get the function object

dll = cdll.LoadLibrary('hello.dll');

GetStructInfo = dll.GetStructInfo;

#set the return type

GetStructInfo.restype = c_char_p;

#set the argtypes

GetStructInfo.argtypes = [POINTER(StructTest)];objectStruct = StructTest();

#invoke api GetStructInfo

retStr = GetStructInfo(byref(objectStruct));#check result

print "number: ", objectStruct.number;

print "pChar: ", objectStruct.pChar;

print "str: ", objectStruct.str;

for i,val in enumerate(objectStruct.iArray):

print 'Array[i]: ', val;

print retStr;

总结

1. 用64位的Python去加载32位的DLL会出错

2. 以上只是些测试程序,在编写Python过程中尽可能的使用"try Except"来处理异常3. 注意在Python与C DLL交互的时候字节对齐问题4. ctypes库的功能还有待继续探索

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。