免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3335 | 回复: 4
打印 上一主题 下一主题

求教调用dll函数返回值为数据结构的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-04-07 15:41 |只看该作者 |倒序浏览
dll:

typedef struct{
    unsigned char*  pBuf;
    UINT                        dwBufLen;
    BYTE                        byRes[64];                                                                                       
}WTOE_PLAY_FRAME, *LPWTOE_PLAY_FRAME;

LPWTOE_PLAY_FRAME CreateCurFrame(LONG nPlayerHandle)

Python file:
from ctypes import *

nPlayerHandle =1
ret = dll.CreateCurFrame(nPlayerHandle)
需要打印ret返回的数据结构

ret返回为整形,通过restype可以转为c_char_p, 但怎么转换为数据结构指针呢?

论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
2 [报告]
发表于 2016-04-07 16:17 |只看该作者
__date__ = "$Date: 2009/09/07 20:00:00 $"
# 修改记录:
# 2009-09-09,增加文件格式说明
###############################################################################
#系统模块
from ctypes import *
from ctypes.wintypes import *
from win32con  import *

k32 = windll.Kernel32
u32 = windll.user32
g32 = windll.gdi32


class _siWW(Structure):
        _fields_ = [("wProcessorArchitecture", WORD),
                                ("wReserved"             , WORD),]

class _siU(Union):
        _anonymous_ = ("ww",)
        _fields_ = [("dwOemId", DWORD),
                                ("ww"     , _siWW),]


class SYSTEM_INFO(Structure):
        _anonymous_ = ('u')
        _fields_ = [("u", _siU),
                                ("dwPageSize", DWORD),
                                ("lpMinimumApplicationAddress", LPVOID),
                                ("lpMaximumApplicationAddress", LPVOID),
                                ("dwActiveProcessorMask", DWORD),
                                ("dwNumberOfProcessors", DWORD),
                                ("dwProcessorType", DWORD),
                                ("dwAllocationGranularity", DWORD),
                                ("wProcessorLevel", WORD),
                                ("wProcessorRevision", WORD),
                                ]


class MEMORY_BASIC_INFORMATION(Structure):
        _fields_ = [("BaseAddress", LPVOID),
                            ("AllocationBase", LPVOID),
                            ("AllocationProtect", DWORD),
                            ("RegionSize", UINT),
                            ("State", DWORD),
                            ("Protect", DWORD),
                            ("Type", DWORD),]


参考这个

论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
3 [报告]
发表于 2016-04-07 16:21 |只看该作者
# coding=UTF-8
###############################################################################
# 自动化测试工具
# 模拟win32窗口行为
#
__author__ = "@nj"
# 版本号
__version__ = "$Revision: 1.0 $"
# 版权信息
__copyright__ = "Copyleft (c) 2001 li.rongqing"
__license__ = "Python"

###############################################################################
#系统模块
import time
import threading
import ctypes, ctypes.wintypes as _wintypes
import collections

#const NAME
import win32con
#new
win32con.WM_XBUTTONDOWN   = 0x20B
win32con.WM_XBUTTONUP     = 0x20C
win32con.WM_XBUTTONDBLCLK = 0x20D
win32con.WM_MOUSEHWHEEL   = 0x20E
msgname = {
    win32con.WM_MOUSEMOVE     : 'WM_MOUSEMOVE    ',
    win32con.WM_MOUSEMOVE     : 'WM_MOUSEMOVE    ',
    win32con.WM_LBUTTONDOWN   : 'WM_LBUTTONDOWN  ',
    win32con.WM_LBUTTONUP     : 'WM_LBUTTONUP    ',
    win32con.WM_LBUTTONDBLCLK : 'WM_LBUTTONDBLCLK',
    win32con.WM_RBUTTONDOWN   : 'WM_RBUTTONDOWN  ',
    win32con.WM_RBUTTONUP     : 'WM_RBUTTONUP    ',
    win32con.WM_RBUTTONDBLCLK : 'WM_RBUTTONDBLCLK',
    win32con.WM_MBUTTONDOWN   : 'WM_MBUTTONDOWN  ',
    win32con.WM_MBUTTONUP     : 'WM_MBUTTONUP    ',
    win32con.WM_MBUTTONDBLCLK : 'WM_MBUTTONDBLCLK',
    win32con.WM_MOUSEWHEEL    : 'WM_MOUSEWHEEL   ',
    win32con.WM_XBUTTONDOWN   : 'WM_XBUTTONDOWN  ',
    win32con.WM_XBUTTONUP     : 'WM_XBUTTONUP    ',
    win32con.WM_XBUTTONDBLCLK : 'WM_XBUTTONDBLCLK',
    win32con.WM_MOUSEHWHEEL   : 'WM_MOUSEHWHEEL  ',
}

