How to communicate with the driver by sending IOCTL codes?
Communicating with the device drivers from the user-mode apps in Windows can be done by either sending standard I/O requests: Read, Write, or by sending dedicated I/O control codes (IOCTLs).
Sending standard I/O requests are required when writing standard filesystem drivers. Custom I/O Control Codes can be used when communicating with non-standard (custom) drivers.
The sample code to communicate with the driver from user-mode app:
HANDLE hDriver = CreateFile(_T("\\\\.\\MyCustomDrv"), GENERIC_READ | GENERIC_WRITE , 0, NULL, OPEN_EXISTING, 0, NULL);
if (hDriver != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned = 0;
DWORD nDataFromDriver = 0;
DeviceIoControl(hDriver, IOCTL_MYUSERIO_GET_DATA, NULL, 0, &nDataFromDriver, sizeof(DWORD), &dwBytesReturned, NULL);
printf("Data from driver: %04X\n", nDataFromDriver);
CloseHandle(hDriver);
}