python调用dll方法

<h1 class="title_txt">python调用dll方法
<cite class="fav_csdnstylebykimi"><a class="fav_csdnstylebykimi" title="收藏到我的网摘中,并分享给我的朋友">收藏</a>
</cite>

</h1>
<div class="blogstory">
<p>在python中调用dll文件中的接口比较简单,实例代码如下:</p>
<p>如我们有一个test.dll文件,内部定义如下:</p>
<div style="padding: 4px 5.4pt; background-color: #e6e6e6; background-image: none; background-position: 0% 50%; width: 95%;">
<div>
<span style="color: #0000ff;">extern</span>
<span style="color: #000000;"></span>
<span style="color: #000000;">"</span>
<span style="color: #000000;">C</span>
<span style="color: #000000;">"</span>
<span style="color: #000000;"><br>
{<br><br></span>
<span style="color: #0000ff;">int</span>
<span style="color: #000000;">__stdcalltest(</span>
<span style="color: #0000ff;">void</span>
<span style="color: #000000;">*</span>
<span style="color: #000000;">p,</span>
<span style="color: #0000ff;">int</span>
<span style="color: #000000;">len)<br>
{<br></span>
<span style="color: #0000ff;">return</span>
<span style="color: #000000;">len;<br>
}<br><br>
}<br></span>
</div>
</div>
<p>在python中我们可以用以下两种方式载入</p>
<div style="padding: 4px 5.4pt; background-color: #e6e6e6; background-image: none; background-position: 0% 50%; width: 95%;">
<div>
<span style="color: #000000;">1</span>
<span style="color: #000000;">.<br></span>
<span style="color: #0000ff;">import</span>
<span style="color: #000000;">ctypes<br>
dll</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">ctypes.windll.LoadLibrary(</span>
<span style="color: #800000;">'</span>
<span style="color: #800000;">test.dll</span>
<span style="color: #800000;">'</span>
<span style="color: #000000;">)<br><br></span>
<span style="color: #000000;">2</span>
<span style="color: #000000;">.<br></span>
<span style="color: #0000ff;">import</span>
<span style="color: #000000;">ctypes<br>
dll</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">ctypes.WinDll(</span>
<span style="color: #800000;">'</span>
<span style="color: #800000;">test.dll</span>
<span style="color: #800000;">'</span>
<span style="color: #000000;">)</span>
</div>
</div>
<p>其中ctypes.windll为ctypes.WinDll类的一个对象,已经在ctypes模块中定义好的。在test.dll中有test接口,可直接用dll调用即可</p>
<div style="padding: 4px 5.4pt; background-color: #e6e6e6; background-image: none; background-position: 0% 50%; width: 95%;">
<div>
<span style="color: #000000;">nRst</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">dll.test()<br></span>
<span style="color: #0000ff;">print</span>
<span style="color: #000000;">nRst</span>
</div>
</div>
<p>由于在test这个接口中需要传递两个参数,一个是void类型的指针,它指向一个缓冲区。一个是该缓冲区的长度。因此我们要获取到python中的字符串的指针和长度</p>
<div style="padding: 4px 5.4pt; background-color: #e6e6e6; background-image: none; background-position: 0% 50%; width: 95%;">
<div>
<span style="color: #000000;">#方法一:</span>
</div>
<div>
<span style="color: #000000;">sBuf</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;"></span>
<span style="color: #800000;">'</span>
<span style="color: #800000;">aaaaaaaaaabbbbbbbbbbbbbb</span>
<span style="color: #800000;">'</span>
<span style="color: #000000;"><br>
pStr</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">ctypes.c_char_p()<br>
pStr.value</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">sBuf<br>
pVoid</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">ctypes.cast(pStr,ctypes.c_void_p).value<br>
nRst</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">dll.test(pVoid,len(pStr.value))</span>
</div>
<div></div>
<div>
<span style="color: #000000;">#方法二:</span>
</div>
<div>
<span style="color: #000000;">test = dll.test</span>
</div>
<div>
<span style="color: #000000;">test.argtypes = [ctypes.c_char_p, ctypes.c_int]</span>
</div>
<div>
<span style="color: #000000;">test.restypes = ctypes.c_int</span>
</div>
<div>
<span style="color: #000000;">nRst = test(sBuf, len(sBuf))</span>
</div>
</div>
<p>如果修改test.dll中接口的定义如下:</p>
<div style="padding: 4px 5.4pt; background-color: #e6e6e6; background-image: none; background-position: 0% 50%; width: 95%;">
<div>
<span style="color: #0000ff;">extern</span>
<span style="color: #000000;"></span>
<span style="color: #000000;">"</span>
<span style="color: #000000;">C</span>
<span style="color: #000000;">"</span>
<span style="color: #000000;"><br>
{<br></span>
<span style="color: #0000ff;">int</span>
<span style="color: #000000;">__cdecltest(</span>
<span style="color: #0000ff;">void</span>
<span style="color: #000000;">*</span>
<span style="color: #000000;">p,</span>
<span style="color: #0000ff;">int</span>
<span style="color: #000000;">len)<br>
{<br></span>
<span style="color: #0000ff;">return</span>
<span style="color: #000000;">len;<br>
}<br>
}<br></span>
</div>
</div>
<p>由于接口中定义的是cdecl格式的调用,所以在python中也需要用相应的类型</p>
<div style="padding: 4px 5.4pt; background-color: #e6e6e6; background-image: none; background-position: 0% 50%; width: 95%;">
<div>
<span style="color: #000000;">1</span>
<span style="color: #000000;">.<br></span>
<span style="color: #0000ff;">import</span>
<span style="color: #000000;">ctypes<br>
dll</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">ctypes.cdll.LoadLibrary(</span>
<span style="color: #800000;">'</span>
<span style="color: #800000;">test.dll</span>
<span style="color: #800000;">'</span>
<span style="color: #000000;">)<br>
##注:一般在linux下为test.o文件,同样可以使用如下的方法:<br>
## dll = ctypes.cdll.LoadLibrary('test.o')<br><br></span>
<span style="color: #000000;">2</span>
<span style="color: #000000;">.<br></span>
<span style="color: #0000ff;">import</span>
<span style="color: #000000;">ctypes<br>
dll</span>
<span style="color: #000000;">=</span>
<span style="color: #000000;">ctypes.CDll(</span>
<span style="color: #800000;">'</span>
<span style="color: #800000;">test.dll</span>
<span style="color: #800000;">'</span>
<span style="color: #000000;">)</span>
</div>
</div>
</div>

你可能感兴趣的:(python)