• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在python ctypes结构传递把Windows API

用户头像
it1352
帮助1

问题说明

我正在尝试在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?

正确答案

#1

我正在使用 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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkeie
系列文章
更多 icon
同类精品
更多 icon
继续加载