def FormatError(code=None): """ A replacement for ctypes.FormtError, but always returns the string encoded in CP1252 (ANSI Code Page). """ if code is None: code = GetLastError() lpMsgBuf = LPWSTR() numChars = _kernel32.FormatMessageW( ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ), None, code, 0, byref(lpMsgBuf), 0, None ) if numChars == 0: return "No error message available." #raise Exception("FormatMessage failed on 0x%X with 0x%X" % (code & 0xFFFFFFFF, GetLastError())) message = lpMsgBuf.value.strip() _kernel32.LocalFree(lpMsgBuf) return message.encode("CP1252", 'backslashreplace')
def MoveFileEx(oldpath, newpath, flags): """Calls MoveFileEx, converting errors to WindowsError exceptions.""" old_p = fs.extend(oldpath) new_p = fs.extend(newpath) if not windll.kernel32.MoveFileExW(old_p, new_p, int(flags)): # pylint: disable=undefined-variable err = GetLastError() msg = u'MoveFileEx(%s, %s, %d): %s (%d)' % ( oldpath, newpath, flags, FormatError(err), err) raise WindowsError(err, msg.encode('utf-8'))
def GetLongPathName(short_path): """Returns the Windows long path equivalent for a 'short' path.""" path = fs.extend(short_path) chars = windll.kernel32.GetLongPathNameW(path, None, 0) if chars: p = create_unicode_buffer(chars) if windll.kernel32.GetLongPathNameW(path, p, chars): return fs.trim(p.value) err = GetLastError() if err: # pylint: disable=undefined-variable msg = u'GetLongPathName(%s): %s (%d)' % ( short_path, FormatError(err), err) raise WindowsError(err, msg.encode('utf-8'))
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, unicode) # Guesswork. QueryDosDeviceW never returns the required number of bytes. chars = 1024 drive_letter = drive_letter p = create_unicode_buffer(chars) if 0 == windll.kernel32.QueryDosDeviceW(drive_letter, p, chars): err = 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
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 # Guesswork. QueryDosDeviceW never returns the required number of bytes. chars = 1024 drive_letter = unicode(drive_letter) p = create_unicode_buffer(chars) if 0 == windll.kernel32.QueryDosDeviceW(drive_letter, p, chars): err = GetLastError() if err: # pylint: disable=E0602 raise WindowsError( err, 'QueryDosDevice(%s): %s (%d)' % ( str(drive_letter), FormatError(err), err)) return p.value
def GetLongPathName(short_path): """Returns the Windows long path equivalent for a 'short' path.""" assert isinstance(short_path, unicode) # Adds '\\\\?\\' when given an absolute path so the MAX_PATH (260) limit is # not enforced. if os.path.isabs(short_path) and not short_path.startswith('\\\\?\\'): short_path = '\\\\?\\' + short_path chars = windll.kernel32.GetLongPathNameW(short_path, None, 0) if chars: p = create_unicode_buffer(chars) if windll.kernel32.GetLongPathNameW(short_path, p, chars): return p.value err = GetLastError() if err: # pylint: disable=E0602 msg = u'GetLongPathName(%s): %s (%d)' % (short_path, FormatError(err), err) raise WindowsError(err, msg.encode('utf-8'))
def GetShortPathName(long_path): """Returns the Windows short path equivalent for a 'long' path.""" long_path = unicode(long_path) # Adds '\\\\?\\' when given an absolute path so the MAX_PATH (260) limit is # not enforced. if os.path.isabs(long_path) and not long_path.startswith('\\\\?\\'): long_path = '\\\\?\\' + long_path chars = windll.kernel32.GetShortPathNameW(long_path, None, 0) if chars: p = create_unicode_buffer(chars) if windll.kernel32.GetShortPathNameW(long_path, p, chars): return p.value err = GetLastError() if err: # pylint: disable=E0602 raise WindowsError( err, 'GetShortPathName(%s): %s (%d)' % ( str(long_path), FormatError(err), err))