rainit2006 / Python-room

my python room
0 stars 0 forks source link

Python for windows #9

Open rainit2006 opened 6 years ago

rainit2006 commented 6 years ago

目次:

rainit2006 commented 6 years ago

rainit2006 commented 6 years ago

win32com win32comとかPyWin32とかPython for Windows extensionsとかいろいろな名前で呼ばれているが同じもののようだ。

使用例: 1、IEで愚鈍人サイトを表示してみる。

import win32com.client

ie = win32com.client.Dispatch("InternetExplorer.Application")

ie.Visible = True
ie.Addressbar = True
ie.Navigate("http://ichitcltk.hustle.ne.jp/gudon2/index.php")

2, WshShellオブジェクトを使ってnotepadを起動。

import win32com.client

wshShell = win32com.client.Dispatch("WScript.Shell")
wshShell.Run("notepad.exe")

3.判定网络连接情况

    import win32com
    from win32com.client import Dispatch
    comobj = win32com.client.Dispatch('{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')

    int_test = comobj.IsConnectedToInternet

Note: DCB00C01-570F-4A9B-8D69-199FDBA5723Bとは,ネットワークの一覧を呼び出すためのクラスIDである。これはWindowsのクラスID(CLSID)の一つです。 このクラスIDは,レジストリの中で定義されており,特定のプログラムに対応しています。

4, 从注册表键值里取得信息

 key = _winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\SSS Corporation\BBB Care", 0, _winreg.KEY_READ|_winreg.KEY_WOW64_64KEY)
        if key:
            ver_vlu = _winreg.QueryValueEx(key, "Version")[0]

5, 利用WMI判定OS是32还是64

import os
    import win32com.client

    wmi = win32com.client.GetObject('winmgmts:/root/cimv2')
    processors = wmi.ExecQuery('Select AddressWidth from Win32_Processor')
    for processor in processors:
        systemtype = processor.AddressWidth
        break

    print(systemtype)

    if systemtype == 32:
        syspath = 'C:\\Program Files'
    else:
        syspath = 'C:\\Program Files (x86)'

    filepath = syspath + '\\SSS\\BBB Control\\BCCC.exe'
rainit2006 commented 6 years ago

ctypes http://zbdk.hatenablog.jp/entry/2012/01/07/211821

pythonでWindows API(DLL)を実行するには「ctypes」というライブラリを使用すればよいみたい. ctypesでは,ほとんどCと同じことができる.ポインタ,構造体,配列からコールバックまで!

例1

import ctypes
user = ctypes.windll.user32
def getWindowText(hwnd):
    # 文字の長さ取得
    length = user.GetWindowTextLengthW(hwnd) + 1
    # 文字列領域確保
    s = (ctypes.c_wchar * length)()
    # 文字列領域に参照でテキスト取得
    user.GetWindowTextW(hwnd, ctypes.byref(s), length)
    return s.value

例2:

from ctypes import *

user32 = windll.user32

user32.MessageBoxA(
    0, 
    "Hello, MessageBox!", 
    "Python to Windows API", 
    0x00000040)

image

MessageBox 関数は user32.dll にエクスポートされているので、 user32.dll を読み込んでいます。 Windows API については、 ある関数がどの DLL にあるかは dumpbin などするまでもなく MSDN ライブラリなどをみればだいたいわかります。

rainit2006 commented 5 years ago

GUI tkinter 注意:python不同版本时的区别

if sys.version_info[0] < 3:
    import Tkinter as tk
else:
    import tkinter as tk