-
Notifications
You must be signed in to change notification settings - Fork 7
/
getdir.py
68 lines (61 loc) · 2.06 KB
/
getdir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import os.path
import ctypes
import ctypes.wintypes
import string
from ctypes import windll
from pathlib import Path
psapi = ctypes.WinDLL('psapi.dll')
EnumProcesses = psapi.EnumProcesses
EnumProcesses.restype = ctypes.wintypes.BOOL
GetProcessImageFileName = psapi.GetProcessImageFileNameA
GetProcessImageFileName.restype = ctypes.wintypes.DWORD
kernel32 = ctypes.WinDLL('kernel32.dll')
OpenProcess = kernel32.OpenProcess
OpenProcess.restype = ctypes.wintypes.HANDLE
CloseHandle = kernel32.CloseHandle
drives = []
bitmask = kernel32.GetLogicalDrives()
for letter in string.ascii_uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
def get_path():
arch = 32
found = False
while True:
ProcessIds = (ctypes.wintypes.DWORD*arch)()
cb = ctypes.sizeof(ProcessIds)
BytesReturned = ctypes.wintypes.DWORD()
if EnumProcesses(ctypes.byref(ProcessIds), cb, ctypes.byref(BytesReturned)):
if BytesReturned.value<cb:
break
else:
arch *= 2
else:
sys.exit("Call to EnumProcesses failed")
for index in range(int(BytesReturned.value / ctypes.sizeof(ctypes.wintypes.DWORD))):
ProcessId = ProcessIds[index]
hProcess = OpenProcess(0x0400, False, ProcessId)
if hProcess:
ImageFileName = (ctypes.c_char*260)()
if GetProcessImageFileName(hProcess, ImageFileName, 260)>0:
filename = os.path.basename(ImageFileName.value)
if 'PathOfExile' in filename.decode('utf-8'):
pt = ImageFileName.value.decode('utf-8').split('\\')
path = '/'.join(pt[3:])
found = True
CloseHandle(hProcess)
break
CloseHandle(hProcess)
if found:
for drive in drives:
p = Path(f"{drive}:/{path}")
try:
if p.is_file():
break
except:
continue
return p.parent.as_posix()
else:
return False