Python win32api 模块,GetSystemDirectory() 实例源码
我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用win32api.GetSystemDirectory()。
def which_file(fname):
"""prints paths for fname where fname can be found,
in case of .dll loads it"""
files = []
path = win32api.GetEnvironmentVariable('PATH')
# try paths as described in MSDN
dirs = [os.getcwd(), win32api.GetSystemDirectory(), win32api.GetWindowsDirectory()] + path.split(';')
dirs_norm = []
dirs_l = []
for d in dirs:
dn = d.lower()
if dn not in dirs_l:
dirs_l.append(dn)
dirs_norm.append(d)
for d in dirs_norm:
fname2 = os.path.join(d, fname)
if os.path.exists(fname2):
if fname2 not in files:
files.append(fname2)
if files:
print('\n'.join([get_file_info(f) for f in files]))
h = 0
if fname.lower().endswith('.dll'):
print('\ttrying to load "%s" ...' % (fname))
try:
h = win32api.LoadLibrary(fname)
if h:
dll_name = win32api.GetModuleFileName(h)
print('\t%s loaded' % (dll_name))
except:
print('\tCannot load "%s" !!!' % (fname))
def get_system_path():
"""
Return the path that Windows will search for dlls.
"""
_bpath = []
try:
import win32api
sys_dir = win32api.GetSystemDirectory()
except ImportError:
sys_dir = os.path.normpath(os.path.join(get_windows_dir(), 'system32'))
# Ensure C:\Windows\system32 and C:\Windows directories are
# always present in PATH variable.
# C:\Windows\system32 is valid even for 64bit Windows. Access do DLLs are
# transparently redirected to C:\Windows\syswow64 for 64bit applactions.
# http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx
_bpath = [sys_dir, get_windows_dir()]
_bpath.extend(compat.getenv('PATH', '').split(os.pathsep))
return _bpath
def get_system_path():
"""
Return the path that Windows will search for dlls.
"""
_bpath = []
try:
import win32api
sys_dir = win32api.GetSystemDirectory()
except ImportError:
sys_dir = os.path.normpath(os.path.join(get_windows_dir(), 'system32'))
# Ensure C:\Windows\system32 and C:\Windows directories are
# always present in PATH variable.
# C:\Windows\system32 is valid even for 64bit Windows. Access do DLLs are
# transparently redirected to C:\Windows\syswow64 for 64bit applactions.
# http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx
_bpath = [sys_dir, get_windows_dir()]
_bpath.extend(compat.getenv('PATH', '').split(os.pathsep))
return _bpath
def setConfig():
Pyro.config.PYRO_TRACELEVEL=3
Pyro.config.PYRO_STORAGE = os.path.splitdrive(win32api.GetSystemDirectory())[0]+os.sep
Pyro.config.PYRO_LOGFILE = "Pyro_ES_svc.log"
def setConfig():
Pyro.config.PYRO_TRACELEVEL=3
Pyro.config.PYRO_STORAGE = os.path.splitdrive(win32api.GetSystemDirectory())[0]+os.sep
Pyro.config.PYRO_LOGFILE = "Pyro_NS_svc.log"
Pyro.config.PYRO_NS_URIFILE = os.path.join(Pyro.config.PYRO_STORAGE, "Pyro_NS_URI.txt")
def _SetupList(self):
child_style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | win32con.WS_HSCROLL | win32con.WS_VSCROLL
child_style |= commctrl.LVS_SINGLESEL | commctrl.LVS_SHOWSELALWAYS | commctrl.LVS_REPORT
self.hwndList = win32gui.CreateWindow("SysListView32", None, child_style, 0, 0, 100, 100, self.hwnd, IDC_LISTBOX, self.hinst, None)
child_ex_style = win32gui.SendMessage(self.hwndList, commctrl.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
child_ex_style |= commctrl.LVS_EX_FULLROWSELECT
win32gui.SendMessage(self.hwndList, commctrl.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, child_ex_style)
# Add an image list - use the builtin shell folder icon - this
# demonstrates the problem with alpha-blending of icons on XP if
# winxpgui is not used in place of win32gui.
il = win32gui.ImageList_Create(
win32api.GetSystemMetrics(win32con.SM_CXSMICON),
win32api.GetSystemMetrics(win32con.SM_CYSMICON),
commctrl.ILC_COLOR32 | commctrl.ILC_MASK,
1, # initial size
0) # cGrow
shell_dll = os.path.join(win32api.GetSystemDirectory(), "shell32.dll")
large, small = win32gui.ExtractIconEx(shell_dll, 4, 1)
win32gui.ImageList_ReplaceIcon(il, -1, small[0])
win32gui.DestroyIcon(small[0])
win32gui.DestroyIcon(large[0])
win32gui.SendMessage(self.hwndList, commctrl.LVM_SETIMAGELIST,
commctrl.LVSIL_SMALL, il)
# Setup the list control columns.
lvc = LVCOLUMN(mask = commctrl.LVCF_FMT | commctrl.LVCF_WIDTH | commctrl.LVCF_TEXT | commctrl.LVCF_SUBITEM)
lvc.fmt = commctrl.LVCFMT_LEFT
lvc.iSubItem = 1
lvc.text = "Title"
lvc.cx = 200
win32gui.SendMessage(self.hwndList, commctrl.LVM_INSERTCOLUMN, 0, lvc.toparam())
lvc.iSubItem = 0
lvc.text = "Order"
lvc.cx = 50
win32gui.SendMessage(self.hwndList, commctrl.LVM_INSERTCOLUMN, 0, lvc.toparam())
win32gui.UpdateWindow(self.hwnd)
def get_system_dir():
import win32api # we assume this exists.
try:
import pythoncom
import win32process
from win32com.shell import shell, shellcon
try:
if win32process.IsWow64Process():
return shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_SYSTEMX86)
return shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_SYSTEM)
except (pythoncom.com_error, win32process.error):
return win32api.GetSystemDirectory()
except ImportError:
return win32api.GetSystemDirectory()
def _SetupList(self):
child_style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | win32con.WS_HSCROLL | win32con.WS_VSCROLL
child_style |= commctrl.LVS_SINGLESEL | commctrl.LVS_SHOWSELALWAYS | commctrl.LVS_REPORT
self.hwndList = win32gui.CreateWindow("SysListView32", None, child_style, 0, 0, 100, 100, self.hwnd, IDC_LISTBOX, self.hinst, None)
child_ex_style = win32gui.SendMessage(self.hwndList, commctrl.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
child_ex_style |= commctrl.LVS_EX_FULLROWSELECT
win32gui.SendMessage(self.hwndList, commctrl.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, child_ex_style)
# Add an image list - use the builtin shell folder icon - this
# demonstrates the problem with alpha-blending of icons on XP if
# winxpgui is not used in place of win32gui.
il = win32gui.ImageList_Create(
win32api.GetSystemMetrics(win32con.SM_CXSMICON),
win32api.GetSystemMetrics(win32con.SM_CYSMICON),
commctrl.ILC_COLOR32 | commctrl.ILC_MASK,
1, # initial size
0) # cGrow
shell_dll = os.path.join(win32api.GetSystemDirectory(), "shell32.dll")
large, small = win32gui.ExtractIconEx(shell_dll, 4, 1)
win32gui.ImageList_ReplaceIcon(il, -1, small[0])
win32gui.DestroyIcon(small[0])
win32gui.DestroyIcon(large[0])
win32gui.SendMessage(self.hwndList, commctrl.LVM_SETIMAGELIST,
commctrl.LVSIL_SMALL, il)
# Setup the list control columns.
lvc = LVCOLUMN(mask = commctrl.LVCF_FMT | commctrl.LVCF_WIDTH | commctrl.LVCF_TEXT | commctrl.LVCF_SUBITEM)
lvc.fmt = commctrl.LVCFMT_LEFT
lvc.iSubItem = 1
lvc.text = "Title"
lvc.cx = 200
win32gui.SendMessage(self.hwndList, commctrl.LVM_INSERTCOLUMN, 0, lvc.toparam())
lvc.iSubItem = 0
lvc.text = "Order"
lvc.cx = 50
win32gui.SendMessage(self.hwndList, commctrl.LVM_INSERTCOLUMN, 0, lvc.toparam())
win32gui.UpdateWindow(self.hwnd)