1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <windows.h> #include <winioctl.h> #include <stdio.h>
#define IOCTL_MY_CONTROL_CODE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
int main() { HANDLE hDevice = CreateFile(L"\\\\.\\MyUniqueDevice", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hDevice == INVALID_HANDLE_VALUE) { printf("Failed to open device. Error: %d\n", GetLastError()); system("pause"); return 1; }
char inputBuffer[100] = "Hello, driver!"; char outputBuffer[100] = { 0 }; DWORD bytesReturned; BOOL success = DeviceIoControl(hDevice, IOCTL_MY_CONTROL_CODE, inputBuffer, sizeof(inputBuffer), outputBuffer, sizeof(outputBuffer), &bytesReturned, NULL); if (success) { printf("Received from driver: %s\n", outputBuffer); } else { printf("DeviceIoControl failed. Error: %d\n", GetLastError()); }
CloseHandle(hDevice); system("pause"); return 0; }
|