#Include <APIConstants.au3>
#Include <Array.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global $hToken, $aAdjust, $aObject, $PID = ProcessExists('Test.exe')
$hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
If (@Error) Or (Not _WinAPI_AdjustTokenPrivileges($hToken, $SE_DEBUG_NAME, $SE_PRIVILEGE_ENABLED, $aAdjust)) Then
MsgBox(16, '', 'You do not have administrator rights.')
Exit
EndIf
$aObject = _ProcessEnumHandles($PID, 14)
_ArrayDisplay($aObject)
If UBound($aObject) > 1 Then
_ProcessCloseHandle($PID, $aObject[1])
EndIf
$aObject = _ProcessEnumHandles($PID, 14)
_ArrayDisplay($aObject)
_WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
_WinAPI_CloseHandle($hToken)
Func _ProcessCloseHandle($PID, $hObject)
Local $hModule, $hProcess, $hProc, $List, $Ret, $Index = 0
$List = _WinAPI_EnumProcessModules($PID)
If @Error Then
Return 0
EndIf
For $i = 1 To $List[0][0]
If Not StringCompare(_WinAPI_PathStripPath($List[$i][1]), 'kernel32.dll') Then
$Index = $i
ExitLoop
EndIf
Next
If Not $Index Then
Return 0
EndIf
$hModule = _WinAPI_LoadLibrary($List[$Index][1])
If Not $hModule Then
Return 0
EndIf
$hProc = _WinAPI_GetProcAddress($hModule, 'CloseHandle')
_WinAPI_FreeLibrary($hModule)
If Not $hProc Then
Return 0
EndIf
$hProcess = _WinAPI_OpenProcess(BitOR($PROCESS_CREATE_THREAD, $PROCESS_QUERY_INFORMATION, $PROCESS_VM_OPERATION, $PROCESS_VM_READ, $PROCESS_VM_WRITE), 0, $PID)
If Not $hProcess Then
Return 0
EndIf
$Ret = DllCall('kernel32.dll', 'ptr', 'CreateRemoteThread', 'ptr', $hProcess, 'ptr', 0, 'dword', 0, 'ptr', $List[$Index][0] + $hProc - $hModule, 'ptr', $hObject, 'dword', 0, 'ptr', 0)
If (Not @Error) And ($Ret[0]) Then
_WinAPI_WaitForSingleObject($Ret[0])
_WinAPI_CloseHandle($Ret[0])
Else
$Ret = 0
EndIf
_WinAPI_CloseHandle($hProcess)
If Not IsArray($Ret) Then
Return 0
Else
Return 1
EndIf
EndFunc ;==>_ProcessCloseHandle
Func _ProcessEnumHandles($PID, $iObjectType)
Local Const $tagSYSTEM_HANDLE_TABLE_ENTRY_INFO = 'ushort ProcessId;ushort Reserved;ubyte ObjectType;ubyte Flags;ushort Value;ptr Address;ulong GrantedAccess'
Local $tSHI = DllStructCreate('ulong;ptr[1048576]')
Local $pSHI = DllStructGetPtr($tSHI, 2)
Local $Size = DllStructGetSize(DllStructCreate($tagSYSTEM_HANDLE_TABLE_ENTRY_INFO))
Local $aResult[101] = [0]
Local $tSHTEI, $Ret
$Ret = DllCall('ntdll.dll', 'uint', 'ZwQuerySystemInformation', 'uint', 16, 'ptr', DllStructGetPtr($tSHI), 'ulong', DllStructGetSize($tSHI), 'ulong*', 0)
If (@Error) Or ($Ret[0]) Then
Return SetError(1, 0, 0)
EndIf
For $i = DllStructGetData($tSHI, 1) To 1 Step -1
$tSHTEI = DllStructCreate($tagSYSTEM_HANDLE_TABLE_ENTRY_INFO, $pSHI + ($i - 1) * $Size)
Switch $tSHTEI.ObjectType
Case $iObjectType
If $tSHTEI.ProcessId = $PID Then
__Inc($aResult)
$aResult[$aResult[0]] = Ptr($tSHTEI.Value)
EndIf
Case Else
EndSwitch
Next
__Inc($aResult, -1)
Return $aResult
EndFunc ;==>_ProcessEnumHandles