This commit is contained in:
恍兮惚兮 2024-11-10 15:36:39 +08:00
parent 669558de29
commit 7a5d471f66
3 changed files with 39 additions and 24 deletions

View File

@ -9,7 +9,8 @@ def dopathexists(file: str):
return False return False
if not file.strip(): if not file.strip():
return False return False
if windows.check_unc_not_exists(file): file = windows.check_maybe_unc_file(file)
if not file:
return False return False
PathFileExists = windll.Shlwapi.PathFileExistsW PathFileExists = windll.Shlwapi.PathFileExistsW
PathFileExists.argtypes = (wintypes.LPCWSTR,) PathFileExists.argtypes = (wintypes.LPCWSTR,)

View File

@ -327,15 +327,33 @@ def GetLongPathName(file):
return path return path
def get_logical_drivers():
buffsize = 300
buffer = create_unicode_buffer(buffsize)
result = _GetLogicalDriveStringsW(buffsize, buffer)
if not result:
return []
if buffsize < result:
buffer = create_unicode_buffer(result)
result = _GetLogicalDriveStringsW(result, buffer)
drivers = buffer[:result].split("\0")
if drivers and not drivers[-1]:
drivers.pop()
return drivers
def check_unc_file(v: str): def check_unc_file(v: str):
buf = create_unicode_buffer(65535) buf = create_unicode_buffer(65535)
for i in range(26): for A in get_logical_drivers():
A = chr(ord("A") + i) + ":" A = A[:-1]
if _QueryDosDeviceW(A, buf, 65535) != 0: if _QueryDosDeviceW(A, buf, 65535) != 0:
prefixdos = buf.value prefixdos = buf.value
if v.startswith(prefixdos): if v.startswith(prefixdos):
return A + v[len(prefixdos) :] return A + v[len(prefixdos) :]
# Get network drive
# 我操了,使用管理员权限时,这个玩意会失败 # 我操了,使用管理员权限时,这个玩意会失败
if _WNetGetUniversalNameW(A, 1, buf, byref(c_uint(65535))) == 0: if _WNetGetUniversalNameW(A, 1, buf, byref(c_uint(65535))) == 0:
@ -344,26 +362,26 @@ def check_unc_file(v: str):
).contents.lpUniversalName ).contents.lpUniversalName
if v.startswith(prefixnetwork): if v.startswith(prefixnetwork):
return A + v[len(prefixnetwork) :] return A + v[len(prefixnetwork) :]
return None return v
def check_unc_not_exists(v: str): def check_maybe_unc_file(v: str):
# 当路径可能为unc路径且不存在时返回True
# 避免访问不存在unc路径时过长等待导致卡死
# (可能误伤)
if v.startswith("\\"): if v.startswith("\\"):
return check_unc_file(v) is None v = check_unc_file(v)
return False if v.startswith("\\"):
return None
return v
def _GetProcessFileName(hHandle): def _GetProcessFileName(hHandle):
w = create_unicode_buffer(65535) w = create_unicode_buffer(65535)
# 我佛了,太混乱了,不同权限获取的东西完全不一样 # 我佛了,太混乱了,不同权限获取的东西完全不一样
size = DWORD(65535)
if ( if (
_GetModuleFileNameExW(hHandle, None, w, 65535) == 0 _GetModuleFileNameExW(hHandle, None, w, 65535) == 0
and ( and (
_QueryFullProcessImageNameW != 0 _QueryFullProcessImageNameW != 0
and _QueryFullProcessImageNameW(hHandle, 0, w, pointer(c_uint())) == 0 and _QueryFullProcessImageNameW(hHandle, 0, w, pointer(size)) == 0
) )
and _GetProcessImageFileNameW(hHandle, w, 65535) == 0 and _GetProcessImageFileNameW(hHandle, w, 65535) == 0
): ):
@ -371,13 +389,7 @@ def _GetProcessFileName(hHandle):
v: str = w.value v: str = w.value
if v.startswith("\\"): return check_maybe_unc_file(v)
_f = check_unc_file(v)
if _f:
return _f
return v
else:
return v
def GetProcessFileName(hHandle): def GetProcessFileName(hHandle):

View File

@ -197,15 +197,17 @@ _extracticon2data.argtypes = c_wchar_p, c_void_p
_extracticon2data.restype = c_bool _extracticon2data.restype = c_bool
def extracticon2data(fname): def extracticon2data(file):
if windows.check_unc_not_exists(fname):
return None file = windows.check_maybe_unc_file(file)
if not file:
return False
ret = [] ret = []
def cb(ptr, size): def cb(ptr, size):
ret.append(cast(ptr, POINTER(c_char))[:size]) ret.append(cast(ptr, POINTER(c_char))[:size])
succ = _extracticon2data(fname, CFUNCTYPE(None, c_void_p, c_size_t)(cb)) succ = _extracticon2data(file, CFUNCTYPE(None, c_void_p, c_size_t)(cb))
if not succ: if not succ:
return None return None
return ret[0] return ret[0]