导航:首页 > 编程语言 > python能调用dll吗

python能调用dll吗

发布时间:2022-08-17 12:57:32

1. 如何用python调用应用程序的.dll文件

python引用DLL文件的方法具体分析如下:
在python中调用dll文件中的接口比较简单,如我们有一个test.dll文件,内部定义如下:
extern "C"
{
int __stdcall test( void* p, int len)
{
return len;
}
}
在python中我们可以用以下两种方式载入
1.
import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )
2.
import ctypes
dll = ctypes.WinDll( 'test.dll' )
其中ctypes.windll为ctypes.WinDll类的一个对象,已经在ctypes模块中定义好的。在test.dll中有test接口,可直接用dll调用即可
nRst = dll.test( )
print nRst
由于在test这个接口中需要传递两个参数,一个是void类型的指针,它指向一个缓冲区。一个是该缓冲区的长度。因此我们要获取到python中的字符串的指针和长度
#方法一:
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = ctypes.c_char_p( )
pStr.value = sBuf
pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
nRst = dll.test( pVoid, len( pStr.value) )
#方法二:
test = dll.test
test.argtypes = [ctypes.c_char_p, ctypes.c_int]
test.restypes = ctypes.c_int
nRst = test(sBuf, len(sBuf))
如果修改test.dll中接口的定义如下:
extern "C"
{
int __cdecl test( void* p, int len)
{
return len;
}
}
由于接口中定义的是cdecl格式的调用,所以在python中也需要用相应的类型
1.
import ctypes
dll = ctypes.cdll.LoadLibrary( 'test.dll' )
##注:一般在linux下为test.o文件,同样可以使用如下的方法:
##dll =ctypes.cdll.LoadLibrary('test.o')
2.
import ctypes
dll = ctypes.CDll( 'test.dll' )

2. 有谁知道python怎么调用c#的dll

1、首选运行工具 makepy.py。

3. 请教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# 的动态链接库,两个动态链接库就在同一个目录下运行。

4. python 调用DLL的问题

那就是有内存泄露。内存没有释放。原因可能是多种。一种是你的DLL或者是AntiVC出了错。另外一种是ctypes出错。其中返回的内容没有释放内存。也许只有10个字节,但是长期积累还是很可观。

如果解决这个问题,就是稳定的程序,怎么用都不会坏。

还有一个保守的办法。如果你调用DLL的频率不是特别快。比如每秒不超过2000次。那么你可以将这个功能封装在一个服务进程里。线程再通过接口去访问。该 服务进程即使内存溢出崩溃了。也会自动重新启动。这样你的20个线程基本上只需要重试几次,等服务进程重新启动完成后,就可以获得结果。基本不影响使用。操作系统也很安全。因为进程退出后,所有的遗留错误都会清空。

5. 我现在想把自己写的python模块源代码封装成dll,然后在别的python脚本里调用,可以吗

可以的,只要把python模块转换成dll模块,利用Python自带的ctypes模块加载调用就行。

ctypes 是Python的外部函数库。它提供了与 C语言兼容的数据类型,并允许调用 DLL 或共享库中的函数。可使用该模块以纯 Python 形式对这些库进行封装。

ctypes导出了cdll对象,在 Windows 系统中还导出了windll和oledll对象用于载入动态链接库。通过操作这些对象的属性,你可以载入外部的动态链接库。cdll载入按标准的cdecl调用协议导出的函数,而windll导入的库按stdcall调用协议调用其中的函数。

(5)python能调用dll吗扩展阅读:

加载调用DLL的相关方法:

1、加载DLL

加载的时候要根据你将要调用的函数是符合什么调用约定的。

stdcall调用约定:两种加载方式

Objdll = ctypes.windll.LoadLibrary("dllpath")

Objdll = ctypes.WinDLL("dllpath")

cdecl调用约定:也有两种加载方式

Objdll = ctypes.cdll.LoadLibrary("dllpath")

Objdll = ctypes.CDLL("dllpath")

其实windll和cdll分别是WinDLL类和CDll类的对象。

