Module winKernel
[hide private]
[frames] | no frames]

Source Code for Module winKernel

  1  #winKernel.py 
  2  #A part of NonVisual Desktop Access (NVDA) 
  3  #Copyright (C) 2006-2007 NVDA Contributors <http://www.nvda-project.org/> 
  4  #This file is covered by the GNU General Public License. 
  5  #See the file COPYING for more details. 
  6   
  7  import ctypes 
  8  import ctypes.wintypes 
  9  from ctypes import * 
 10  from ctypes.wintypes import * 
 11   
 12  kernel32=ctypes.windll.kernel32 
 13  advapi32 = windll.advapi32 
 14   
 15  #Constants 
 16  INFINITE = 0xffffffff 
 17  #Process control 
 18  PROCESS_ALL_ACCESS=0x1F0FFF 
 19  PROCESS_TERMINATE=0x1 
 20  PROCESS_VM_OPERATION=0x8 
 21  PROCESS_VM_READ=0x10 
 22  PROCESS_VM_WRITE=0X20 
 23  SYNCHRONIZE=0x100000 
 24  PROCESS_QUERY_INFORMATION=0x400 
 25  READ_CONTROL=0x20000 
 26  MEM_COMMIT=0x1000 
 27  MEM_RELEASE=0x8000 
 28  PAGE_READWRITE=0x4 
 29  MAXIMUM_ALLOWED = 0x2000000 
 30  STARTF_USESTDHANDLES = 0x00000100 
 31  #Console handles 
 32  STD_INPUT_HANDLE=-10 
 33  STD_OUTPUT_HANDLE=-11 
 34  STD_ERROR_HANDLE=-12 
 35  LOCALE_USER_DEFAULT=0x800  
 36  DATE_LONGDATE=0x00000002  
 37  TIME_NOSECONDS=0x00000002 
 38   
