加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Python调用Dll的几个例子

(2014-06-29 19:12:40)

from ctypes import *
fileName="TestDll.dll"
func=cdll.LoadLibrary(fileName)
#print func.HelloWorld()
func.HelloWorld()


(Lib.h)
#ifndef LIB_H
#define LIB_H
extern "C" int __declspec(dllexport)add(int x, int y);
#endif
(Lib.cpp)
#include "Lib.h"

int add(int x, int y)
{
return x + y;
}

编译为TestDLL.dll。
然后建立一个Python文件TestDLLMain.py测试:
from ctypes import *
dll = CDLL("TestDLL.dll")
print dll.add(1, 1)

结果:2

简单得不能再简单了!


from ctypes import *
fileName="LRDllTest.dll"
func=cdll.LoadLibrary(fileName)

import win32com.client
#usedll=win32com.client.Dispatch("Memory.dll")

搞定了几个东西
1,取得鼠标位置,get_mpos()
2,移动鼠标,set_mpos()
3,鼠标左键点击,move_click()
4,键盘输入(部分搞定,那个scancode还不明白是啥意思,为什么0x99输出的是字符'p'???)
代码如下
[code]
from ctypes import *
import time

#
win_start = (9, 753)
close_window = (1017, 4)
#

PUL = POINTER(c_ulong)
class KeyBdInput(Structure):
        _fields_ = [("wVk", c_ushort),("wScan", c_ushort),("dwFlags", c_ulong),("time", c_ulong),("dwExtraInfo", PUL)]

class HardwareInput(Structure):
        _fields_ = [("uMsg", c_ulong),("wParamL", c_short),("wParamH", c_ushort)]

class MouseInput(Structure):
        _fields_ = [("dx", c_long),("dy", c_long),("mouseData", c_ulong),("dwFlags", c_ulong),("time",c_ulong),("dwExtraInfo", PUL)]

class Input_I(Union):
        _fields_ = [("ki", KeyBdInput),("mi", MouseInput),("hi", HardwareInput)]

class Input(Structure):
        _fields_ = [("type", c_ulong),("ii", Input_I)]

class POINT(Structure):
        _fields_ = [("x", c_ulong),("y", c_ulong)]

#
def get_mpos():
        orig = POINT()
        windll.user32.GetCursorPos(byref(orig))
        return int(orig.x), int(orig.y)
#

#
def set_mpos(pos):
        x,y = pos
        windll.user32.SetCursorPos(x, y)
#

#
def move_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]))
        if move_back:
                set_mpos((origx, origy))
        return origx, origy
#


def sendkey(scancode, pressed):
        FInputs = Input * 1
        extra = c_ulong(0)
        ii_ = Input_I()
        flag = 0x8 # KEY_SCANCODE
        ii_.ki = KeyBdInput( 0, 0, flag, 0, pointer(extra) )
        InputBox = FInputs( ( 1, ii_ ) )
       
        if scancode == None:
                return
        InputBox[0].ii.ki.wScan = scancode
        InputBox[0].ii.ki.dwFlags = 0x8 # KEY_SCANCODE
        if not(pressed):
                InputBox[0].ii.ki.dwFlags |= 0x2 # released
        windll.user32.SendInput(1, pointer(InputBox), sizeof(InputBox[0]))
        if pressed:
                print "%x pressed" % scancode
 &
更多来源:http://www.zgjx114.com
nbsp;      else:
                print "%x released" % scancode

if __name__ == '__main__':
        origx, origy = get_mpos()
        print 'Orig Pos:', origx, origy
        #set_mpos(9, 753)
        #move_click(win_start)
        move_click((800,600))
        time.sleep(1)
        sendkey(0x99,1)
        #keyboard_input()
       
        #set_mpos(origx, origy)

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有