python内置库之学习ctypes库(三)--调用Win32API
生活随笔
收集整理的這篇文章主要介紹了
python内置库之学习ctypes库(三)--调用Win32API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ctypes庫踩坑日記3
- 1.調用win32的api
- 2.最好讓結構體和程序分開
- 3.取完數據找到對應信息,創建code.py
- 4.創建main.py,代碼這樣看著就很簡潔
1.調用win32的api
task.py
from data import *
from code import *class Win32_Api(object):def __init__(self):self.__KERNEL32=windll.kernel32self.__USER32=windll.user32self.__GDI32=windll.gdi32def 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] = avreturn infodef number_msg(self,number,msg):try:data=msg["code"].value[str(number)]return dataexcept:...#############################################################def GetTimeZoneInformation(self):"""檢索當前的時區參數。這些參數控制協調世界時(UTC)與本地時間之間的轉換。:return:"""GetTimeZoneInformation = self.__KERNEL32.GetTimeZoneInformationt = 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.AnyPopupres=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_Apidef main():cc = Win32_Api()cc.GetTimeZoneInformation()cc.AnyPopup()cc.GetACP()if __name__ == '__main__':main()
總結
以上是生活随笔為你收集整理的python内置库之学习ctypes库(三)--调用Win32API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言使用指定字符串替换特定的子串
- 下一篇: 2019秋招面试常考题目