Share via


Funzione CompareObjectHandles (handleapi.h)

Confronta due handle di oggetti per determinare se fanno riferimento allo stesso oggetto kernel sottostante.

Sintassi

BOOL CompareObjectHandles(
  [in] HANDLE hFirstObjectHandle,
  [in] HANDLE hSecondObjectHandle
);

Parametri

[in] hFirstObjectHandle

Primo handle dell'oggetto da confrontare.

[in] hSecondObjectHandle

Secondo handle dell'oggetto da confrontare.

Valore restituito

Valore booleano che indica se i due handle fanno riferimento allo stesso oggetto kernel sottostante. TRUE se lo stesso, in caso contrario FALSE.

Commenti

La funzione CompareObjectHandles è utile per determinare se due handle del kernel fanno riferimento allo stesso oggetto kernel senza imporre un requisito concesso a entrambi i diritti di accesso specifici per eseguire il confronto. Ad esempio, se un processo desidera determinare se un handle di processo è un handle per il processo corrente, è possibile usare una chiamata a CompareObjectHandles (GetCurrentProcess(), hProcess, per determinare se hProcess fa riferimento al processo corrente. In particolare, questo non richiede l'uso dei diritti di accesso specifici degli oggetti, mentre in questo esempio, chiamando GetProcessId per controllare gli ID processo di due handle di processo impone un requisito che gli handle concedono PROCESS_QUERY_LIMITED_INFORMATION accesso. Tuttavia, è legale che un handle di processo non disponga di tale diritto di accesso concesso a seconda del modo in cui l'handle deve essere usato.

Esempio

L'esempio di codice seguente crea tre handle, due dei quali fanno riferimento allo stesso oggetto, quindi li confronta per mostrare che gli oggetti kernel sottostanti identici restituiranno TRUE, mentre gli oggetti non identici restituiranno FALSE.

#include <windows.h>
#include <stdio.h>
#include <wchar.h>

HANDLE Event1;
HANDLE Event2;
HANDLE Event3;

	// Create a handle to a named event.
Event1 = CreateEventW (NULL, TRUE, FALSE, L"{75A520B7-2C11-4809-B43A-0D31FB1FDD19}");
if (Event1 == NULL) {	ExitProcess (0);	}

	// Create a handle to the same event.
Event2 = CreateEventW (NULL, TRUE, FALSE, L"{75A520B7-2C11-4809-B43A-0D31FB1FDD19}");
if (Event2 == NULL) {	ExitProcess (0);	}

	// Create a handle to an anonymous, unnamed event.
Event3 = CreateEventW (NULL, TRUE, FALSE, NULL);
if (Event3 == NULL) {	ExitProcess (0);	}

	// Compare two handles to the same kernel object.
if (CompareObjectHandles (Event1, Event2) != FALSE) 
	{	// This message should be printed by the program.
		wprintf (L"Event1 and Event2 refer to the same underlying event object.\n");
	}

	// Compare two handles to different kernel objects.
if (CompareObjectHandles (Event1, Event3) == FALSE) 
	{	// This message should be printed by the program.
		wprintf (L"Event1 and Event3 refer to different underlying event objects.  (Error %lu)\n", GetLastError ());		
	}

	// Compare two handles to different kernel objects, each of a different type of kernel object.
	// The comparison is legal to make, though the function will always return FALSE and indicate 
	// a last error status of ERROR_NOT_SAME_OBJECT.
if (CompareObjectHandles (Event1, GetCurrentProcess ()) == FALSE) 
	{	// This message should be printed by the program.
		wprintf (L"Event1 and the current process refer to different underlying kernel objects.  (Error %lu)\n", GetLastError ());
	}

Requisiti

Requisito Valore
Client minimo supportato Windows 10 [app desktop | App UWP]
Server minimo supportato Windows Server 2016 [app desktop | App UWP]
Piattaforma di destinazione Windows
Intestazione handleapi.h (includere Windows.h)
Libreria Kernelbase.lib
DLL Kernelbase.dll

Vedi anche

Handle e Funzioni oggetto