本文目錄一覽:
python怎麼導入ctypes
1. 載入Windows系統自帶的dll文件:
#載入cdecl調用約定的dll
msvcrt =cdll.msvcrt
#載入stdcall調用約定的dll
kernel32 =windll.kernel32
2. 載入自己dll文件,假如為addFuncDll,方式如下:
mydll =CDLL(“addFuncDll.dll”)
或者 mydll = cdll.addFuncDll
如果其中有函數add,計算兩個整數的和,則使用方式如下:
result=mydll.add(4,5)
可以多一步指明add函數的參數類型(也可不指明):
mydll.add.argtypes= [c_int,c_int]
3. 結構體在python中定義為Structure的子類如下:
class POINT(Structure):
_fields_ = [(“x”, c_int),
(“y”,c_int)]
_fields中每一項為元組(成員名稱,類型)
結構體還可以用於其他的結構體:
class RECT(Structure):
_fields_ = [(“upperleft”,POINT),
(“lowerright”,POINT)]
python ctypes 問題
我在互動式環境下使用沒問題,你查一下環境吧。另外,庫名可以不加後綴名,因為linux下可能是so後綴的,加了也沒關係。
C:\Users\spython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type “help”, “copyright”, “credits” or “license” for more information.
from ctypes import *
dir(CDLL)
[‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__doc__’, ‘__format__’, ‘__getattr__’,
‘__getattribute__’, ‘__getitem__’, ‘__hash__’, ‘__init__’, ‘__module__’, ‘__new
__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__
str__’, ‘__subclasshook__’, ‘__weakref__’, ‘_func_flags_’, ‘_func_restype_’]
dll=CDLL(“msvcrt”)
dll
CDLL ‘msvcrt’, handle 75b30000 at 2624570
dll=CDLL(“msvcrt.dll”)
dll
CDLL ‘msvcrt.dll’, handle 75b30000 at 2608ed0
python用ctypes操作剪切板遇到問題!!
這邊執行沒有問題,版本如下
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
代碼如下
import ctypes
def get():
”’從剪切板中獲得字元串”’
h=ctypes.WinDLL(‘user32.dll’)
h.OpenClipboard(0)
aa=h.GetClipboardData(13)
ss=ctypes.c_wchar_p(aa)
h.CloseClipboard()
return ss.value
def set(mystr):
”’把字元串放到剪切板中,成功返回1,失敗返回0”’
u=ctypes.WinDLL(‘user32.dll’)
k=ctypes.WinDLL(‘kernel32.dll’)
s=mystr.encode(‘utf-16’)
s=s[2:]+b’\0\0′
ss=ctypes.c_char_p(s)
u.OpenClipboard(0)
u.EmptyClipboard()
k.GlobalAlloc.argtypes=[ctypes.c_uint32,ctypes.c_uint32]
try:
cb=k.GlobalAlloc(0,len(s))
cb=ctypes.c_void_p(cb)
print(type(cb))
ctypes.memmove(cb,ss,len(s))
rr=u.SetClipboardData(13,cb) # 13-unicode
finally:
u.CloseClipboard()
if rr==0:
return 0
else:
return 1
#—–
set(“abcdefg”)
程序返回
class ‘ctypes.c_void_p’
python ctypes 怎麼處理函數返回的一般指針
test.c(動態庫源代碼)
[cpp] view plain copy
// 編譯生成動態庫: gcc -g -fPIC -shared -o libtest.so test.c
#include stdio.h
#include string.h
#include stdlib.h
typedef struct StructPointerTest
{
char name[20];
int age;
}StructPointerTest, *StructPointer;
StructPointer test() // 返回結構體指針
{
StructPointer p = (StructPointer)malloc(sizeof(StructPointerTest));
strcpy(p-name, “Joe”);
p-age = 20;
return p;
}
編譯:gcc -g -fPIC -shared -o libtest.so test.c
call.py(python調用C語言生成的動態庫):
[python] view plain copy
#!/bin/env python
# coding=UTF-8
from ctypes import *
#python中結構體定義
class StructPointer(Structure):
_fields_ = [(“name”, c_char * 20), (“age”, c_int)]
if __name__ == “__main__”:
lib = cdll.LoadLibrary(“./libtest.so”)
lib.test.restype = POINTER(StructPointer)
p = lib.test()
print “%s: %d” %(p.contents.name, p.contents.age)
最後運行結果:
[plain] view plain copy
[zcm@c_py #112]$make clean
rm -f *.o libtest.so
[zcm@c_py #113]$make
gcc -g -fPIC -shared -o libtest.so test.c
[zcm@c_py #114]$./call.py
Joe: 20
[zcm@c_py #115]$
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257438.html