在python ctypes中将结构传递给Windows API [英] Passing Structure to Windows API in python ctypes

查看:119
本文介绍了在python ctypes中将结构传递给Windows API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows 7上的应用程序中的SysDateTimeObject中设置日期。我正在使用python 2.7和ctypes库以及以下代码,该代码尝试将DTM_SETSYSTEMTIME消息发送到SysDateTimeObject:

I'm trying to set the date in a SysDateTimeObject in an application on Windows 7. I'm using python 2.7 and the ctypes library with the following code which tries to send a DTM_SETSYSTEMTIME message to the SysDateTimeObject:

from ctypes import *
from ctypes.wintypes import BOOL,HWND,RECT,LPCSTR,UINT,INT,DWORD,WORD
import sys
import time

class SYSTEMTIME(Structure):
    _fields_=[('wYear',WORD),
              ('wMonth',WORD),
              ('wDayOfWeek',WORD),
              ('wDay',WORD),
              ('wHour',WORD),
              ('wMinute',WORD),
              ('wSecond',WORD),
              ('wMilliseconds',WORD)]

self.user32 = windll.user32
my_time=SYSTEMTIME(2035,0,0,0,0,0,0,0)
self.user32.SendMessageW(window,c_uint(0x1002),0,byref(my_time))

窗口是正确的SysDateTimeObject的HWND,0x1002是DTM_SETSYST的代码EMTIME消息。 SendMessageW的第三个参数是用于启用或禁用DateTimeControl的常量。我可以将其设置为1,并且它将按预期禁用该控件。第四个参数是指向SYSTEMTIME结构的指针。但是,如上所述,它似乎无能为力。我能够发送其他消息,但是当一个函数需要一个指向结构的指针时,事情就会开始失败。我在这里使用ctypes错误吗?

window is a HWND to the correct SysDateTimeObject and 0x1002 is the code for the DTM_SETSYSTEMTIME message. The third parameter of SendMessageW is a constant to enable or disable the DateTimeControl. I can set it to 1 and it will disable the control, as expected. The fourth parameter is a pointer to a filled in SYSTEMTIME structure. However, it seems to do nothing as written above. I am able to send other messages but when a function requires a pointer to a structure, things start failing. Am I using ctypes wrong here?

推荐答案

我正在使用 pywinauto 并遇到了这种需求并设法解决了这一问题。造成问题的原因是,您的 SYSTEMTIME 结构位于您自己进程的私有内存空间中时,您试图在另一个进程中运行它。因此,每当尝试对传递的结构执行任何操作时,它都会失败—访问被拒绝。您需要一个远程内存块来解决该问题。

I was using pywinauto and ran into this need and managed to solve it. The cause of the trouble is that you're trying to run it in a different process, while your SYSTEMTIME structure is in your own process's private memory space. Thus, whenever it tries to do anything with the passed structure, it fails—access denied. You need a remote memory block to resolve the problem.

pywinauto.controls.common_controls._RemoteMemoryBlock 确实可以做到这一点。

pywinauto.controls.common_controls._RemoteMemoryBlock does exactly this.

最终效果是您将拥有如下代码:

The eventual effect is that you will have code like this:

remote_mem = common_controls._RemoteMemoryBlock(window)
remote_mem.Write(my_time)

user32.SendMessageW(window, win32defines.DTM_SETSYSTEMTIME,
        win32defines.GDT_VALID, remote_mem)

如果需要使用DTM_GETSYSTEMTIME,则可以将 my_time = remote_mem.Read(my_time )

If you need to use DTM_GETSYSTEMTIME, you would then put my_time = remote_mem.Read(my_time) after the SendMessage call.

这篇关于在python ctypes中将结构传递给Windows API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