2. 调用动态连接库方法

  1. 加载动态连接库。 采用load的方式加载python

    from ctypes import *
    	handle = cdll.LoadLibrary("./test.dll")
    	print(handle)

    可能出现的错误code

    FileNotFoundError: Could not find module './dest.dll'. Try using the full path with constructor syntax.
    	OSError: [WinError 193] %1 不是有效的 Win32 应用程序。

    这两个都是版本不兼容的缘由。 只须要修改32 | 64位编译便可。get

  2. 访问方法。it

    cpp代码io

    #include<stdio.h>
    	extern "C" int test()
    	{
    		printf("hello test\n");
    		return 0;
    	}

    python代码编译

    from ctypes import *
    	handle = cdll.LoadLibrary("./test.dll")
    	handle.test()

    编译执行。class

    g++ -shared -fPIC -m32 test.cpp -o test.dllorg++ -shared -fPIC -m64 test.cpp -o test.dlltest

    python test.pyimport

  3. 另外一种访问连接库方法。module

    from ctypes import *
    	handle = cdll.LoadLibrary("./test.dll")
    	getattr(handle,"test")()

    由于有可能动态连接库的方法是一种不符合python命名规范进行命名的。如1231asfdasdfsa#!QW。方法名是以数字开头,并且中间还包含有特殊字符。所以能够经过attr的方式进行访问。

  4. 可能会如下标的方式进行访问。如cdll.kernel32[1]

相关文章
相关标签/搜索