_user32   = ctypes.windll.user32
_kernel32 = ctypes.windll.kernel32

'''
封装 HOOK 函数 SetWindowsHookEx
'''
class win32hook:
    def __init__(self,wh=None):
        self._thread = None #线程
        self._hookid = 0    #标识号
        self.wh_xxx = wh    #钩子类型
        if not wh and hasattr(self, '_wh_type'):
            self.wh_xxx = self._wh_type
        return
    #
    def start(self):
        if self._thread is None:
            self._thread = threading.Thread(target=self.__hook_thread)
            self._thread.start()
        return
    def stop(self):
        if self._thread is None:
            return
        _user32.PostThreadMessageW(self._thread.ident,win32con.WM_QUIT,0,0)
        self._thread.join()
        self._thread = None
        return
    def __hook_thread(self):
        LRESULT=ctypes.c_long
        HOOKPROC = _wintypes.WINFUNCTYPE(LRESULT,
            ctypes.c_int, _wintypes.WPARAM, _wintypes.LPARAM)
        MouseHookFunc = HOOKPROC(self.__hook_func)
        self._hookid = _user32.SetWindowsHookExW(
            self.wh_xxx,
            MouseHookFunc,
            _kernel32.GetModuleHandleW(None),
            0)
        #print 'SetWindowsHookExW',self._hookid
        if self._hookid == 0:
            raise OSError("could not register keyboard hook")
        msg = _wintypes.MSG()
        while _user32.GetMessageW(ctypes.byref(msg),None,0,0):
            _user32.TranslateMessage(byref(msg))
            _user32.DispatchMessage(byref(msg))
            pass
        if _user32.UnhookWindowsHookEx(self._hookid)==0:
           raise OSError("could not unregister key hook %s"%self._hookid)
        self._hookid = 0
        return
    def __hook_func(self,nCode,wParam,lParam):
        if nCode < win32con.HC_ACTION:
            return _user32.CallNextHookEx(self._hookid,nCode,wParam,lParam)
        if hasattr(self, '_hook_func'):
            self._hook_func(nCode,wParam,lParam)
        return _user32.CallNextHookEx(self._hookid,nCode,wParam,lParam)
    wh2class = {}
    @staticmethod
    def register( cls):
        print 'register',cls.__name__, cls._wh_type
        win32hook.wh2class[ cls._wh_type] = cls
    @staticmethod
    def creator(wh_type):
        cls = win32hook.wh2class[wh_type]
        assert(wh_type == cls._wh_type)
        print 'instance', cls.__name__, cls._wh_type
        return cls

#这里是对参数进行解码的...

