python内置库之学习ctypes库(三)--调用Win32API

Alt

1.调用win32的api

task.py

from data import *
from code import *


class Win32_Api(object):
    def __init__(self):
        self.__KERNEL32=windll.kernel32
        self.__USER32=windll.user32
        self.__GDI32=windll.gdi32

    def dump_dict(self, structure):
        """
        传进来一个结构体,返回字典
        目前只解析单一结构体和嵌套结构体
        :param structure:
        :return:
        """
        info = {}
        if '_fields_' in dir(structure):
            for k, v in structure._fields_:
                av = getattr(structure, k)
                if type(v) == type(Structure):
                    av = self.dump_dict(av)
                info[k] = av
            return info

    def number_msg(self,number,msg):
        try:
            data=msg["code"].value[str(number)]
            return data
        except:
            ...

    #############################################################
    def GetTimeZoneInformation(self):
        """
        检索当前的时区参数。这些参数控制协调世界时(UTC)与本地时间之间的转换。
        :return:
        """
        GetTimeZoneInformation = self.__KERNEL32.GetTimeZoneInformation
        t = TIME_ZONE_INFORMATION()
        res = GetTimeZoneInformation(pointer(t))
        print("res", res)
        # 第一种取值方法
        # print("Bias",t.Bias)
        # print("StandardName",t.StandardName)
        # data = {
        #     "wYear": t.StandardDate.wYear,
        #     "wMonth": t.StandardDate.wMonth,
        #     "wDayOfWeek": t.StandardDate.wDayOfWeek,
        #     "wDay": t.StandardDate.wDay,
        #     "wHour": t.StandardDate.wHour,
        #     "wMinute": t.StandardDate.wMinute,
        #     "wSecond": t.StandardDate.wSecond,
        #     "wMilliseconds": t.StandardDate.wMilliseconds
        # }
        # print("StandardDate",data)
        # print("StandardBias",t.StandardBias)
        # print("DaylightName",t.DaylightName)
        # data={
        #     "wYear": t.DaylightDate.wYear,
        #     "wMonth": t.DaylightDate.wMonth,
        #     "wDayOfWeek": t.DaylightDate.wDayOfWeek,
        #     "wDay": t.DaylightDate.wDay,
        #     "wHour": t.DaylightDate.wHour,
        #     "wMinute": t.DaylightDate.wMinute,
        #     "wSecond": t.DaylightDate.wSecond,
        #     "wMilliseconds": t.DaylightDate.wMilliseconds
        # }
        # print("DaylightDate",data)
        # print("DaylightBias",t.DaylightBias)

        # 第二种取值方法(有对比,才有伤害,更能显出其他方法的便利)
        data = self.dump_dict(t)
        print("Data", data)

    def AnyPopup(self):
        """
        AnyPopup功能表示屏幕上是否存在拥有,
        可见,顶级弹出窗口或重叠窗口。
        该函数搜索整个Windows屏幕,而不仅仅是呼叫应用程序的客户端区域。
        :return:
        如果弹出窗口存在,则返回值不为零,即使弹出窗口被其他窗口完全覆盖。
        如果弹出窗口不存在,返回值为零。
        """
        AnyPopup=self.__USER32.AnyPopup
        res=AnyPopup()
        print("res",res)

    def GetACP(self):
        """
        :return:
        """
        res = self.__KERNEL32.GetACP()
        print("res:", res)
        msg=self.number_msg(res,GetACP_code)
        print(msg)
        # 936





2.最好让结构体和程序分开

创建data.py

from ctypes import *
from ctypes.wintypes import *

class SYSTEMTIME(Structure):
    """
    SYSTEMTIME结构表示使用个人成员进行月,日,工作日,小时,分钟,秒和毫秒的日期和时间
    """
    _fields_ = [
        ("wYear", WORD),
        ("wMonth", WORD),
        ("wDayOfWeek", WORD),
        ("wDay", WORD),
        ("wHour", WORD),
        ("wMinute", WORD),
        ("wSecond", WORD),
        ("wMilliseconds", DWORD)
    ]

class TIME_ZONE_INFORMATION(Structure):
    """
    TIME_ZONE_INFORMATION结构指定特定于时区的信息
    """
    _fields_=[
        ("Bias",LONG),
        ("StandardName",WCHAR*32),
        ("StandardDate",SYSTEMTIME),
        ("StandardBias",LONG),
        ("DaylightName",WCHAR*32),
        ("DaylightDate",SYSTEMTIME),
        ("DaylightBias",LONG)
    ]

实现原理:你给他想要的数据结构,他把得到的数据填充到你给的数据结构当中,你再取值,就是这么简单

3.取完数据找到对应信息,创建code.py

from enum import Enum,unique

@unique
class GetACP_code(Enum):
    code={
        "874":"泰国",
        "932":"日本",
        "936":"中国人(中国,新加坡)",
        "949":"朝鲜的",
        "950":"中文(台湾,香港)",
        "1200":"Unicode(ISO 10646的BMP)",
        "1250":"Windows 3.1东欧",
        "1251":"Windows 3.1西里尔文",
        "1252":"Windows 3.1 Latin 1(美国,西欧)",
        "1253":"Windows 3.1希腊语",
        "1254":"Windows 3.1土耳其语",
        "1255":"希伯来语",
        "1256":"阿拉伯",
        "1257":"波罗的海的"
    }

4.创建main.py,代码这样看着就很简洁

from task import Win32_Api

def main():
    cc = Win32_Api()
    cc.GetTimeZoneInformation()
    cc.AnyPopup()
    cc.GetACP()
    
if __name__ == '__main__':
    main()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bruce-li__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值