Files

65 lines
2.6 KiB
Python

import ctypes
from ctypes import wintypes
rstrtmgr = ctypes.windll.rstrtmgr
class RM_UNIQUE_PROCESS(ctypes.Structure):
_fields_ = [("dwProcessId", wintypes.DWORD),
("ProcessStartTime", wintypes.FILETIME)]
class RM_PROCESS_INFO(ctypes.Structure):
_fields_ = [("Process", RM_UNIQUE_PROCESS),
("strAppName", ctypes.c_wchar * 255),
("strServiceShortName", ctypes.c_wchar * 63),
("ApplicationType", ctypes.c_int),
("AppStatus", ctypes.c_uint),
("TSSessionId", wintypes.DWORD),
("bRestartable", wintypes.BOOL)]
RmStartSession = rstrtmgr.RmStartSession
RmStartSession.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.c_uint32, ctypes.c_wchar_p]
RmStartSession.restype = ctypes.c_uint32
RmRegisterResources = rstrtmgr.RmRegisterResources
RmRegisterResources.argtypes = [ctypes.c_uint32, ctypes.c_uint32, ctypes.c_wchar_p,
ctypes.c_uint32, ctypes.POINTER(ctypes.c_void_p),
ctypes.c_uint32, ctypes.c_wchar_p]
RmRegisterResources.restype = ctypes.c_uint32
RmGetList = rstrtmgr.RmGetList
RmGetList.argtypes = [ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(RM_PROCESS_INFO), ctypes.POINTER(ctypes.c_uint32)]
RmGetList.restype = ctypes.c_uint32
RmEndSession = rstrtmgr.RmEndSession
RmEndSession.argtypes = [ctypes.c_uint32]
RmEndSession.restype = ctypes.c_uint32
session = ctypes.c_uint32(0)
desc = ctypes.create_unicode_buffer("diag")
rc = RmStartSession(ctypes.byref(session), 0, desc)
if rc != 0:
print("RmStartSession failed", rc)
raise SystemExit
path = ctypes.create_unicode_buffer(r"C:\ub-build\userbase\classes\application.yml")
rc = RmRegisterResources(session, 1, path, 0, None, 0, None)
print("RmRegisterResources rc=", rc)
nNeeded = ctypes.c_uint32(0)
nActual = ctypes.c_uint32(0)
reason = ctypes.c_uint32(0)
rc = RmGetList(session, ctypes.byref(nNeeded), ctypes.byref(nActual), None, ctypes.byref(reason))
print("first RmGetList rc=", rc, "needed=", nNeeded.value)
if nNeeded.value > 0:
arr = (RM_PROCESS_INFO * nNeeded.value)()
nActual = ctypes.c_uint32(nNeeded.value)
rc = RmGetList(session, ctypes.byref(nNeeded), ctypes.byref(nActual), arr, ctypes.byref(reason))
print("second RmGetList rc=", rc, "actual=", nActual.value)
for i in range(nActual.value):
p = arr[i]
print("HOLDER PID=", p.Process.dwProcessId, "app=", p.strAppName)
else:
print("NO PROCESS HOLDER (likely ACL/permission, not a lock)")
RmEndSession(session)