def open_process_token(handle, desired_access, &block)
token_handle = nil
begin
FFI::MemoryPointer.new(:handle, 1) do |token_handle_ptr|
result = OpenProcessToken(handle, desired_access, token_handle_ptr)
if result == Facter::Util::Windows::FFI::WIN32_FALSE
raise Facter::Util::Windows::Error.new(
"OpenProcessToken(#{handle}, #{desired_access.to_s(8)}, #{token_handle_ptr})")
end
yield token_handle = Facter::Util::Windows::FFI.read_handle(token_handle_ptr)
end
token_handle
ensure
Facter::Util::Windows::FFI::WIN32.CloseHandle(token_handle) if token_handle
end
nil
end