Delen via


Voorbeeldcode voor Windows-knop

Dit onderwerp bevat een volledig codevoorbeeld dat laat zien hoe u een Windows-knop omlaag en vervolgens omhoog kunt schakelen. Het voorbeeld bevat de benodigde headerbestanden en bibliotheekfuncties om ervoor te zorgen dat het volledig functioneel is.

Vereiste voorwaarden

Voordat u deze code gebruikt, moet u ervoor zorgen dat u de volgende headerbestanden hebt opgenomen en de vereiste bibliotheken hebt gekoppeld:

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

Codevoorbeeld

Met de volgende code schakelt u de geïdentificeerde Windows-knop omlaag en vervolgens omhoog:

#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";  
}  

Uitleg

  1. Header-bestanden: de benodigde headerbestanden worden aan het begin van de code opgenomen.
  2. GUID-definitie: de GUID voor de GPIO-knoppen melden interface is gedefinieerd.
  3. EnumDefinitie: Een enum voor GPIO-knoptypen wordt gedefinieerd.
  4. GetDevicePath-functie: er wordt een tijdelijke aanduiding voor GetDevicePath opgegeven. Vervang dit door de werkelijke implementatie.
  5. Hoofdfunctie: De InjectButtonPress functie voert de volgende stappen uit:
    • Hiermee haalt u het apparaatpad op.
    • Hiermee opent u het apparaat.
    • Hiermee drukt u de knop naar beneden en omhoog.
    • Hiermee sluit u de apparaatgreep.

Zorg ervoor dat u de tijdelijke aanduidingsfunctie GetDevicePath vervangt door de werkelijke implementatie om het apparaatpad voor uw specifieke apparaat op te halen.