Untitled
unknown
c_cpp
a year ago
2.7 kB
8
Indexable
#include <windows.h>
#include <string>
// Definimos el IOCTL para comunicarnos con el driver
#define IOCTL_MOUSE_MOVE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
// Definimos las macros para clics del mouse
#define MOUSE_LEFT_BUTTON_DOWN 0x0001
#define MOUSE_LEFT_BUTTON_UP 0x0002
// Estructura para enviar datos al driver
typedef struct _MOUSE_INPUT {
long x; // Movimiento en el eje X
long y; // Movimiento en el eje Y
unsigned short button_flags; // Flags para clics del mouse
} MOUSE_INPUT;
// adding error handling since std::cerr is not working
void LogErrorWithCode(const wchar_t* message) {
DWORD error = GetLastError();
wchar_t buffer[256];
swprintf_s(buffer, 256, L"%s (Error code: %lu)\n", message, error);
OutputDebugString(buffer);
}
// Función para mover el mouse
void MoveMouse(HANDLE hDevice, long x, long y, unsigned short button_flags = 0) {
MOUSE_INPUT input = { x, y, button_flags }; // Preparamos la estructura
DWORD returned;
if (!DeviceIoControl(
hDevice, // Handle del driver
IOCTL_MOUSE_MOVE, // Código IOCTL definido
&input, // Datos de entrada
sizeof(input), // Tamaño de los datos de entrada
nullptr, // Sin datos de salida
0, // Tamaño de datos de salida
&returned, // Bytes devueltos
nullptr // Sin estructura OVERLAPPED
)) {
LogErrorWithCode(L"Error al mover el mouse");
}
}
int main() {
// Abrimos una conexión con el driver
HANDLE hDevice = CreateFile(
L"\\\\.\\MouseDriver", // Nombre simbólico del driver (Unicode)
GENERIC_READ | GENERIC_WRITE, // Permisos
0, // No compartir
nullptr, // Sin seguridad adicional
OPEN_EXISTING, // Abrir un dispositivo existente
0, // Atributos normales
nullptr // Sin handle de template
);
if (hDevice == INVALID_HANDLE_VALUE) {
LogErrorWithCode(L"No se pudo abrir el driver");
return 1;
}
// Ejemplo: mover el mouse 100 píxeles a la derecha y 50 píxeles hacia abajo
MoveMouse(hDevice, 100, 50);
// Ejemplo: realizar un clic izquierdo
MoveMouse(hDevice, 0, 0, MOUSE_LEFT_BUTTON_DOWN);
// Ejemplo: mover el mouse hacia la izquierda y liberar el clic
MoveMouse(hDevice, -100, -50, MOUSE_LEFT_BUTTON_UP);
// Cerramos la conexión con el driver
CloseHandle(hDevice);
return 0;
}
Editor is loading...
Leave a Comment