当前位置:首页 » 编程语言 » python调用dll

python调用dll

发布时间: 2022-09-06 09:39:53

1. python调用dll怎么传入一个指针接收结果

dk 可能是调用Create_Data前开辟的一个空间,dkLen传入该空间大小,以方便函数Create_Data写入结果到指针指向的空间。

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

3. python调用外部C#库的dll文件

importclr
importSystem

clr.AddReferenceToFile("SimpleHash.dll")
fromCommonimport*

classHashPy(SimpleHash):
def__init__(self):
pass

defHashCalc(self,arg1,arg2):
#strtobyte[]
arg1=System.Text.Encoding.Default.GetBytes(arg1)
arg2=System.Text.Encoding.Default.GetBytes(arg2)

returnSimpleHash.HashCalc(self,arg1,arg2)

audiobuff='
12345678
12345678
'
key='12345678'

printHashPy().HashCalc(audiobuff,key)

python ctype只能调用c/c++. 你要调用c#的dll 要用IronPython。如上面的例子

4. 如何用python调用.net中的dll

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

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

}

5. python3,64位的,怎么样调用32位的DLL最方便

工作流程:1.创建一个进程外COM服务器(EXE)。2.将32位dll的接口函数封装为COM服务器的相关接口。3.注册COM服务器*.exe/regserver(注销*.exe/unregserver)。4.64位进程调用32位COM服务器接口,成功。从而曲线实现了64位进程调用32位dll。具体步骤:我首先创建了一个简单的dll工程,只输出一个函数intc=add(inta,intb);生成lib和dll然后创建一个进程外COM(EXE类型),内部链接dll,添加方法Method:Add(long*c){*c=add(1,2);}编译生成。然后注册COM,*.exe/regserver最创建一个64位WIN32工程验证64位环境下方法调用是否正确,经验证正确!!!结论:以上方法可以解决64位进程调用32位dll的问题32位进程调用64位dll应该也可以通过这种方法解决,原因64位windows系统下安装了32位和64位两套COM系统

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

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

7. 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)

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

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

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

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

(8)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) 即完成一次调用。

9. python调用dll怎么返回多个值

多个返回值需要用list集合来解析。。
举例参考一下:
import ctypes
# Load DLL into memory.
hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")
# Set up prototype and parameters for the desired function call.
# HLLAPI
hllApiProto = ctypes.WINFUNCTYPE (
ctypes.c_int, # Return type.
ctypes.c_void_p, # Parameters 1 ...
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p) # ... thru 4.
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),
# Actually map the call ("HLLAPI(...)") to a Python name.
hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)
# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.
p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))

热点内容
跳转页源码 发布:2024-09-17 03:13:05 浏览:543
html文件上传表单 发布:2024-09-17 03:08:02 浏览:784
聊天软件编程 发布:2024-09-17 03:00:07 浏览:726
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207
cf编译后没有黑框跳出来 发布:2024-09-17 01:46:54 浏览:249
安卓怎么禁用应用读取列表 发布:2024-09-17 01:46:45 浏览:524
win10设密码在哪里 发布:2024-09-17 01:33:32 浏览:662
情逢敌手迅雷下载ftp 发布:2024-09-17 01:32:35 浏览:337
安卓如何让软件按照步骤自动运行 发布:2024-09-17 01:28:27 浏览:197