#include "vdd.h" #include #include #include #include #include "gpioctl.h" /** Global variables **/ HANDLE hndFile; HANDLE hVDD; /* VDD module handle */ BOOLEAN IOHook; /* true if we installed a I/O hooked */ static VDD_IO_PORTRANGE PortRange[2]; BOOL VDDInitialize( HANDLE hVdd, DWORD dwReason, LPVOID lpReserved) { VDD_IO_HANDLERS IOHandlers[2]; hVDD = hVdd; switch ( dwReason ) { case DLL_PROCESS_ATTACH: hndFile = CreateFile( "\\\\.\\PortIODev", // Open the Device "file" GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); IOHandlers[0].inb_handler = MyInB; IOHandlers[0].inw_handler = NULL; IOHandlers[0].insb_handler = NULL; IOHandlers[0].insw_handler = NULL; IOHandlers[0].outb_handler = MyOutB; IOHandlers[0].outw_handler = NULL; IOHandlers[0].outsb_handler = NULL; IOHandlers[0].outsw_handler = NULL; PortRange[0].First = IO_PORT_FIRST1; PortRange[0].Last = IO_PORT_LAST1; // Есть деловое предложение // IOHandlers[1]=IOHandlers[0] IOHandlers[1].inb_handler = MyInB; IOHandlers[1].inw_handler = NULL; IOHandlers[1].insb_handler = NULL; IOHandlers[1].insw_handler = NULL; IOHandlers[1].outb_handler = MyOutB; IOHandlers[1].outw_handler = NULL; IOHandlers[1].outsb_handler = NULL; IOHandlers[1].outsw_handler = NULL; PortRange[1].First = IO_PORT_FIRST2; PortRange[1].Last = IO_PORT_LAST2; // hook I/O mapped I/O IOHook = VDDInstallIOHook(hVDD, (WORD)2, PortRange, IOHandlers); break; case DLL_PROCESS_DETACH: // communicate to appropriate Device driver about your departure if (IOHook) VDDDeInstallIOHook(hVDD, 2, PortRange); CloseHandle(hndFile); break; default: break; } return TRUE; } VOID MyInB( WORD Port, PBYTE Buffer ) { ULONG PortNumber = Port; DWORD ReturnedLength; DeviceIoControl( hndFile, // Handle to device IOCTL_GPD_READ_PORT_UCHAR, // IO Control code for Read &PortNumber, // Buffer to driver. sizeof(PortNumber), // Length of buffer in bytes. Buffer, // Buffer from driver. 1, // Length of buffer in bytes. &ReturnedLength, // Bytes placed in DataBuffer. NULL // NULL means wait till op. completes. ); } VOID MyOutB( WORD Port, BYTE Data ) { GENPORT_WRITE_INPUT InputBuffer; DWORD ReturnedLength; InputBuffer.PortNumber = Port; InputBuffer.CharData = Data; DeviceIoControl( hndFile, // Handle to device IOCTL_GPD_WRITE_PORT_UCHAR, // IO Control code for Write &InputBuffer, // Buffer to driver. Holds port & data. 5, // Length of buffer in bytes. NULL, // Buffer from driver. Not used. 0, // Length of buffer in bytes. &ReturnedLength, // Bytes placed in outbuf. Should be 0. NULL // NULL means wait till I/O completes. ); Data = InputBuffer.CharData; }