39 -def GetStdHandle(handleID):
40 h=kernel32.GetStdHandle(handleID) 41 if h==0: 42 raise WinError() 43 return h
44 45 GENERIC_READ=0x80000000 46 GENERIC_WRITE=0x40000000 47 FILE_SHARE_READ=1 48 FILE_SHARE_WRITE=2 49 OPEN_EXISTING=3 50
51 -def CreateFile(fileName,desiredAccess,shareMode,securityAttributes,creationDisposition,flags,templateFile):
52 res=kernel32.CreateFileW(fileName,desiredAccess,shareMode,securityAttributes,creationDisposition,flags,templateFile) 53 if res==0: 54 raise ctypes.WinError() 55 return res
56 57 58
59 -def openProcess(*args):
60 return kernel32.OpenProcess(*args)
61
62 -def closeHandle(*args):
63 return kernel32.CloseHandle(*args)
64 65 #added by Rui Batista to use on Say_battery_status script 66 #copied from platform sdk documentation (with required changes to work in python)
67 -class SYSTEM_POWER_STATUS(ctypes.Structure):
68 _fields_ = [("ACLineStatus", ctypes.c_byte), ("BatteryFlag", ctypes.c_byte), ("BatteryLifePercent", ctypes.c_byte), ("Reserved1", ctypes.c_byte), ("BatteryLifeTime", ctypes.wintypes.DWORD), ("BatteryFullLiveTime", ctypes.wintypes.DWORD)]
69 70
71 -def GetSystemPowerStatus(sps):
72 return kernel32.GetSystemPowerStatus(ctypes.byref(sps))
73
74 -def getThreadLocale():
75 return kernel32.GetThreadLocale()
76
77 -def GetDateFormat(Locale,dwFlags,lpDate,lpFormat):
78 bufferLength=kernel32.GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, None, 0) 79 buf=ctypes.create_unicode_buffer("", bufferLength) 80 kernel32.GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, buf, bufferLength) 81 return buf.value
82
83 -def GetTimeFormat(Locale,dwFlags,lpTime,lpFormat):
84 bufferLength=kernel32.GetTimeFormatW(Locale,dwFlags,lpTime,lpFormat, None, 0) 85 buf=ctypes.create_unicode_buffer("", bufferLength) 86 kernel32.GetTimeFormatW(Locale,dwFlags,lpTime,lpFormat, buf, bufferLength) 87 return buf.value
88
89 -def openProcess(*args):
90 return kernel32.OpenProcess(*args)
91
92 -def virtualAllocEx(*args):
93 res = kernel32.VirtualAllocEx(*args) 94 if res == 0: 95 raise WinError() 96 return res
97
98 -def virtualFreeEx(*args):
99 return kernel32.VirtualFreeEx(*args)
100
101 -def readProcessMemory(*args):
102 return kernel32.ReadProcessMemory(*args)
103
104 -def writeProcessMemory(*args):
105 return kernel32.WriteProcessMemory(*args)
106
107 -def waitForSingleObject(handle,timeout):
108 return kernel32.WaitForSingleObject(handle,timeout)
109 110 SHUTDOWN_NORETRY = 0x00000001 111
112 -def SetProcessShutdownParameters(level, flags):
113 res = kernel32.SetProcessShutdownParameters(level, flags) 114 if res == 0: 115 raise ctypes.WinError()
116
117 -def GetExitCodeProcess(process):
118 exitCode = ctypes.wintypes.DWORD() 119 if not kernel32.GetExitCodeProcess(process, ctypes.byref(exitCode)): 120 raise ctypes.WinError() 121 return exitCode.value
122
123 -def TerminateProcess(process, exitCode):
124 if not kernel32.TerminateProcess(process, exitCode): 125 raise ctypes.WinError()
126 127 DRIVE_UNKNOWN = 0 128 DRIVE_NO_ROOT_DIR = 1 129 DRIVE_REMOVABLE = 2 130 DRIVE_FIXED = 3 131 DRIVE_REMOTE = 4 132 DRIVE_CDROM = 5 133 DRIVE_RAMDISK = 6 134
135 -def GetDriveType(rootPathName):
136 return kernel32.GetDriveTypeW(rootPathName)
137
138 -class SECURITY_ATTRIBUTES(Structure):
139 _fields_ = ( 140 ("nLength", DWORD), 141 ("lpSecurityDescriptor", LPVOID), 142 ("bInheritHandle", BOOL) 143 )
144 - def __init__(self, **kwargs):
145 super(SECURITY_ATTRIBUTES, self).__init__(nLength=sizeof(self), **kwargs)
146
147 -def CreatePipe(pipeAttributes, size):
148 read = ctypes.wintypes.HANDLE() 149 write = ctypes.wintypes.HANDLE() 150 if kernel32.CreatePipe(ctypes.byref(read), ctypes.byref(write), byref(pipeAttributes) if pipeAttributes else None, ctypes.wintypes.DWORD(size)) == 0: 151 raise ctypes.WinError() 152 return read.value, write.value
153
154 -class STARTUPINFOW(Structure):
155 _fields_=( 156 ('cb',DWORD), 157 ('lpReserved',LPWSTR), 158 ('lpDesktop',LPWSTR), 159 ('lpTitle',LPWSTR), 160 ('dwX',DWORD), 161 ('dwY',DWORD), 162 ('dwXSize',DWORD), 163 ('dwYSize',DWORD), 164 ('dwXCountChars',DWORD), 165 ('dwYCountChars',DWORD), 166 ('dwFillAttribute',DWORD), 167 ('dwFlags',DWORD), 168 ('wShowWindow',WORD), 169 ('cbReserved2',WORD), 170 ('lpReserved2',POINTER(c_byte)), 171 ('hSTDInput',HANDLE), 172 ('hSTDOutput',HANDLE), 173 ('hSTDError',HANDLE), 174 )
175 - def __init__(self, **kwargs):
176 super(STARTUPINFOW, self).__init__(cb=sizeof(self), **kwargs)
177 STARTUPINFO = STARTUPINFOW 178
179 -class PROCESS_INFORMATION(Structure):
180 _fields_=( 181 ('hProcess',HANDLE), 182 ('hThread',HANDLE), 183 ('dwProcessID',DWORD), 184 ('dwThreadID',DWORD), 185 )
186
187 -def CreateProcessAsUser(token, applicationName, commandLine, processAttributes, threadAttributes, inheritHandles, creationFlags, environment, currentDirectory, startupInfo, processInformation):
188 if advapi32.CreateProcessAsUserW(token, applicationName, commandLine, processAttributes, threadAttributes, inheritHandles, creationFlags, environment, currentDirectory, byref(startupInfo), byref(processInformation)) == 0: 189 raise WinError()
190
191 -def GetCurrentProcess():
192 return kernel32.GetCurrentProcess()
193
194 -def OpenProcessToken(ProcessHandle, DesiredAccess):
195 token = HANDLE() 196 if advapi32.OpenProcessToken(ProcessHandle, DesiredAccess, byref(token)) == 0: 197 raise WinError() 198 return token.value
199 200 DUPLICATE_SAME_ACCESS = 0x00000002 201
202 -def DuplicateHandle(sourceProcessHandle, sourceHandle, targetProcessHandle, desiredAccess, inheritHandle, options):
203 targetHandle = HANDLE() 204 if kernel32.DuplicateHandle(sourceProcessHandle, sourceHandle, targetProcessHandle, byref(targetHandle), desiredAccess, inheritHandle, options) == 0: 205 raise WinError() 206 return targetHandle.value
207