Codice di esempio del pulsante Windows

In questo argomento viene fornito un esempio di codice completo che illustra come attivare o disattivare un pulsante windows verso il basso e quindi su. L'esempio include i file di intestazione e le funzioni di libreria necessari per garantire che sia completamente funzionante.

Prerequisiti

Prima di usare questo codice, assicurarsi di aver incluso i file di intestazione seguenti e collegato le librerie necessarie:

#include <windows.h>  
#include <setupapi.h>  
#include <initguid.h>  
#include <devpkey.h>  
#include <stdio.h>  

Codice di esempio

Il codice seguente abbassa e poi alza il pulsante Windows identificato.

#include <windows.h>  
#include <setupapi.h>  
#include <initguid.h>  
#include <devpkey.h>  
#include <stdio.h>  
   
// Define the GUID for the GPIO buttons notify interface  
DEFINE_GUID(GUID_GPIOBUTTONS_NOTIFY_INTERFACE,   
0x4a1e55b2, 0xf16f, 0x11d2, 0xbc, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);  
   
// Enum for GPIO button types  
typedef enum {  
    GPIO_BUTTON_WINDOWS = 0x01  
} GPIOBUTTONS_BUTTON_TYPE;  
   
// Function to get the device path  
LPWSTR GetDevicePath(LPGUID InterfaceGuid);  
   
int __cdecl InjectButtonPress(__in int argc, __in_ecount(argc) char **argv) {  
    LPWSTR DevicePath;  
    HANDLE FileHandle;  
    BOOL b;  
    BYTE buffer;  
    HWND hwnd;  
    MSG msg;  
  
    // Get the device path for the GPIO buttons notify interface  
    DevicePath = GetDevicePath((LPGUID)&GUID_GPIOBUTTONS_NOTIFY_INTERFACE);  
    if (DevicePath == NULL) {  
        printf("Failed to get device path.\n");  
        return 1;  
    }  
  
    // Open the device  
    FileHandle = CreateFile(DevicePath,  
                            GENERIC_WRITE,  
                            0,  
                            NULL,  
                            OPEN_EXISTING,  
                            0,  
                            NULL);  
    if (FileHandle == INVALID_HANDLE_VALUE) {  
        printf("Failed to open device.\n");  
        return 1;  
    }  
  
    // Send button down  
    buffer = GPIO_BUTTON_WINDOWS;  
    b = WriteFile(FileHandle, &buffer, sizeof(buffer), NULL, NULL);  
    if (!b) {  
        printf("Failed to send button down.\n");  
        CloseHandle(FileHandle);  
        return 1;  
    }  
  
    // Send button up  
    buffer = GPIO_BUTTON_WINDOWS;  
    b = WriteFile(FileHandle, &buffer, sizeof(buffer), NULL, NULL);  
    if (!b) {  
        printf("Failed to send button up.\n");  
        CloseHandle(FileHandle);  
        return 1;  
    }  
  
    // Close the device handle  
    CloseHandle(FileHandle);  
    return 0;  
}  
   
// Implementation of GetDevicePath function  
LPWSTR GetDevicePath(LPGUID InterfaceGuid) {  
    // Implementation to retrieve the device path  
    // This is a placeholder and should be replaced with actual code  
    return L"\\\\.\\DevicePath";  
}  

Spiegazione

  1. File di intestazione: i file di intestazione necessari sono inclusi all'inizio del codice.
  2. Definizione GUID: viene definito il GUID per l'interfaccia di notifica dei pulsanti GPIO.
  3. Definizione enumerazione: viene definita un'enumerazione per i tipi di pulsante GPIO.
  4. Funzione GetDevicePath: viene fornita una funzione segnaposto per GetDevicePath . Sostituirlo con l'implementazione effettiva.
  5. Funzione main: la InjectButtonPress funzione esegue i passaggi seguenti:
    • Recupera il percorso del dispositivo.
    • Apre il dispositivo.
    • Invia i comandi di pressione e rilascio del pulsante.
    • Chiude l'handle del dispositivo.

Assicurati di sostituire la funzione segnaposto GetDevicePath con l'implementazione concreta per recuperare il percorso del dispositivo per il tuo dispositivo specifico.