2、调用dll中的方法

加载dll的时候会返回一个DLL对象(假设名字叫Objdll),利用该对象就可以调用dll中的方法。 e.g.如果dll中有个方法名字叫Add(注意如果经过stdcall声明的方法,如果不是用def文件声明的导出函数或者extern “C” 声明的话,编译器会对函数名进行修改,这个要注意。)

调用:nRet = Objdll.Add(12, 15) 即完成一次调用。

6. python可以调用易语言dll吗

完全可以 dll 都可以调用 通用的

7. python 调用 C++ 编译的 dll

困难啊。通常调用C编写的DLL就比较难。如果C++更难。如果说在linux还过得去。那么在linux下还得去。在windows就更难了。标准不一样。

建议你直接使用cython,轻松就搞定了。ctypes只是偶尔才用一下。

往往调用失败与dll的封装格式,编译的方法,参数,版本都有关系。c++的函数命名方式也不同于C,似乎前面要加下划线。

8. python怎么调用dll共享库

可以的,python中一般有两种方法调用DLL中的函数。1.直接使用函数名,函数名可以用dependencywalker等工具查看。(这个工具在vc或者vs的工具包中)[python]viewplainimportctypesdll=CTYPES.CDLL("test.dll")res=test(3,4)2.使用Ordinal,Ordinal可以用dependencywalker等工具查看。[python]viewplainimportctypesdll=CTYPES.CDLL("test.dll")res=dll[1](3,4)

9. python2.7可以直接import dll库吗

在python中调用dll文件中的接口比较简单,实例代码如下:
如我们有一个test.dll文件,内部定义如下:
extern "C"
{

int __stdcall test( void* p, int len)
{
return len;
}

}

在python中我们可以用以下两种方式载入
1.
import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )

2.
import ctypes
dll = ctypes.WinDll( 'test.dll' )
其中ctypes.windll为ctypes.WinDll类的一个对象,已经在ctypes模块中定义好的。在test.dll中有test接口,可直接用dll调用即可
nRst = dll.test( )
print nRst
由于在test这个接口中需要传递两个参数,一个是void类型的指针,它指向一个缓冲区。一个是该缓冲区的长度。因此我们要获取到python中的字符串的指针和长度
#方法一:
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = ctypes.c_char_p( )
pStr.value = sBuf
pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
nRst = dll.test( pVoid, len( pStr.value) )

#方法二:
test = dll.test
test.argtypes = [ctypes.c_char_p, ctypes.c_int]
test.restypes = ctypes.c_int
nRst = test(sBuf, len(sBuf))
如果修改test.dll中接口的定义如下:
extern "C"
{
int __cdecl test( void* p, int len)
{
return len;
}
}

由于接口中定义的是cdecl格式的调用,所以在python中也需要用相应的类型
1.
import ctypes
dll = ctypes.cdll.LoadLibrary( 'test.dll' )
##注:一般在linux下为test.o文件,同样可以使用如下的方法:
## dll = ctypes.cdll.LoadLibrary('test.o')

2.
import ctypes
dll = ctypes.CDll( 'test.dll' )

阅读全文

与python能调用dll吗相关的资料

热点内容
随车拍app绑定什么设备 浏览:895
方维团购系统源码 浏览:990
linux反弹shell 浏览:156
打印机接口加密狗还能用吗 浏览:299
二板股票源码 浏览:446
度人经pdf 浏览:902
怎么配置android远程服务器地址 浏览:960
java程序员看哪些书 浏览:943
什么app可以免费和外国人聊天 浏览:797
pdf手写笔 浏览:182
别永远伤在童年pdf 浏览:990
爱上北斗星男友在哪个app上看 浏览:421
主力散户派发源码 浏览:671
linux如何修复服务器时间 浏览:61
荣县优途网约车app叫什么 浏览:479
百姓网app截图是什么意思 浏览:229
php如何嵌入html 浏览:817
解压专家怎么传输 浏览:745
如何共享服务器的网络连接 浏览:136
程序员简易表白代码 浏览:170