class MSLLHOOKSTRUCT(ctypes.Structure):
    _fields_=[
        #The x- and y-coordinates of the cursor, in screen coordinates.
        ('pt'         , _wintypes.POINT),
#        '''
#        If the message is WM_MOUSEWHEEL,
#        the high-order word of this member is the wheel delta.
#        The low-order word is reserved.
#        A positive value indicates that the wheel was rotated forward,
#        away from the user;
#        a negative value indicates that the wheel was rotated backward,
#        toward the user.
#        One wheel click is defined as WHEEL_DELTA, which is 120.
#
#        If the message is WM_XBUTTONDOWN,
#        WM_XBUTTONUP, WM_XBUTTONDBLCLK,
#        WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
#        or WM_NCXBUTTONDBLCLK,
#        the high-order word specifies which X button was pressed or released,
#        and the low-order word is reserved.
#        This value can be one or more of the following values.
#        Otherwise, mouseData is not used.
#        Value         Meaning
#        XBUTTON1
#        0x0001        The first X button was pressed or released.
#        XBUTTON2
#        0x0002        The second X button was pressed or release
#        '''
        ('mouseData'  ,_wintypes.DWORD),
        #The event-injected flag. An application can use the following value to test the mouse flags.
        # LLMHF_INJECTED 0x00000001 Test the event-injected flag.
        ('flags'      ,_wintypes.DWORD),
        #The time stamp for this message.
        ('time'       ,_wintypes.DWORD),
        #Additional information associated with the message.
        ('dwExtraInfo',_wintypes.DWORD),
    ]

class mousehook(win32hook):
    _wh_type = win32con.WH_MOUSE_LL
    def __init__(self):
        win32hook.__init__(self, self._wh_type)
        return
    def _hook_func(self,nCode,wParam,lParam):
        msll = MSLLHOOKSTRUCT.from_address(lParam)
        print '%16s :pt=(% 4d,% 4d) mouseData=% 4d,flag=% 4d, %08X'%(
            msgname[wParam],msll.pt.x,msll.pt.y,msll.mouseData,msll.flags,msll.dwExtraInfo)

win32hook.register(mousehook)


class KBDLLHOOKSTRUCT(ctypes.Structure):
    _fields_=[
        ('vkCode'     ,_wintypes.DWORD),
        ('scanCode'   ,_wintypes.DWORD),
        ('flags'      ,_wintypes.DWORD),
        ('time'       ,_wintypes.DWORD),
        ('dwExtraInfo',_wintypes.DWORD),
    ]

class keybdhook(win32hook):
    _wh_type = win32con.WH_KEYBOARD_LL
    def __init__(self):
        win32hook.__init__(self, None)#self._wh_type)
        return
    def _hook_func(self,nCode,wParam,lParam):
        kbll = KBDLLHOOKSTRUCT.from_address(lParam)
        #if not (kbd.flags&LLKHF_UP) and kbd.vkCode!=VK_RETURN:
        KeyboardEvent = collections.namedtuple('KeyboardEvent',
            ['event_type', 'key_code','scan_code', 'alt_pressed', 'time'])
        event_types = {
            win32con.WM_KEYDOWN    : 'key down    ',
            win32con.WM_KEYUP      : 'key up      ',
            win32con.WM_SYSKEYDOWN : 'sys key down', # WM_SYSKEYDOWN, used for Alt key.
            win32con.WM_SYSKEYUP   : 'sys key up  ', # WM_SYSKEYUP, used for Alt key.
            }
        event = KeyboardEvent(event_types[wParam], kbll.vkCode, kbll.scanCode, kbll.flags, kbll.dwExtraInfo)
        print event

win32hook.register(keybdhook)

#过渡设计**
class hookmanager:
    def __init__(self):
        win32hook.__init__(self, None)#self._wh_type)
    pass

def test(whtype):
    m = win32hook.creator(whtype)()
    m.start()
    buf = ''
    while buf!='quit':
        buf=raw_input("Press quit Enter for quit ")
    print "Sending quit message to hook thread"
    m.stop()
    print "done"

if __name__=='__main__':
    test(win32con.WH_MOUSE_LL)
    test(win32con.WH_KEYBOARD_LL)

论坛徽章:
0
4 [报告]
发表于 2016-04-11 15:27 |只看该作者
代码确实能运行。但好像解决不了我的这个问题啊~
返回一个对象指针,有简单些的方法吗?

论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
5 [报告]
发表于 2016-04-13 14:20 |只看该作者
哎,给个饼套脖子上都会饿死....就不会转个圈吃吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP