2011
03
23
03
23
pythonでwindowsレジストリからデータを引っ張る
レジストリにはマシンに関する多く(すべて?)のデータが格納されている。自分のほしい値を返す関数が実装されていなくて、レジストリから値を引っ張って解決したことがある。一つが既定のブラウザで開きたいとき(これは後々すでに実装されていることに気付いた)、もう一つがデスクトップのタスクバーの位置を除いた作業領域を知りたかったとき。
実際の作業はとても簡単だった。OpenKeyキーで開いてEnumValueで値を取得するだけ。
実際に書いたスクリプトを以下に。
渡したアドレスを既定のブラウザで開く
================================================================
def OpenBrowser(addresse):
import _winreg
reg = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, 'http\shell\open\command')
brwsr=_winreg.EnumValue(reg, 0)[1]
req = brwsr + ' ' + addresse
os.system(req)
================================================================
デスクトップの作業領域を調べる
================================================================
def WorkArea():
import _winreg
from win32api import GetSystemMetrics
reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,\
'Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2')
pos = _winreg.EnumValue(reg, 0)[1][12]
width = ord(_winreg.EnumValue(reg, 0)[1][16])
height = ord(_winreg.EnumValue(reg, 0)[1][20])
x,y = GetSystemMetrics(0),GetSystemMetrics(1)
if pos == '\x03':#bottom
top,bottom,left,right = 0, y-height, 0, x
elif pos == '\x01':#top
top,bottom,left,right = height, y, 0, x
elif pos == '\x02':#right
top,bottom,left,right = 0, y, 0, x-width
elif pos == '\x00':#left
top,bottom,left,right = 0, y, width, x
return top,bottom,left,right
================================================================
実際の作業はとても簡単だった。OpenKeyキーで開いてEnumValueで値を取得するだけ。
実際に書いたスクリプトを以下に。
渡したアドレスを既定のブラウザで開く
================================================================
def OpenBrowser(addresse):
import _winreg
reg = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, 'http\shell\open\command')
brwsr=_winreg.EnumValue(reg, 0)[1]
req = brwsr + ' ' + addresse
os.system(req)
================================================================
デスクトップの作業領域を調べる
================================================================
def WorkArea():
import _winreg
from win32api import GetSystemMetrics
reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,\
'Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2')
pos = _winreg.EnumValue(reg, 0)[1][12]
width = ord(_winreg.EnumValue(reg, 0)[1][16])
height = ord(_winreg.EnumValue(reg, 0)[1][20])
x,y = GetSystemMetrics(0),GetSystemMetrics(1)
if pos == '\x03':#bottom
top,bottom,left,right = 0, y-height, 0, x
elif pos == '\x01':#top
top,bottom,left,right = height, y, 0, x
elif pos == '\x02':#right
top,bottom,left,right = 0, y, 0, x-width
elif pos == '\x00':#left
top,bottom,left,right = 0, y, width, x
return top,bottom,left,right
================================================================
スポンサーサイト