本文整理汇总了Python中ctypes.GetLastError方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.GetLastError方法的具体用法?Python ctypes.GetLastError怎么用?Python ctypes.GetLastError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.GetLastError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: win32_path_short
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def win32_path_short (self, path):
if not path:
return ''
path = os.path.abspath(path)
if self.unix:
return path
self._win32_load_kernel()
if not self.GetShortPathName:
try:
import ctypes
self.GetShortPathName = self.kernel32.GetShortPathNameA
args = [ ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int32 ]
self.GetShortPathName.argtypes = args
self.GetShortPathName.restype = ctypes.c_uint32
except: pass
if not self.GetShortPathName:
return path
retval = self.GetShortPathName(path, self.textdata, 2048)
shortpath = self.textdata.value
if retval <= 0:
import ctypes
print 'ERROR(%d): %s'%(ctypes.GetLastError(), path)
return ''
return shortpath
示例2: get_volumes
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def get_volumes(self):
"""Retrieve a list with all the volumes found on all disks."""
volumes = []
volume = ctypes.create_unicode_buffer(chr(0) * self.MAX_PATH)
handle_volumes = kernel32.FindFirstVolumeW(volume, self.MAX_PATH)
if handle_volumes == self.INVALID_HANDLE_VALUE:
raise exception.WindowsCloudbaseInitException(
"FindFirstVolumeW failed: %r")
try:
while True:
volumes.append(volume.value)
found = kernel32.FindNextVolumeW(handle_volumes, volume,
self.MAX_PATH)
if not found:
errno = ctypes.GetLastError()
if errno == self.ERROR_NO_MORE_FILES:
break
else:
raise exception.WindowsCloudbaseInitException(
"FindNextVolumeW failed: %r")
finally:
kernel32.FindVolumeClose(handle_volumes)
return volumes
示例3: get_user_name
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def get_user_name():
global os_encoding
DWORD = c_uint32
nSize = DWORD(0)
windll.advapi32.GetUserNameA(None, byref(nSize))
error = GetLastError()
ERROR_INSUFFICIENT_BUFFER = 122
if error != ERROR_INSUFFICIENT_BUFFER:
raise WinError(error)
lpBuffer = create_string_buffer('', nSize.value + 1)
success = windll.advapi32.GetUserNameA(lpBuffer, byref(nSize))
if not success:
raise WinError()
return lpBuffer.value.decode(encoding = os_encoding).encode("utf8")
示例4: get_process_image_filename
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def get_process_image_filename(self, pid):
process_name = None
if pid == 4:
# Skip the inevitable errno 87, invalid parameter
process_name = 'System'
elif pid:
hProcess = windll.kernel32.OpenProcess(
PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if hProcess:
lpImageFileName = create_string_buffer(MAX_PATH)
if windll.psapi.GetProcessImageFileNameA(hProcess, lpImageFileName, MAX_PATH) > 0:
process_name = os.path.basename(lpImageFileName.value)
else:
self.logger.error('Failed to call GetProcessImageFileNameA, %d' %
(ctypes.GetLastError()))
windll.kernel32.CloseHandle(hProcess)
return process_name
示例5: checkFirmwareType
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def checkFirmwareType(self):
# Load in kernel32.dll
kernel32_dll = ctypes.WinDLL("C:\\Windows\\System32\\kernel32.dll")
# Because we're using bogus parameters in the function call, it
# should always fail (return 0).
if kernel32_dll.GetFirmwareEnvironmentVariableW(ctypes.c_wchar_p(""),
ctypes.c_wchar_p("{00000000-0000-0000-0000-000000000000}"), None,
ctypes.c_int(0)) == 0:
# Check the last error returned to determine firmware type.
# If the error is anything other than ERROR_INVALID_FUNCTION
# or ERROR_NOACCESS, raise an exception.
last_error = ctypes.GetLastError()
if last_error == self._ERROR_INVALID_FUNCTION:
return "Legacy"
elif last_error == self._ERROR_NOACCESS:
return "UEFI"
else:
raise ctypes.WinError()
else:
return "Unknown"
# Check for PAE, SMEP, SMAP, and NX hardware support using CPUID.
示例6: fcntl
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def fcntl(fd, op, arg=0):
if op == F_GETFD or op == F_GETFL:
return 0
elif op == F_SETFD:
# Check that the flag is CLOEXEC and translate
if arg == FD_CLOEXEC:
success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, arg)
if not success:
raise ctypes.GetLastError()
else:
raise ValueError("Unsupported arg")
#elif op == F_SETFL:
## Check that the flag is NONBLOCK and translate
#if arg == os.O_NONBLOCK:
##pass
#result = ioctlsocket(fd, FIONBIO, 1)
#if result != 0:
#raise ctypes.GetLastError()
#else:
#raise ValueError("Unsupported arg")
else:
raise ValueError("Unsupported op")
示例7: QueryDosDevice
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def QueryDosDevice(drive_letter):
"""Returns the Windows 'native' path for a DOS drive letter."""
assert re.match(r'^[a-zA-Z]:$', drive_letter), drive_letter
assert isinstance(drive_letter, six.text_type)
# Guesswork. QueryDosDeviceW never returns the required number of bytes.
chars = 1024
drive_letter = drive_letter
p = wintypes.create_unicode_buffer(chars)
if not windll.kernel32.QueryDosDeviceW(drive_letter, p, chars):
err = ctypes.GetLastError()
if err:
# pylint: disable=undefined-variable
msg = u'QueryDosDevice(%s): %s (%d)' % (
drive_letter, FormatError(err), err)
raise WindowsError(err, msg.encode('utf-8'))
return p.value
示例8: GetShortPathName
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def GetShortPathName(long_path):
"""Returns the Windows short path equivalent for a 'long' path."""
path = fs.extend(long_path)
chars = windll.kernel32.GetShortPathNameW(path, None, 0)
if chars:
p = wintypes.create_unicode_buffer(chars)
if windll.kernel32.GetShortPathNameW(path, p, chars):
return fs.trim(p.value)
err = ctypes.GetLastError()
if err:
# pylint: disable=undefined-variable
msg = u'GetShortPathName(%s): %s (%d)' % (
long_path, FormatError(err), err)
raise WindowsError(err, msg.encode('utf-8'))
return None
示例9: enable_privilege
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def enable_privilege(name):
"""Enables the privilege for the current process token.
Returns:
- True if the assignment is successful.
"""
SE_PRIVILEGE_ENABLED = 2
ERROR_NOT_ALL_ASSIGNED = 1300
size = ctypes.sizeof(TOKEN_PRIVILEGES) + ctypes.sizeof(LUID_AND_ATTRIBUTES)
buf = ctypes.create_string_buffer(size)
tp = ctypes.cast(buf, ctypes.POINTER(TOKEN_PRIVILEGES)).contents
tp.count = 1
tp.get_array()[0].LUID = get_luid(name)
tp.get_array()[0].Attributes = SE_PRIVILEGE_ENABLED
token = get_process_token()
try:
if not AdjustTokenPrivileges(token, False, tp, 0, None, None):
# pylint: disable=undefined-variable
raise WindowsError(
u'AdjustTokenPrivileges(%r): failed: %s' %
(name, ctypes.GetLastError()))
finally:
windll.kernel32.CloseHandle(token)
return ctypes.GetLastError() != ERROR_NOT_ALL_ASSIGNED
示例10: _monitor
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def _monitor(self):
buff = ctypes.create_string_buffer(_BUFF_SIZE)
while not self._stop.isSet():
size_returned = ctypes.c_ulong(0)
result = ctypes.windll.kernel32.ReadDirectoryChangesW(
self._directory_handle,
buff,
ctypes.c_ulong(_BUFF_SIZE),
True, # recursive.
ctypes.c_ulong(_FILE_NOTIFY_CHANGE_ANY),
ctypes.byref(size_returned),
None,
None) # this is a blocking call.
if result == 0 and ctypes.GetLastError() == _ERROR_NOTIFY_ENUM_DIR:
logging.warning('Buffer overflow while monitoring for file changes.')
# we need to notify that something changed anyway
with self._lock:
self._change_set |= {'Unknown file'}
if result != 0 and size_returned.value != 0:
additional_changes = _parse_buffer(buff)
with self._lock:
self._change_set |= additional_changes
self._change_event.set()
示例11: Register
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def Register(self):
success = WTSRegisterSessionNotification(
eg.messageReceiver.hwnd,
NOTIFY_FOR_ALL_SESSIONS
)
if success:
self.inited = True
return
errorNum = GetLastError()
# if we get the error RPC_S_INVALID_BINDING (1702), the system
# hasn't started all needed services. For this reason we wait some
# time and try it again.
if errorNum == 1702:
self.retryCount += 1
if self.retryCount > 60:
# if we tried it to often, give up
eg.PrintError("WTSRegisterSessionNotification timeout")
return
eg.scheduler.AddTask(2.0, self.Register)
return
# some other error has happened
raise SystemError("WTSRegisterSessionNotification", errorNum)
示例12: load_pdb
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def load_pdb(self, pdb_filename):
size = os.path.getsize(pdb_filename)
module_base = 0x10000000
module_base = SymLoadModuleEx(
self.process,
0,
ctypes.c_char_p(pdb_filename.encode('utf-8')),
None,
module_base,
size,
None,
0
)
if not module_base:
raise Exception('Failed SymLoadModuleEx last error: %d' % ctypes.GetLastError())
print('module_base: %x' % module_base)
return module_base
示例13: FindNextVolume
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def FindNextVolume(hSearch):
volume_name = ctypes.create_unicode_buffer(" " * 255)
if kernel32.FindNextVolumeW(hSearch, volume_name, 255) != 0:
return volume_name.value
else:
errno = ctypes.GetLastError()
if errno == winerror.ERROR_NO_MORE_FILES:
FindVolumeClose(hSearch)
return None
raise RuntimeError("FindNextVolume failed (%s)" % errno)
示例14: get_error
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def get_error():
error = ctypes.GetLastError()
return (error, ctypes.FormatError(error))
示例15: _connect_raw
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import GetLastError [as 别名]
def _connect_raw(self):
# This is a similar procedure to the one in
# https://github.com/microsoft/go-winio/blob/master/pipe.go (tryDialPipe)
while True:
try:
return open(self._name, 'wb+', buffering=0)
except (OSError, IOError):
if GetLastError() != self.ERROR_PIPE_BUSY:
raise
time.sleep(0.001) # 1ms