Python 调用DLL文件文档格式.docx
- 文档编号:18774135
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:17
- 大小:24.27KB
Python 调用DLL文件文档格式.docx
《Python 调用DLL文件文档格式.docx》由会员分享,可在线阅读,更多相关《Python 调用DLL文件文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
#char*--1
szPara=create_string_buffer('
\0'
*100)
dll.PrintInfo(byref(szPara),100);
printszPara.value
#char*--2
sBuf='
aaaaaaaaaabbbbbbbbbbbbbb'
pStr=c_char_p()
pStr.value=sBuf
#pVoid=ctypes.cast(pStr,ctypes.c_void_p).value
dll.PrintInfo(pStr,len(pStr.value))
printpStr.value
#char*--3
strMa="
\0"
*20
FunPrint=dll.PrintInfo
FunPrint.argtypes=[c_char_p,c_int]
#FunPrint.restypes=c_void_p
nRst=FunPrint(strMa,len(strMa))
printstrMa,len(strMa)
#char*--4
pStr2=c_char_p("
printpStr2.value
dll.PrintInfo(pStr2,len(pStr.value))
3、C基本类型和ctypes中实现的类型映射表
ctypes数据类型C数据类型
c_char
char
c_short
short
c_intint
c_longlong
c_ulongunsignlong
c_floatfloat
c_doubledouble
c_void_pvoid
对应的指针类型是在后面加上"
_p"
,如int*是c_int_p等等。
在python中要实现c语言中的结构,需要用到类。
4、DLL中的函数返回一个指针。
虽然这不是个好的编程方法,不过这种情况的处理方法也很简单,其实返回的都是地址,把他们转换相应的python类型,在通过value属性访问。
pchar=dll.getbuffer()
szbuffer=c_char_p(pchar)
printszbuffer.value
5、处理C中的结构体类型
为什么把这个单独提出来说呢,因为这个是最麻烦也是最复杂的,在python里面申明一个类似c的结构体,要用到类,并且这个类必须继承自Structure。
先看一个简单的例子:
C里面dll的定义如下:
typedefstruct_SimpleStruct
{
int
nNo;
floatfVirus;
char
szBuffer[512];
}SimpleStruct,*PSimpleStruct;
typedefconstSimpleStruct*PCSimpleStruct;
extern"
C"
int__declspec(dllexport)PrintStruct(PSimpleStructsimp);
intPrintStruct(PSimpleStructsimp)
{
printf("
nMaxNum=%f,szContent=%s"
simp->
fVirus,simp->
szBuffer);
returnsimp->
nNo;
}
Python的定义:
classSimpStruct(Structure):
_fields_=[("
nNo"
c_int),
("
fVirus"
c_float),
szBuffer"
c_char*512)]
AddDll.dll"
simple=SimpStruct();
simple.nNo=16
simple.fVirus=3.1415926
simple.szBuffer="
magicTong\0"
printdll.PrintStruct(byref(simple))
上面例子结构体很简单,如果结构体里面有指针,甚至是指向结构体的指针,python里面也有相应的处理方法。
下面这个例子来自网上,本来想自己写个,懒得写了,能说明问题就行:
C代码如下:
typedefstruct
charwords[10];
}keywords;
keywords*kws;
unsignedintlen;
}outStruct;
int__declspec(dllexport)test(outStruct*o);
inttest(outStruct*o)
unsignedinti=4;
o->
kws=(keywords*)malloc(sizeof(unsignedchar)*10*i);
strcpy(o->
kws[0].words,"
TheFirstData"
);
kws[1].words,"
TheSecondData"
len=i;
return1;
Python代码如下:
classkeywords(Structure):
_fields_=[('
words'
c_char*10),]
classoutStruct(Structure):
kws'
POINTER(keywords)),
('
len'
c_int),]
o=outStruct()
dll.test(byref(o))
printo.kws[0].words;
printo.kws[1].words;
printo.len
6、例子
说得天花乱坠,嘿嘿,还是看两个实际的例子。
例子一:
这是一个GUID生成器,其实很多第三方的python库已经有封装好的库可以调用,不过这得装了那个库才行,如果想直接调用一些API,对于python来说,也要借助一个第三方库才行,这个例子比较简单,就是用C++调用win32API来产生GUID,然后python通过调用c++写的dll来获得这个GUID
C++代码如下:
__declspec(dllexport)char*newGUID();
char*newGUID()
staticcharbuf[64]={0};
statcGUIDguid;
if(S_OK==:
:
CoCreateGuid(&
guid))
{
//"
%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
_snprintf(buf,sizeof(buf),
"
guid.Data1,
guid.Data2,
guid.Data3,
guid.Data4[0],guid.Data4[1],
guid.Data4[2],guid.Data4[3],
guid.Data4[4],guid.Data4[5],
guid.Data4[6],guid.Data4[7]
);
:
MessageBox(NULL,buf,"
GUID"
MB_OK);
}
return(char*)buf;
defCreateGUID():
"
创建一个全局唯一标识符
类似:
E06093E2-699A-4BF2-A325-4F1EADB50E18
NewVersion
try:
#dllpath
strDllPath=sys.path[0]+str(os.sep)+"
createguid.dll"
dll=CDLL(strDllPath)
b=dll.newGUID()
a=c_char_p(b)
exceptException,error:
printerror
return"
returna.value
例子二:
这个例子是调用kernel32.dll中的createprocessA函数来启动一个记事本进程。
#-*-coding:
utf-8-*-
#定义_PROCESS_INFORMATION结构体
class_PROCESS_INFORMATION(Structure):
hProcess'
c_void_p),
hThread'
dwProcessId'
c_ulong),
dwThreadId'
c_ulong)]
#定义_STARTUPINFO结构体
class_STARTUPINFO(Structure):
cb'
c_ulong),
lpReserved'
c_char_p),
lpDesktop'
lpTitle'
dwX'
dwY'
dwXSize'
dwYSize'
dwXCountChars'
dwYCountChars'
dwFillAttribute'
dwFlags'
wShowWindow'
c_ushort),
cbReserved2'
lpReserved2'
hStdInput'
hStdOutput'
hStdError'
NORMAL_PRIORITY_CLASS=0x00000020
#定义NORMAL_PRIORITY_CLASS
kernel32=windll.LoadLibrary("
kernel32.dll"
)#加载kernel32.dll
CreateProcess=kernel32.CreateProcessA
#获得CreateProcess函数地址
ReadProcessMemory=kernel32.ReadProcessMemory#获得ReadProcessMemory函数地址
WriteProcessMemory=kernel32.WriteProcessMemory#获得WriteProcessMemory函数地址
TerminateProcess=kernel32.TerminateProcess
#声明结构体
ProcessInfo=_PROCESS_INFORMATION()
StartupInfo=_STARTUPINFO()
fileName='
c:
/windows/notepad.exe'
#要进行修改的文件
address=0x0040103c
#要修改的内存地址
strbuf=c_char_p("
_"
)
#缓冲区地址
bytesRead=c_ulong(0)
#读入的字节数
bufferSize=len(strbuf.value)
#缓冲区大小
#创建进程
CreateProcess(fileName,0,0,0,0,NORMAL_PRIORITY_CLASS,0,0,byref(StartupInfo),byref(ProcessInfo))
Python调用Dll的几个例子
fileName="
TestDll.dll"
func=cdll.LoadLibrary(fileName)
#printfunc.HelloWorld()
func.HelloWorld()
(Lib.h)
#ifndefLIB_H
#defineLIB_H
int__declspec(dllexport)add(intx,inty);
#endif
(Lib.cpp)
#include"
Lib.h"
intadd(intx,inty)
returnx+y;
编译为TestDLL.dll。
然后建立一个Python文件TestDLLMain.py测试:
TestDLL.dll"
printdll.add(1,1)
2
简单得不能再简单了!
fromctypesimport*
LRDllTest.dll"
importwin32com.client
#usedll=win32com.client.Dispatch("
Memory.dll"
搞定了几个东西
1,取得鼠标位置,get_mpos()
2,移动鼠标,set_mpos()
3,鼠标左键点击,move_click()
4,键盘输入(部分搞定,那个scancode还不明白是啥意思,为什么0x99输出的是字符'
p'
?
代码如下
[code]
importtime
#<
somepos>
win_start=(9,753)
close_window=(1017,4)
/somepos>
PUL=POINTER(c_ulong)
classKeyBdInput(Structure):
_fields_=[("
wVk"
c_ushort),("
wScan"
dwFlags"
c_ulong),("
time"
dwExtraInfo"
PUL)]
classHardwareInput(Structure):
uMsg"
wParamL"
c_short),("
wParamH"
c_ushort)]
classMouseInput(Structure):
dx"
c_long),("
dy"
mouseData"
c_ulong),("
classInput_I(Union):
ki"
KeyBdInput),("
mi"
MouseInput),("
hi"
HardwareInput)]
classInput(Structure):
type"
ii"
Input_I)]
classPOINT(Structure):
x"
y"
GetPos>
defget_mpos():
orig=POINT()
windll.user32.GetCursorPos(byref(orig))
returnint(orig.x),int(orig.y)
/GetPos>
SetPos>
defset_mpos(pos):
x,y=pos
windll.user32.SetCursorPos(x,y)
/SetPos>
moveandclick>
defmove_click(pos,move_back=False):
origx,origy=get_mpos()
set_mpos(pos)
FInputs=Input*2
extra=c_ulong(0)
ii_=Input_I()
ii_.mi=MouseInput(0,0,0,2,0,pointer(extra))
ii2_=Input_I()
ii2_.mi=MouseInput(0,0,0,4,0,pointer(extra))
x=FInputs((0,ii_),(0,ii2_))
windll.user32.SendInput(2,pointer(x),sizeof(x[0]))
ifmove_back:
set_mpos((origx,origy))
returnorigx,origy
/moveandclick>
defsendkey(scancode,pressed):
FInputs=Input*1
flag=0x8#KEY_SCANCODE
ii_.ki=KeyBdInput(0,0,flag,0,pointer(extra))
InputBox=FInputs((1,ii_))
ifscancode==None:
return
InputBox[0].ii.ki.wScan=scancode
InputBox[0].ii.ki.dwFlags=0x8#KEY_SCANCODE
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Python 调用DLL文件 调用 DLL 文件
![提示](https://static.bdocx.com/images/bang_tan.gif)