Freigeben über


Beispielcode für Windows-Schaltfläche

Dieses Thema enthält ein vollständiges Codebeispiel, das veranschaulicht, wie sie eine Windows-Schaltfläche nach unten und dann nach oben umschalten. Das Beispiel enthält erforderliche Headerdateien und Bibliotheksfunktionen, um sicherzustellen, dass es voll funktionsfähig ist.

Voraussetzungen

Stellen Sie vor der Verwendung dieses Codes sicher, dass Sie die folgenden Headerdateien eingefügt und die erforderlichen Bibliotheken verknüpft haben:

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

Codebeispiel

Mit dem folgenden Code wird die identifizierte Windows-Schaltfläche nach unten und dann nach oben umgeschaltet:

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

Erklärung

  1. Headerdateien: Die erforderlichen Headerdateien sind am Anfang des Codes enthalten.
  2. GUID-Definition: Die GUID für die GPIO-Schaltflächenbenachrichtigungsschnittstelle ist definiert.
  3. Enum-Definition: Ein Enum für GPIO-Schaltflächentypen wird definiert.
  4. GetDevicePath-Funktion: Eine Platzhalterfunktion für GetDevicePath wird bereitgestellt. Ersetzen Sie dies durch die tatsächliche Implementierung.
  5. Hauptfunktion: Die InjectButtonPress Funktion führt die folgenden Schritte aus:
    • Ruft den Gerätepfad ab.
    • Öffnet das Gerät.
    • Sendet die Befehle zum Drücken und Loslassen der Taste.
    • Schließt den Gerätehandle.

Stellen Sie sicher, dass Sie die Platzhalterfunktion GetDevicePath durch die tatsächliche Implementierung ersetzen, um den Gerätepfad für Ihr bestimmtes Gerät abzurufen.